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 photography 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):
sudo gem install rflickr --include-dependencies
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.
>> require 'flickr'
>> API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
>> SHARED_SECRET = "xxxxxxxxxxxxxxxx"
>> flickr = Flickr.new("/tmp/flickr.cache", API_KEY, SHARED_SECRET)
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.
>> flickr.auth.getToken('that_frob_number_we_saved' )
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.
cp /tmp/flickr.cache /your/rails/application/config/flickr.cache
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
In the file
photography_controller.rb we will need the following information, rename as necessary to your application.
class PhotographyController < ApplicationController
API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SHARED_SECRET = "xxxxxxxxxxxxxxxx"
render :action => 'view'
flickr = Flickr.new(RAILS_ROOT + "/config/flickr.cache", API_KEY, SHARED_SECRET)
@photos = flickr.people.getPublicPhotos(flickr.people.findByUsername("YOUR_FLICKR_NAME"))
Then in the
view.rhtml that you will have created in your
views folder (or at least, are creating now) paste the following code.
<% for photo in @photos %>
<a href="<%= photo.flickr.photos.getInfo(photo.id).urls.values %>"><img src="<%= photo.url('s') %>" /></a>
<% end %>
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.