First Ruby Experience…

So I’ve had this itch I’ve needed to scratch in the form of learning a little Ruby and writing something in that has a little more substance than just “Hello World!”. Well that time has come, and I’m pleased to say, I’ve got something! Not necessarily something that will solve world hunger, but I think it’s neat. I was inspired by some work I found over on Coder Wall, where Victor Martinez shared some nifty idea about taking a photo every time you perform a git commit. I decided to take it one step further, and have these photos uploaded to a G+ Photo Album. And here begins my Ruby experience…

Preamble

When running a ruby app/script, and you require a gem, as I did(curb and nokogiri), you need to also have “require rubygems” at the top of your ruby file to tell your app to load gems needed by your application, or else nothing will work. This is only relevant for those running ruby pre v1.9. I’m developing on my mac, so the default is v1.8.7. There are some great docs on ruby, and especially here (See section on Post Install) about dealing with ruby gems in your project.

Dependencies

So lets talk about what my project depends on in order to work.

  1. ImageSnap – Without this, it’s pretty useless. Unfortunately, it’s not all pure ruby. You’ll have to download it and install it somewhere on your machine where it’ll be universally available. There is an ImageSnap Ruby gem, but all that is, is a wrapper around calling imagesnap on the command line. If you’re interested, you can see it on Github
  2. Curb – really neat library for making HTTP requests, and dealing with responses.
  3. Nokogiri – Xml library to search and build xml docs
  4. Rubys built in OptionParser – Used to parse my ruby app command line like arguments

Configuration

Once you’ve downloaded the source, you can install the gem locally. Sorry, it’s not available publically – yet! Once you have the source downloaded, just run

gem build picasa-photo-uploader.gemspec
gem install picasa-photo-uploader

If you get an error about trying to load the picasa-photo-uploader gem, then you may very well have an issue about which ruby installation the script is trying to use. A good source on this can be found at http://docs.rubygems.org/read/chapter/19. On my mac I have the default 1.8 installed, but somehow I was actually using a different version of Ruby than I thought. Some changes to sym links fixed the issue for me 🙂

Git Commit Hook

Finally, once everything was installed, it was time to edit my git commit hook script. I gleened all I needed to know off of the git docs. My commit script looks a little like this:

#!/usr/bin/env ruby
require 'picasa-photo-uploader'

unless File.directory?(File.expand_path("../../rebase-merge", __FILE__))
    file="~/.gitshots/#{Time.now.to_i}.jpg"
    PicasaPhotoUploader.snapAndUpload "GMAIL_EMAIL_ADDRESS" "PASSWORD" "ALBUM_NAME" "#{file}"
end
exit 0

Conclusion

While learning Ruby, it was a rocky journey. I am by no means any expert, but I think I have a fair grounding in what it is capable of. One thing I seriously battled with was getting different versions of Ruby to play nice. One tool to make this easier for you is called RVM. I strongly suggest you check it out if you haven’t already. I feel like my rocky journey wasn’t helped by the fact that I had the default v1.8.7 installed on my Mac – there seems to be endless issues with using <= 1.8.7 and installing gems etc. I found the last hurdle with the git post-commit script a long and arduous battle to get working. I had endless issues with the dreaded LoadError.

Source

If you’re interested, the source can be found here.

Advertisements