Tutorial: Creating a Ruby Gem
Building your first Ruby Gem
*PreReqs*
- Have bundler installed
- Understand what RSpec is and why it’s important
- Have basic knowledge of Ruby
I say ‘first’ because this is geared towards people who probably have never built a gem before, and thus it will be pretty basic. This gem will go to a webpage and return you the title of the webpage. The first thing you want to do is create your new gem.
bundle gem my_gem
cd my_gem
- Open folder with your text editor
We are going to test drive this gem all the way through.
Let’s set up our gemspec now. The gemspec is for gem specific information.
spec.name
– The name of your gemspec.version
– This links to a module which specifies your gem version. (NOTE: Needs to be updated every time you release new content)spec.authors
– Authors of the gem, in an array.spec.email
– Email address(es) of the author(s)spec.summary
– Short summary of the what the gem doesspec.description
– Long summary of the what the gem doesspec.homepage
– I usually put the online repository where the gem is stored
Now we want to set up the gems our gem will be using.
Add the following at the bottom of your gemspec:
1 spec.add_development_dependency "jazz_hands"
2 spec.add_development_dependency "rspec"
3 spec.add_development_dependency "vcr"
4 spec.add_development_dependency "webmock"
5 spec.add_dependency "nokogiri"
add_development_dependency
- Is used for gems that you need during developmentadd_dependency
- Is used for gems that you need during development/production
Now, run bundle
in your console
Next, run rspec --init
to create your spec_helper and your spec folder.
Open .rspec
and delete ‘–warnings’.
Create a file spec/my_gem_spec.rb
and place the following inside:
1 require 'spec_helper'
2
3 describe MyGem do
4 it 'queries a website and returns the title' do
5 google = MyGem.new("http://www.google.com")
6 expect(google.title).to eq("Google")
7 end
8 end
Open my_gem.gemspec
and change line 8 to be:
1 spec.version = MyGemVersion::VERSION
Open lib/my_gem/version.rb and change line 1 to be:
1 module MyGemVersion
Open lib/my_gem.rb and paste the following in:
require “my_gem/version”
1 class MyGem
2 def initialize(url)
3 @url = url
4 end
5 end
Now, run rspec
in terminal and you should get the following error:
undefined method 'title' for #<MyGem:0x007f83826c5ab8 @url="http://www.google.com">
Great, so now we need to actually create the method that queries the page and returns the title:
Update lib/my_gem.rb to be:
1 require "my_gem/version"
2 require 'nokogiri'
3 require 'open-uri'
4
5 class MyGem
6 def initialize(url)
7 @url = Nokogiri::HTML(open(url))
8 end
9
10 def title
11 @url.title
12 end
13 end
And run rspec
again and your tests should be passing.
Now, let’s add VCR so that we don’t have to hit google.com everytime we run our specs.
Open spec/spec_helper.rb
and paste in the following:
1 require 'my_gem'
2 require 'vcr'
3 require 'webmock'
4
5 RSpec.configure do |config|
6 VCR.configure do |c|
7 c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
8 c.hook_into :webmock # or :fakeweb
9 end
10 end
Open spec/my_gem_spec.rb
and paste in the following:
1 require 'spec_helper'
2
3 describe MyGem do
4 it 'queries a website and returns the title' do
5 VCR.use_cassette('/lib/my_gem/google') do
6 google = MyGem.new("http://www.google.com")
7 expect(google.title).to eq("Google")
8 end
9 end
10 end
Run rspec
again, it should be green.
There are many other fun things you can do with this gem, such as:
- return the body content
- return all of the links on the page
- get the metadata
- basically any information that is on the page
Releasing the gem
At this point, you may want to release the gem (sending it to RubyGems.org).
rake build
- builds the gemrake install
- installs it on your systemrake release
- sends it to RubyGems.org for others to use
If you would like to see my final code, you can view it on GitHub here: my_gem
blog comments powered by Disqus