Raghu
Joined: 12 Mar 2009 Posts: 34
|
Posted: Tue Jul 28, 2009 10:40 am Post subject: Integrating flickr API in ruby on rails application |
|
|
Hi Friends,
Recently I worked on, integrating Flickr API by using rails application.
For this,
1) If you have flickr gem you have to uninstall by using the following command
>gem uninstall flickr
2) Install the rflickr by following command
>gem install rflickr
(You may need to login to Flickr first.)
You must have an API key to make use of the Flickr API.
You can get api key below url
url: http://www.flickr.com/services/api/misc.api_keys.html
• Click on Apply for your api key
• Click on Apply for a Non-Commercial API Key
Once submitting all required information you will get the following API key and Secret
Sample
Key:
5c211baf9bd705496b21522132c80148
Secret:
bb6500467b1af07f
Click on - Edit key details and select the Authentication Type: Web Application.
Once all the settings have been completed its time to create and rails application.
Next you should also find your Flickr ID and enter that in the config file too.
You can get Flickr ID using below url
http://idgettr.com/
sample
ID: 21879932@N02
For example,
Create a new rails demo application
>ruby script/generate controller flickr
>ruby script/generate model my_flickr
Add the following code in your controller flickr.rb
class FlickrController < ApplicationController
layout 'main'
def index
get_and_render
end
def show
get_photos
end
def scroll_right
@session[:flickr_page] += 1
get_and_render
end
def scroll_left
if @session[:flickr_page] > 1
@session[:flickr_page] -= 1
get_and_render
end
end
def toggle_display
@session[:flickr_display] = !@session[:flickr_display]
get_and_render
end
def get_and_render
get_photos
@flickr_display=session[:flickr_display]
@flickr_page = 1
@flickr_per_page = 6
render :partial => 'flickr_photo_bar', :no_layout => true
end
private
def get_photos
session[:flickr_page] = 1
session[:flickr_per_page] = 6
session[:flickr_display] = true
if session[:flickr_display]
@my_flickr ||= MyFlickr.new
@photos = @my_flickr.my_search(MY_CONFIG[:flickr_id], session[:flickr_per_page], session[:flickr_page])
end
end
end
add the following code in flickr.helper
module FlickrHelper
require 'flickr'
def flickr_link(photo)
if MY_CONFIG[:rflickr_lib]
"http://www.flickr.com/photos/#{photo.owner_id}/#{photo.id}"
else
"http://www.flickr.com/photos/#{photo.owner.id}/#{photo.id}"
end
end
def get_url(photo, size)
if MY_CONFIG[:rflickr_lib]
photo.url(size)
else
begin
photo.source.gsub(/\....$/,"_#{size}.jpg")
rescue
'error'
end
end
end
def textile_flickr_link(photo, size_ch = 's')
'!' + get_url(photo, size_ch) + '(' + photo.title + ')!:' + flickr_link(photo)
end
PHOTO_PREVIEW_SIZE = 's' if not defined? PHOTO_PREVIEW_SIZE
PHOTO_INSERT_SIZE = 't' if not defined? PHOTO_INSERT_SIZE
def photo_image_tag(photo)
image_tag(get_url(photo, PHOTO_PREVIEW_SIZE),
:alt => photo.title,
:title => photo.title,
:id => textile_flickr_link(photo, PHOTO_INSERT_SIZE),
:class => 'flickr_photo_square')
end
def flickr_photo_tag(photo)
link_to(photo_image_tag(photo), flickr_link(photo))
end
def flickr_photo_draggable_tag(photo)
photo_image_tag(photo) +
draggable_element(textile_flickr_link(photo, PHOTO_INSERT_SIZE), :revert => true)
end
end
add the following code in your model i.e., my_flickr.rb
require 'flickr'
class MyFlickr < Flickr
def initialize
if MY_CONFIG[:rflickr_lib]
if MY_CONFIG[:flickr_cache_file]
super(MY_CONFIG[:flickr_cache_file], MY_CONFIG[:flickr_key],MY_CONFIG[:flickr_shared_secret])
else
super(nil, MY_CONFIG[:flickr_key])
auth_mode = false
end
else
super
end
end
def my_search(user_id='29281775@N00', per_page = 10, page = 1)
tags=nil
tag_mode=nil
text=nil
min_upload_date=nil
max_upload_date=nil
min_taken_date=nil
max_taken_date=nil
license=nil
extras=nil
sort=nil
if MY_CONFIG[:rflickr_lib]
photos.search(user_id,tags,tag_mode,text,min_upload_date,
max_upload_date,min_taken_date,max_taken_date,
license,extras,per_page,page,sort)
else
people_getPublicPhotos(:user_id => user_id, :per_page => per_page.to_s, :page => page.to_s)['photos']['photo'].collect { |photo| Photo.new(photo['id']) }
end
end
end
add the following rhtml file in \app\views\flickr\ _flickr_photo_bar.rhtml
<div id='flickr_toggle_display_<%=@session[:flickr_display] ? "on" : "off"%>'>
<p>
<%= link_to_remote((@session[:flickr_display] ? "[Hide" : "[Display") + " Flickr Pictures]",
:update => "flickr_photo_bar",
:url => { :controller => "flickr", :action => "toggle_display" } ) %>
</p>
</div>
<% if @session[:flickr_display] %>
<% if @session[:flickr_page] > 1 %>
<%= link_to_remote(image_tag('topSliderPageLeft.gif', :class => 'flickr_slider'),
:update => "flickr_photo_bar",
:url => { :controller => "flickr", :action => "scroll_left" } ) %>
<% else %>
<%= image_tag('topSliderPageLeftDisabled.gif', :class => 'flickr_slider') %>
<% end %>
<div id="flickr_photos">
<div>
<% if @photos.nil? %>
<p>There was an error retrieving your photos from Flickr.</p>
<% else %>
<% for photo in @photos %>
<%= flickr_photo_tag(photo) %>
<% end %>
<% end %>
</div>
</div>
<%= link_to_remote(image_tag('topSliderPageRight.gif', :class => 'flickr_slider'),
:update => "flickr_photo_bar",
:url => { :controller => "flickr", :action => "scroll_right" } ) %>
<!-- :loading => visual_effect(:slide_right, 'photos', :duration => 2)) %-->
<% end %>
add the following rhtml file in \app\views\flickr\show.rhtml
<div id=flickr_photo_bar>
<%= render :partial => 'flickr_photo_bar' %>
</div>
Add the following rhtml fild in \app\views\layout\main.rhtml
<html>
<head>
<title>Mashup Camp Rails Demo</title>
<%= stylesheet_link_tag "styles.css", :media => "all" %>
<%= javascript_include_tag :defaults %>
</head>
<% if @body_tag %>
<body <%= @body_tag %>>
<% else %>
<body>
<% end %>
<%= @content_for_layout %>
</body>
</html>
Add this to the bottom of app/config/environment.rb
MY_CONFIG = {
:flickr_cache_file => "#{RAILS_ROOT}/config/flickr.cache",
:flickr_key => "your api key",
:flickr_shared_secret => "your secret key",
:flickr_id => "your flickr_id",
:rflickr_lib => true
}
Now we need to read, write, delete permission from flickr services.
So we find for flickr token
Generating a Flickr Token
Before you can use the Flickr API key, you must generate a token. We will use Ruby interactively, through the Ruby console.
>ruby script\console
>f = MyFlickr.new
>f.auth.getFrob – its gives the frob key
You can put the file flickr.cache in config
>u = f.auth.login_link – it will generate the url with api_sig, api_key
Sample
"http://flickr.com/services/auth/?api_sig=3a031ccb711d58cc4fc1cd26576887cb&frob=3462763-e6bbacaeb35c9968&perms=write&
api_key=c1dd096037d33c250fa4dbe9d3a8f2a3"
so you will need to paste url and get authentication permissions from flickr service. Then go back to commond prompt.
>f.auth.getToken
>f.auth.cache_token
And now start your server, and try it:
http://localhost:3000/flickr |
|