A brief intro to 2 flickr gems for Rails 2.1
Posted by Vince Wadhwani on Jul 14, 2008
If you spend some time googling for Rails plugins to interface with Flickr, you'll no doubt run into rflickr. Dig a little deeper and you'll find that it hasn't been updated in a while. There is a fork of it up on Github, but unfortunately that doesn't work with Rails 2.1 either. But, there are two plugins that I have confirmed work with Rails 2.1 AND Ruby 1.8.7. At the time of this post, the documentation was a little lacking in some areas so I'll save you some time in case you decide to pursue flickr integration with your Rails app before the maintainers do some writing. Let's move on..
The first gem is simply called "Flickr" and is "An update of Scott Raymond's insanely easy flickr library". It's true it's pretty easy. You can install it as a gem or check it out directly from github and put it in your vendor/plugins folder.
Working with "flickr" is pretty straight forward and there are some examples in the docs. One thing I was looking to do was get all the photos tagged with "buyindie" and display the last 6. The steps to get going when just need to do that are pretty easy. Since we don't actually have to send data over to flickr we can skip the authorizing process. Once you've installed the gem or plugin, crack open the script/console and get working:
require 'flickr' flickr = Flickr.new('your_flickr_api_key') @photos = flickr.photos_search("tags" => 'buyindie', 'per_page' => 6, 'page' => 1)
The last line is the actual search that will return an array of photos. The syntax for searching by tags is hidden in the actual library so hopefully the above saves you some time. Once you have the @photos array you can step through and find the various attributes like @photos.first.title. One thing you will notice is that some of the properties you need aren't actually accessible. Well, that's a bummer. For example, to grab the URL of the image itself (the url can be grabbed with @photos.first.url) you need:
http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
And if you look for @photos.first.farm you'll find that you can see it in the array but that you're out of luck getting the value returned to you. Same thing for secret. As you can see from the flickr format above you'll need both of those. Crack open the flickr.lib file in the plugin, go down to about line 427 and add these snippets of code.
def farm
@farm.nil? ? getInfo.farm : @farm
end
def secret
@secret.nil? ? getInfo.secret : @secret
end
Next time you reload you'll be able to access the farm and secret that you need. Form the URL for the direct link to the photo with code like so.
<%= image_tag("http://farm#{photo.farm}.static.flickr.com/#{photo.server}/#{photo.id}_#{photo.secret}_t.jpg" %>
Another choice you have is using flickr_fu. This is the one I ended up using. It's much more powerful once you get going. The documentation is much better too.
I installed the gem this time around. It's easily done:
sudo gem install flickr-fu
If you get an error make sure you have the mime-types gem installed too. As of this writing it wasn't getting pulled in properly though I understand from the developer that it's being rectified.
sudo gem install mime-types
Unlike the first plugin we talked about, you need to setup a yaml file to hold your info (api key, secret). The format of that yml file is like so:
key: YOUR_API_KEY secret: YOUR_API_SECRET token_cache: token.yml
I saved it as config/flickr.yml and then referenced it in my applications controller using:
flickr = Flickr.new("#{RAILS_ROOT}/config/flickr.yml")
With that out of the way, things become similar to the way we did things above. To fetch the last 6 photos tagged as 'buyindie' we use this code:
@photos = flickr.photos.search(:tags => 'buyindie', 'per_page' => 6, 'page' => 1)
This time all the elements we need are available so no need to modify any code in the gem. I'm still doing a lot discovering here (including deciding whether I want to actually dig in and use flickr photos on the site.. (would you be more likely to tag indie places if you knew the pic could appear on the front page?) My next step is to stop by and cache those photos so that the load times don't suffer too much. First point of reference is this blog post by Alex Pardoe. Stay tuned!
Update: 07-15-2008 Some back and forth later and I've discovered even more coolness from flickr_fu. You don't actually have to build the URL for the image, you can just use
@photos.first.url
Likewise to link back to the original page, you can just call
@photos.first.url_photopage
Do you have and example for showing your own photos? I think i’m missing something using flickr_fu. Please be as detailed as possible, thank you very much.
Felipe Cerda
The syntax would be something like: photos = flickr.photos.search(:userid => ‘72379732@N00’) where you replace the userid with whichever one you’re looking for.
Be sure to check out the docs.. it’s all in there..
vincew
Thanks, i think i know that but my problems is actually showing the picture… do i have to use a simple puts after that code?
Felipe Cerda
What about image_tag(@photos.first.url) .. that should work.. (where @photos is your array)
vince
This works much better then rflickr. Thanks for the post.
Marcus
Thanks for pointing out flickrfu. I’ve been bumbling around with flickr and rflickr for the past year and flickrfu works so much better. Great article!
Mark Nutter
I did a recent appraisal of options for accessing Flickr in Ruby, and I did have the most success with flickr-fu, as well. Several others were half baked or plain broken.
However, I ultimately ditched flickr-fu because it seemed to not support my needs: accessing sets for nicer presentation on my site. It looks like I could use flickr-fu if I had all the photos I want tagged. Did I miss something or is that accurate?
Anyway, I ended up just making a Yahoo Pipes feed combining the feeds for the sets I wanted and using that, which might be a better option anyway in terms of balancing complexity with the features I actually need.
Seth Thomas Rasmussen