I said it was coming and here it is, my little tutorial on how to use the rFlickr Ruby on Rails gem to create a photograph section like the one on my own website. The first thing to note is that pretty much all of the options available in the Flickr API (here) are available for use in rFlickr due to the fact it is all based around XML. There is a laborious process of configuration to go through, however, to make everything work, but once this is done you should have no problems.
Firstly install the rFlickr gem, I should at this juncture note the fact I am primarily a UNIX user so will aim these instructions at other UNIX users, mainly because I don’t know the specifics for Rails installations on Windows. So lets dive in (‘$’ denotes the terminal prompt and ‘\’ denotes line continues below):
The second thing you will need to do is make sure you have a Flickr account with some photos on it then pay a visit to http://www.flickr.com/services/api/keys/ and sign yourself up for an API key, once you have generated the key make a note of the key itself and the ‘secret’ that you are given, you will be needing these quite a bit.
The next thing to do is to basically follow the tutorial here, albeit with a few modifications, I have re-written the tutorial in full below.
To make differences clear the Rails console prompt will be shown as ‘>>’, don’t forget to replace the x’s with your information.
The above just sets up your rFlickr object and makes sure that you don’t already have a key.
This returns a value that you will need to save somewhere.
Click or copy the link you are given into a browser and authorize the API for usage, don’t worry, we’re almost there.
Right, this is as far as the tutorial online goes, but there are some other useful steps we need to take to make everything more useable, mainly the moving of the token as the ‘/tmp’ directory may get cleared by our host.
Now we can get onto the actual programming and leave the authentication business behind.
We’re going to need a controller to use, for the purposes of this tutorial I will use a controller named ‘Photography’, it should save me some time as that’s what mine is called, the page to be rendered will be called ‘view’.
In the file ‘photography_controller.rb’ we will need the following information, rename as necessary to your application.
Then in the ‘view.rhtml’ that you will have created in your ‘views’ folder (or at least, are creating now) paste the following code.
And that’s it, your basically done, all the thumbnails will link directly to your Flickr page, easy wasn’t it? The main problem occurs when you load your newly created page, it’s very, very slow, due to the speed of the Flickr API (I think). In order to improve the situation I would recommend using either page or fragment caching, but that will be covered in a future tutorial.
Hopefully this will have given you a few pointers in using the rFlickr gem, read through the Flickr API for more inspiration if you are feeling adventurous. Check back soon.
Update: If you are having problems with Rails 2.0, take a look at this fix.
Update: I have written a new tutorial on caching your photos page, that should speed it up a lot, assuming you are having problems.