i5m
February 1st, 2007, 11:32 AM
In trying to learn Ruby on Rails I've had a go at re-creating the "Putting Flickr on Rails (http://media.rubyonrails.org/video/flickr-rails-ajax.mov)" screencast, but instead of using the flickr-1.0.0 gem (http://redgreenblu.com/flickr/) I wanted to use the rflickr gem (http://rubyforge.org/projects/rflickr/) since that seems to be the favourite one to use nowadays; more upto date, feature complete, etc.
I've managed to get it to work, and although the changes required are actually minor I thought I'd post them for the benefit of other newbies (I was surprised I couldn't find the answer already to this) to avoid much head banging
Installing the gem
If you are doing this on a webhost, such as here at OCS you need to install the gem into the rails vendor directory. You can do all this via ssh if you want. In fact if you do it all via ssh I suggest using vim with the Rails plugin (http://rails.vim.tpope.net/). It's great.
cd into the vendor/plugins directory of your rails app
get the gem,
curl -O http://files.rubyforge.vm.bytemark.co.uk/rflickr/rflickr-2006.02.01.gem
unpack it
gem unpack rflickr-2006.02.01.gem
Then you can delete the .gem file
Getting a Flickr API key
The rflickr gem requires that you authorise with Flickr before you can use it. There's a fair bit of info on the web about this, especially about getting your flickr api key (I'll not go into that here):
flickr tag question on the Rails list (http://lists.rubyonrails.org/pipermail/rails/2006-May/042399.html)
Using RMagick with Flickr (http://schf.uc.org/articles/2006/10/23/using-rmagick-with-flickr)
RoR and Flickr Mashup (http://www.maxdunn.com/RoR+and+Flickr)
There's also an example in the rflickr GETTING_STARTED file in the gem you've just installed.
I couldn't get anything web-based to work so did it via irb on my OCS account.
cd into the vendor/plugins/rflickr-2006.02.01/lib directory
start irb
And do the following
require 'flickr'
#That's why the directory change is important
API_KEY = "yourFlickrapikey"
SHARED_SECRET = "yourFlickrsharedsecret"
TOKEN_CACHE = "A writeable location, on OCS something like /home/you/flickr_token_cache"
flickr = Flickr.new(TOKEN_CACHE,API_KEY,SHARED_SECRET)
flickr.auth.token
#Should return nil since not got it
flickr.auth.login_link
#Generates a link. Copy it and paste into a browser in order to authorise. It'll be something like
#http://flickr.com/services/auth/?api_sig=alongalphanumeric&frob=number-alphanumeric&perms=delete&api_key=anotherlongalphanumeric
#Once authorised, copy the bit after &frob= and put in the following
flickr.auth.getToken('number-alphanumeric')
#And then cache it
flickr.auth.cache_token
#done
Setting up rails to find the gem
In the config/environment.rb file add the following at the bottom:
require 'flickr'
Differences in the files
If you are using vim with the rails plugin and you are at the route of the rails site you are trying this out on, then if you start vim you can type things like :Rcontroller flickr" to jump straight to the flickr-Controller.rb if it exists, or ":Rgenerate controller flickr" to make it if it doesn't. You can they use ":Rview _photo" to switch to the partial (if it exists) and :Rstyle to get to the stylesheet, etc. It's really handy.
For the flickr_controller.rb, use the following
flickr = Flickr.new('the token cache location','your api key','your shared secret')
render :partial => "photo", :collection =>
flickr.photos.search(nil,params[:tags],nil,nil,nil,nil,nil,nil,nil,nil,'24')
You're not meant to have to specify your api key and shared secret now you've cached the token, but I couldn't get it to work without doing this.
The rflickr syntax seems neater to me compared to the original. Documentation is available (http://www.gemjack.com/gems/rflickr-2006.02.01/index.html). Click on Flickr::Photos and then find 'search'
For the _photo.rhtml partial, use the following
<img class="photo" src="<%= photo.url('s') %>" alt="" />
This confused me for a bit, especially since I still don't really understand what rails/ruby is doing. I couldn't understand why it's not flickr.photo.url(). I think because we've said render :partial => "photo" it also passes on the variable 'photo' to the partial as documented here under "Rendering a collection of partials (http://api.rubyonrails.com/classes/ActionView/Partials.html)" so 'photo' is a collection (list of all the searched photos) and the partial is taking each one hence why the url() method can be used directly. Something like that.
Documentation for this feature is as before. Click on Flickr::Photos and then find 'url'. Information for the sizes is available at the flickr api site (http://www.flickr.com/services/api/misc.urls.html.) . 's' is small surprisingly.
And that's it. While it's there, my copy of this is at: http://www.i5m.me.uk/newsite/flickr
I've managed to get it to work, and although the changes required are actually minor I thought I'd post them for the benefit of other newbies (I was surprised I couldn't find the answer already to this) to avoid much head banging
Installing the gem
If you are doing this on a webhost, such as here at OCS you need to install the gem into the rails vendor directory. You can do all this via ssh if you want. In fact if you do it all via ssh I suggest using vim with the Rails plugin (http://rails.vim.tpope.net/). It's great.
cd into the vendor/plugins directory of your rails app
get the gem,
curl -O http://files.rubyforge.vm.bytemark.co.uk/rflickr/rflickr-2006.02.01.gem
unpack it
gem unpack rflickr-2006.02.01.gem
Then you can delete the .gem file
Getting a Flickr API key
The rflickr gem requires that you authorise with Flickr before you can use it. There's a fair bit of info on the web about this, especially about getting your flickr api key (I'll not go into that here):
flickr tag question on the Rails list (http://lists.rubyonrails.org/pipermail/rails/2006-May/042399.html)
Using RMagick with Flickr (http://schf.uc.org/articles/2006/10/23/using-rmagick-with-flickr)
RoR and Flickr Mashup (http://www.maxdunn.com/RoR+and+Flickr)
There's also an example in the rflickr GETTING_STARTED file in the gem you've just installed.
I couldn't get anything web-based to work so did it via irb on my OCS account.
cd into the vendor/plugins/rflickr-2006.02.01/lib directory
start irb
And do the following
require 'flickr'
#That's why the directory change is important
API_KEY = "yourFlickrapikey"
SHARED_SECRET = "yourFlickrsharedsecret"
TOKEN_CACHE = "A writeable location, on OCS something like /home/you/flickr_token_cache"
flickr = Flickr.new(TOKEN_CACHE,API_KEY,SHARED_SECRET)
flickr.auth.token
#Should return nil since not got it
flickr.auth.login_link
#Generates a link. Copy it and paste into a browser in order to authorise. It'll be something like
#http://flickr.com/services/auth/?api_sig=alongalphanumeric&frob=number-alphanumeric&perms=delete&api_key=anotherlongalphanumeric
#Once authorised, copy the bit after &frob= and put in the following
flickr.auth.getToken('number-alphanumeric')
#And then cache it
flickr.auth.cache_token
#done
Setting up rails to find the gem
In the config/environment.rb file add the following at the bottom:
require 'flickr'
Differences in the files
If you are using vim with the rails plugin and you are at the route of the rails site you are trying this out on, then if you start vim you can type things like :Rcontroller flickr" to jump straight to the flickr-Controller.rb if it exists, or ":Rgenerate controller flickr" to make it if it doesn't. You can they use ":Rview _photo" to switch to the partial (if it exists) and :Rstyle to get to the stylesheet, etc. It's really handy.
For the flickr_controller.rb, use the following
flickr = Flickr.new('the token cache location','your api key','your shared secret')
render :partial => "photo", :collection =>
flickr.photos.search(nil,params[:tags],nil,nil,nil,nil,nil,nil,nil,nil,'24')
You're not meant to have to specify your api key and shared secret now you've cached the token, but I couldn't get it to work without doing this.
The rflickr syntax seems neater to me compared to the original. Documentation is available (http://www.gemjack.com/gems/rflickr-2006.02.01/index.html). Click on Flickr::Photos and then find 'search'
For the _photo.rhtml partial, use the following
<img class="photo" src="<%= photo.url('s') %>" alt="" />
This confused me for a bit, especially since I still don't really understand what rails/ruby is doing. I couldn't understand why it's not flickr.photo.url(). I think because we've said render :partial => "photo" it also passes on the variable 'photo' to the partial as documented here under "Rendering a collection of partials (http://api.rubyonrails.com/classes/ActionView/Partials.html)" so 'photo' is a collection (list of all the searched photos) and the partial is taking each one hence why the url() method can be used directly. Something like that.
Documentation for this feature is as before. Click on Flickr::Photos and then find 'url'. Information for the sizes is available at the flickr api site (http://www.flickr.com/services/api/misc.urls.html.) . 's' is small surprisingly.
And that's it. While it's there, my copy of this is at: http://www.i5m.me.uk/newsite/flickr