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 put into external files, modules. To do this, in Ruby there is RubyGems - an application manager and libraries designed in one file package - gem. And to collect such a gem, and most importantly, to make it available for any machine connected to the Internet, it turned out to be very simple.



    Source materials


    So, it all starts with the code that you need to collect in the gem. I have a small class is a wrapper for the API recently lit up Habré microblogging service Renoter . Here it is - pastie.org/453300 As you can see, there is only one class, only the JSON library of dependencies.

    All of the following actions I performed on MacOS X 10.5.6, although there are unlikely to be major differences in other OSs. Also I have installed:
    • Ruby 1.8.6
    • RubyGems 1.3.2
    • Git 1.6.2.2


    Getting started


    First of all, let's go and create a git repository on GitHub . The point here is that this is how we kill several birds with one stone: firstly, the problem of distributing the gem is solved, it will always be available on the server gems.github.com , like username-gemname. Secondly, it is a great and popular platform for hosting source codes. And the third, perhaps the most delicious: github can independently assemble your gem, and rebuild in case of an update. We will stop on this a bit later. So, we create a new repository, and put a tick on the item "RubyGem" in the settings.

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

    This is what the required minimum looks like. In the lib folder, copy the renoter.rb file containing the above code. I will remove the logout method from it to show the gem update later.


    Now let's write something in 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.


    Go 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 collected. I’ll just comment on my own, for additional documentation you can go to the offsite.

    Here is a config. Note that I turned on the generation of rdoc documentation, so you need to format the renoter.rb file. On this, in principle, all preparations end, and it is time to check the assembly of the heme. We go into the console and give the following commands:
    gem build renoter.gemspec

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

    Now let's do github. Here again everything is simple: we go into the gem directory, and we 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 will ask for your password, and after receiving it obediently commit the entire folder. Now you can sit back in a chair, as the great operating system told us, or make a cup of coffee. It will take about 15 minutes for your heme to collect. When the gem has collected, github will notify you by email and in private messages, and your gem will appear on this list . Now you can install it:
    gem sources -a gems.github.com (add the server, maybe you have already added it)
    sudo gem install cheetah-renoter

    That's it, the gem is installed along with the generated documentation, and it can be used.

    Update


    Remember, I said that github can rebuild the gem after the update? We add the logout method, which I removed, to the renoter.rb file, and we will verify this. Also, you need to change the version of the gem to the next one, change the s.version parameter in the renoter.gemspec file to 0.0.2 Now commit again:
    git add *
    git commit -m 'Commit renoter gem, 0.0.2'
    git push

    And again, a notification comes to the mail that the gem has been rebuilt, it can be updated.
    sudo gem update cheetah-renoter


    Result


    Although I basically did all this for self-study, in the end I got a very working gem.
    Source codes can be taken here - github.com/cheetah/renoter

    Related Materials





    Also popular now: