📜 ⬆️ ⬇️

Making a gem for RubyGems

We collect stones for Ruby


It often happens that you drag pieces of code from project to project. Such pieces are very convenient to make in external files, modules. For this Ruby exists in RubyGems - application manager and libraries, decorated in a single file package - gem. And it turned out to be very easy to assemble such a gem, and most importantly, to make it accessible to any machine connected to the Internet.



Raw materials


So, it all starts with the code that must be collected in the heme. I have a small wrapper class for the API of the Renoter microblogging service, recently lit up in Habré . Here it is - pastie.org/453300 As you can see, there is only one class here, of the dependencies, only the JSON library.
')
I made all of the following actions in MacOS X 10.5.6, although in other OSes there are hardly any strong differences. Also I had installed:


Getting started


First of all, go and create a git repository on GitHub`s . The point here is that we kill several birds with one stone: first, the problem of distribution of the heme is solved, it will always be available on the server gems.github.com , as username-gemname. Secondly, it is an excellent and popular platform for hosting source codes. And third, perhaps the most delicious: github is able to independently collect your gem, and recompile in case of an update. On this we will stop later. So, create a new repository, and tick the “RubyGem” item in the settings.

Now go to the file structure.
mkdir renoter
cd renoter
mkdir lib
touch README
touch renoter.gemspec

It looks like the necessary minimum. Copy the renoter.rb file containing the above code to the lib folder. I will remove the logout method from it in order to later show the gem update.


Now we will write something in the README, this file will be displayed on the project page in github.
Renoter Gem
--------------------------------

Usage:

require 'renoter'

r = Renoter.new
public_timeline = r.public_timeline # Last 20 statuses

if(r.login('user', 'password'))
r.update_status 'Hello from ruby!'
print 'OK!'
else
print 'Login failed'
end

friends_timeline = r.friends_timeline('5') # Last 5 freinds statuses


For more functionality see documentation.


Moving to the renoter.gemspec file. Actually this is one of the main files, namely, this is the configuration file on the basis of which the gem will be assembled. I just comment on my own, for additional documentation you can go to offsite.

That turned out to be a config. Note that I enabled the generation of rdoc documentation, so you need to arrange the file renoter.rb. On this, in principle, all preparations end, and it is time to check the assembly of heme. Go to the console, and give the following commands:
gem build renoter.gemspec

And if everything before this step was done correctly, we should get the assembled gem file, called renoter-0.0.1.gem. Delete the resulting gem file.

Now let's do github. Here again, everything is simple: go to the directory heme, and command:
git init
git add *
git commit -m 'Commit renoter gem, 0.0.1'
git remote add origin git@github.com: cheetah / renoter.git
git push origin master

Github asks for your password, and having received it obediently commits the entire folder. Now you can lie back in your chair, as the great operating system told us, or make a cup of coffee. It will take about 15 minutes for your gem to gather. When the gem is collected, github will notify you by e-mail and in private messages, and your gem will appear on this list . Now you can install it:
gem sources -a gems.github.com (add server, maybe you have it already added)
sudo gem install cheetah-renoter

Everything gem is installed with the generated documentation, and it can be used.

Update


Remember, I said, then that github can rebuild gems after an update? Add the logout method that I cleaned to the renoter.rb file and verify this. You also need to change the version of the gem to the next, change the parameter s.version to 0.0.2 in the renoter.gemspec file Now commit again:
git add *
git commit -m 'Commit renoter gem, 0.0.2'
git push

And again the notification comes to the post office, that the heme is reassembled, can be updated.
sudo gem update cheetah-renoter


Result


Although basically I did all this for self-study, in the end it turned out to be quite a working gem.
Source codes can be found here - github.com/cheetah/renoter

Materials on topic




Source: https://habr.com/ru/post/57829/


All Articles