Being web developers, we all love Ruby on Rails. And we have our own reasons. But, one of the reasons why we love Ruby on Rails (RoR) is the simplicity, which eases the way we code.
One of the simplest functionalities, which most RoR developers love is, creating gems in Ruby.
A gem in Ruby programming language is a software package in which Ruby applications or libraries can be distributed in a single format.
But, then most of our newbie web developers ask these questions-
And to that, the answer is Yes. We can use that method. But sometimes, if the code seems more general than the project-specific code, it should be converted to a gem so that it can be reused and shared with multiple projects. This will ultimately help you to maintain a clean line of code as well as will reduce the duplication of the code.
Recently in one of our projects, there was the functionality of sending notifications using FCM. FCM gem already exists in Rails to fulfil the functionality. But we wanted to integrate upgraded APIs from FCM itself, which the existing gem was not using.
And, how do we do that?
To adapt the newer APIs, one way was to code manually to send the notifications using FCM APIs. But, that is not a good idea because the “notifications” functionality is something which is going to be used in the majority of the projects.
So, we thought to have a private gem which will have a core logic of sending notifications to the devices using FCN non-legacy API, which can be further used in many such projects.
So, we will see the step by step process of how we can create a gem in Ruby on Rails. So here we go!!
Now let’s start with setting up our gem. Here we will be creating a gem with bundler. So to begin creating a gem using Bundler, use the bundle gem command like this:
Before running above command we should know how to name your gem. There are some naming conventions to be followed.
— For example if we have a gem name like gem gem-structure-demo_notifier
— It will be included in one of the files like require ‘gem/structure/demo_notifier’
— And its class or module will have a name like module/class Gem::Structure::DemoNotifier
— For example if we have a gem name like gem demo_notifier
— It will be included in one of the files like require ‘demo_notifier’
— And its class or module will have a name like module/class DemoNotifier
The command we have specified above will create the following file structure for creating a gem.
If this is your first time running the bundle gem command then you will be asked whether you want to include the CODE_OF_CONDUCT.md and LICENSE.txt files with your project.
We will see what all files are generated with gem bundle command and have a brief description for each file :
In the gemspec file as you can see there are dependencies called development dependencies. So what is this exactly?
It’s what you use while creating the gem. Things like rspec, rake and bundler should be in this file, because those might not be necessary for the people adding this gem to their Gemfile.
In this file we will also have to include the version of a gem we are creating. This version will reference DemoNotifier module’s variable VERSION.
Versioning of a gem requires some conventions to be followed as follows:
— Increasing the third number, means the ‘build’ number means that the change was small and did not affect the functionality of the gem.
— Increasing the second number (A minor number) means the changes include adding new functionality, but the gem did not lose compatibility with the previous versions.
— Increasing the first number ( A major number) means that the changes made to the gem were so severe that it’s no longer compatible with the previously released versions.
Now after configuring all the above-mentioned things, it is important to test the written code.
Testing the gem is a very important part when building a gem. It helps to assure you that your code works, and helps others know that the gem is working perfectly fine. When going through any gem, Ruby developers always check a solid test suite as one of the main reasons for trusting that piece of code which they want to include in their applications.
For writing test cases gem file structure also creates rspec related in which you can write the test cases using rspec guidelines and test your code through the test cases. Here in our file structure, the main rspec test cases will be written in demo_notifier/specs/demo_notifier_spec.rb
To run the test cases you can run rspec or bundle exec rspec. If the result is green then the code is working fine and good to go.
Now that we have completed the coding of a gem we want it to be used in other projects. For this, we will run the build command.
This command will build the gem and output a gem file as follows:
Finally, after building the gem it’s time to push the gem. There are two ways of pushing your gem.
For pushing the gem to RubyGems you will need to create an account on RubyGems.org. Once that’s finished, use your email and password when pushing the gem (RubyGems saves the credentials in ~/.gem/credentials for you so you only need to log in once).
After that, it is just a matter of making it available to rails communities around the globe through rubygems.org. This is done with two simple commands:
And once its done run following command :
With this command, the gem will be pushed and will be available on RubyGems.
Sometimes we want to keep our gem private and don’t want to share it with the world. But RubyGems doesn’t support that.
So to keep the gem private you can push your gem to a private Github Repository and include the gem to your project using github url with oauth credentials. And if we don’t want to include oauth credentials to the url then you can add the ssh key to the gem repository and gain access from where you are installing the gem.
This way your gem will become private and will only be accessible to limited people.