Class: Flickr
Overview
Flickr client class. Requires an API key
Defined Under Namespace
Classes: Photo, PhotoCollection, User
Constant Summary collapse
- HOST_URL =
'https://flickr.com'- 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
-
#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.
-
#method_missing(method_id, params = {}) ⇒ Object
Implements everything else.
-
#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
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’)
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 47 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.
66 67 68 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 66 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.
12 13 14 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 12 def api_key @api_key end |
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
12 13 14 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 12 def auth_token @auth_token end |
#user ⇒ Object
Returns the value of attribute user.
13 14 15 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 13 def user @user end |
Instance Method Details
#request(method, params = {}) ⇒ Object
Takes a Flickr API method name and set of parameters; returns an XmlSimple object with the response
71 72 73 74 75 76 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 71 def request(method, params={}) url = request_url(method, params) response = XmlSimple.xml_in(open(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”)
81 82 83 84 85 86 87 88 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 81 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
90 91 92 93 94 |
# File 'lib/acts_as_unvlogable/flickr.rb', line 90 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 |