Using rFlickr

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):
$ 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.
$ cd /your/rails/application $ ./script/console
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)
>> flickr.auth.token
=> nilThe above just sets up your rFlickr object and makes sure that you don’t already have a key.
>> flickr.auth.getFrobThis returns a value that you will need to save somewhere.
>> flickr.auth.login_link
=> "http://some.link.flickr.com"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')
>> flickr.auth.cache_token
>> exitRight, 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 $ rm /tmp/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 ‘view’.
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"
def index
view
render :action => 'view'
end
def view
flickr = Flickr.new(RAILS_ROOT + "/config/flickr.cache", \
API_KEY, SHARED_SECRET)
@photos = flickr.people.getPublicPhotos( \
flickr.people.findByUsername("YOUR_FLICKR_NAME"))
end
endThen in the ‘views.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.



Bram - 20 May 2008 @ 20:39
I see that the photos on the photo page have been cached. Is there any good tutorial on how to cache images? I could only find page caching, but nothing on images.
Thanks
Alex - 23 May 2008 @ 16:51
I will be adding a tutorial relating to caching a photos page in the near future. As a clue for now though, I make use of the database in the back end of the website and the sever filesystem.
Imran Anwar - 13 Sep 2009 @ 06:27
Alex
This was simply beautiful and brilliant and what a great public service.
I was having problems getting rflickr to work under LovdByLess but this was so well written I could understand what should be happening.
At the end of my doing all that is stated above the only problem I saw was that there was no tmp/flickr.cache file created so there was nothing to copy over to /config/flickr.cache so my problem is not solved but your work above most appreciated.
Imran
Alex - 20 Sep 2009 @ 15:56
The problem maybe that the ‘tmp’ directory was not writable or does not exist. You can actually substitute the ‘/tmp’ with a directory of your choice as long as you can still get the ‘flickr.cache’ file into your application from the substituted directory.