Class: Flickr
- Inherits:
-
Object
- Object
- Flickr
- Defined in:
- lib/flickr.rb
Overview
Flickr client class. Requires an API key
Defined Under Namespace
Classes: Group, Photo, PhotoCollection, Photoset, User
Constant Summary collapse
- HOST =
'api.flickr.com'- HOST_URL =
'http://' + HOST
- API_PATH =
'/services/rest'- VALID_SIZES =
Flickr, annoyingly, uses a number of representations to specify the size of a photo, depending on the context. It gives a label such a “Small” or “Medium” to a size of photo, when returning all possible sizes. However, when generating the uri for the page that features that size of photo, or the source url for the image itself it uses a single letter. Bizarrely, these letters are different depending on whether you want the Flickr page for the photo or the source uri – e.g. a “Small” photo (240 pixels on its longest side) may be viewed at “www.flickr.com/photos/sco/2397458775/sizes/s/” but its source is at “
”. The VALID_SIZES hash associates the correct letter with a label { "Square" => ["s", "sq"], "Thumbnail" => ["t", "t"], "Small" => ["m", "s"], "Medium" => [nil, "m"], "Large" => ["b", "l"] }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#find_by_url(url) ⇒ Object
Implements flickr.urls.lookupGroup and flickr.urls.lookupUser.
-
#get_token_from(frob) ⇒ Object
Gets authentication token given a Flickr frob, which is returned when user allows access to their account for the application with the api_key which made the request.
-
#groups(group_name, options = {}) ⇒ Object
Implements flickr.groups.search.
-
#http_get(url) ⇒ Object
Does an HTTP GET on a given URL and returns the response body.
-
#initialize(api_key_or_params = nil, email = nil, password = nil, shared_secret = nil) ⇒ Flickr
constructor
To use the Flickr API you need an api key (see www.flickr.com/services/api/misc.api_keys.html), and the flickr client object shuld be initialized with this.
-
#licenses ⇒ Object
Implements flickr.photos.licenses.getInfo.
-
#login(email = '', password = '') ⇒ Object
Stores authentication credentials to use on all subsequent calls.
-
#login_url(perms, extra = nil) ⇒ Object
Returns url for user to login in to Flickr to authenticate app for a user.
-
#method_missing(method_id, params = {}) ⇒ Object
Implements everything else.
-
#photos(*criteria) ⇒ Object
Implements flickr.photos.getRecent and flickr.photos.search.
-
#photos_request(method, params = {}) ⇒ Object
acts like request but returns a PhotoCollection (a list of Photo objects).
- #photos_search(params = {}) ⇒ Object (also: #search)
- #photoset(photoset_id) ⇒ Object
-
#recent ⇒ Object
flickr.photos.getRecent 100 newest photos from everyone.
-
#related_tags(tag) ⇒ Object
Implements flickr.tags.getRelated.
-
#request(method, params = {}) ⇒ Object
Takes a Flickr API method name and set of parameters; returns an XmlSimple object with the response.
-
#request_url(method, params = {}) ⇒ Object
Builds url for Flickr API REST request from given the flickr method name (exclusing the ‘flickr.’ that begins each method call) and params (where applicable) which should be supplied as a Hash (e.g ‘user_id’ => “foo123”).
- #signature_from(params = {}) ⇒ Object
-
#tag(tag) ⇒ Object
Gets public photos with a given tag.
-
#users(lookup = nil) ⇒ Object
Implements flickr.people.findByEmail and flickr.people.findByUsername.
Constructor Details
#initialize(api_key_or_params = nil, email = nil, password = nil, shared_secret = nil) ⇒ Flickr
To use the Flickr API you need an api key (see www.flickr.com/services/api/misc.api_keys.html), and the flickr client object shuld be initialized with this. You’ll also need a shared secret code if you want to use authentication (e.g. to get a user’s private photos) There are two ways to initialize the Flickr client. The preferred way is with a hash of params, e.g. ‘api_key’ => ‘your_api_key’, ‘shared_secret’ => ‘shared_secret_code’. The older (deprecated) way is to pass an ordered series of arguments. This is provided for continuity only, as several of the arguments are no longer usable (‘email’, ‘password’)
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/flickr.rb', line 79 def initialize(api_key_or_params=nil, email=nil, password=nil, shared_secret=nil) @host = HOST_URL @api = API_PATH if api_key_or_params.is_a?(Hash) @api_key = api_key_or_params['api_key'] @shared_secret = api_key_or_params['shared_secret'] @auth_token = api_key_or_params['auth_token'] else @api_key = api_key_or_params @shared_secret = shared_secret login(email, password) if email and password end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, params = {}) ⇒ Object
Implements everything else. Any method not defined explicitly will be passed on to the Flickr API, and return an XmlSimple document. For example, Flickr#test_echo is not defined, so it will pass the call to the flickr.test.echo method.
187 188 189 |
# File 'lib/flickr.rb', line 187 def method_missing(method_id, params={}) request(method_id.id2name.gsub(/_/, '.'), params) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
43 44 45 |
# File 'lib/flickr.rb', line 43 def api_key @api_key end |
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
43 44 45 |
# File 'lib/flickr.rb', line 43 def auth_token @auth_token end |
#user ⇒ Object
Returns the value of attribute user.
44 45 46 |
# File 'lib/flickr.rb', line 44 def user @user end |
Instance Method Details
#find_by_url(url) ⇒ Object
Implements flickr.urls.lookupGroup and flickr.urls.lookupUser
117 118 119 120 |
# File 'lib/flickr.rb', line 117 def find_by_url(url) response = urls_lookupUser('url'=>url) rescue urls_lookupGroup('url'=>url) rescue nil (response['user']) ? User.new(response['user']['id'], nil, nil, nil, @api_key) : Group.new(response['group']['id'], @api_key) unless response.nil? end |
#get_token_from(frob) ⇒ Object
Gets authentication token given a Flickr frob, which is returned when user allows access to their account for the application with the api_key which made the request
96 97 98 99 100 101 102 103 104 |
# File 'lib/flickr.rb', line 96 def get_token_from(frob) auth_response = request("auth.getToken", :frob => frob)['auth'] @auth_token = auth_response['token'] @user = User.new( 'id' => auth_response['user']['nsid'], 'username' => auth_response['user']['username'], 'name' => auth_response['user']['fullname'], 'client' => self) @auth_token end |
#groups(group_name, options = {}) ⇒ Object
Implements flickr.groups.search
150 151 152 153 154 155 156 157 158 |
# File 'lib/flickr.rb', line 150 def groups(group_name, ={}) collection = groups_search({"text" => group_name}.merge())['groups']['group'] collection = [collection] if collection.is_a? Hash collection.collect { |group| Group.new( "id" => group['nsid'], "name" => group['name'], "eighteenplus" => group['eighteenplus'], "client" => self) } end |
#http_get(url) ⇒ Object
Does an HTTP GET on a given URL and returns the response body
192 193 194 |
# File 'lib/flickr.rb', line 192 def http_get(url) Net::HTTP.get_response(URI.parse(url)).body.to_s end |
#licenses ⇒ Object
Implements flickr.photos.licenses.getInfo
170 171 172 |
# File 'lib/flickr.rb', line 170 def licenses photos_licenses_getInfo['licenses']['license'] end |
#login(email = '', password = '') ⇒ Object
Stores authentication credentials to use on all subsequent calls. If authentication succeeds, returns a User object. NB This call is no longer in API and will result in an error if called
109 110 111 112 113 114 |
# File 'lib/flickr.rb', line 109 def login(email='', password='') @email = email @password = password user = request('test.login')['user'] rescue fail @user = User.new(user['id'], nil, nil, nil, @api_key) end |
#login_url(perms, extra = nil) ⇒ Object
Returns url for user to login in to Flickr to authenticate app for a user
175 176 177 178 179 180 181 |
# File 'lib/flickr.rb', line 175 def login_url(perms, extra = nil) if extra "http://flickr.com/services/auth/?api_key=#{@api_key}&perms=#{perms}&extra=#{extra}&api_sig=#{signature_from('api_key'=>@api_key, 'perms' => perms, 'extra' => extra)}" else "http://flickr.com/services/auth/?api_key=#{@api_key}&perms=#{perms}&api_sig=#{signature_from('api_key'=>@api_key, 'perms' => perms)}" end end |
#photos(*criteria) ⇒ Object
Implements flickr.photos.getRecent and flickr.photos.search
123 124 125 |
# File 'lib/flickr.rb', line 123 def photos(*criteria) criteria ? photos_search(*criteria) : recent end |
#photos_request(method, params = {}) ⇒ Object
acts like request but returns a PhotoCollection (a list of Photo objects)
205 206 207 208 |
# File 'lib/flickr.rb', line 205 def photos_request(method, params={}) photos = request(method, params) PhotoCollection.new(photos, @api_key) end |
#photos_search(params = {}) ⇒ Object Also known as: search
133 134 135 |
# File 'lib/flickr.rb', line 133 def photos_search(params={}) photos_request('photos.search', params) end |
#photoset(photoset_id) ⇒ Object
160 161 162 |
# File 'lib/flickr.rb', line 160 def photoset(photoset_id) Photoset.new(photoset_id, @api_key) end |
#recent ⇒ Object
flickr.photos.getRecent 100 newest photos from everyone
129 130 131 |
# File 'lib/flickr.rb', line 129 def recent photos_request('photos.getRecent') end |
#related_tags(tag) ⇒ Object
Implements flickr.tags.getRelated
165 166 167 |
# File 'lib/flickr.rb', line 165 def (tag) ('tag'=>tag)['tags']['tag'] end |
#request(method, params = {}) ⇒ Object
Takes a Flickr API method name and set of parameters; returns an XmlSimple object with the response
197 198 199 200 201 202 |
# File 'lib/flickr.rb', line 197 def request(method, params={}) url = request_url(method, params) response = XmlSimple.xml_in(http_get(url), { 'ForceArray' => false }) raise response['err']['msg'] if response['stat'] != 'ok' response end |
#request_url(method, params = {}) ⇒ Object
Builds url for Flickr API REST request from given the flickr method name (exclusing the ‘flickr.’ that begins each method call) and params (where applicable) which should be supplied as a Hash (e.g ‘user_id’ => “foo123”)
213 214 215 216 217 218 219 220 |
# File 'lib/flickr.rb', line 213 def request_url(method, params={}) method = 'flickr.' + method url = "#{@host}#{@api}/?api_key=#{@api_key}&method=#{method}" params.merge!('api_key' => @api_key, 'method' => method, 'auth_token' => @auth_token) signature = signature_from(params) url = "#{@host}#{@api}/?" + params.merge('api_sig' => signature).collect { |k,v| "#{k}=" + CGI::escape(v.to_s) unless v.nil? }.compact.join("&") end |
#signature_from(params = {}) ⇒ Object
222 223 224 225 226 |
# File 'lib/flickr.rb', line 222 def signature_from(params={}) return unless @shared_secret # don't both getting signature if no shared_secret request_str = params.reject {|k,v| v.nil?}.collect {|p| "#{p[0].to_s}#{p[1]}"}.sort.join # build key value pairs, sort in alpha order then join them, ignoring those with nil value return Digest::MD5.hexdigest("#{@shared_secret}#{request_str}") end |
#tag(tag) ⇒ Object
Gets public photos with a given tag
139 140 141 |
# File 'lib/flickr.rb', line 139 def tag(tag) photos('tags'=>tag) end |
#users(lookup = nil) ⇒ Object
Implements flickr.people.findByEmail and flickr.people.findByUsername.
144 145 146 147 |
# File 'lib/flickr.rb', line 144 def users(lookup=nil) user = people_findByEmail('find_email'=>lookup)['user'] rescue people_findByUsername('username'=>lookup)['user'] return User.new("id" => user["nsid"], "username" => user["username"], "client" => self) end |