Class: Flickr::Base
Direct Known Subclasses
Constant Summary collapse
- REST_ENDPOINT =
'http://api.flickr.com/services/rest/'
- AUTH_ENDPOINT =
'http://flickr.com/services/auth/'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#token_cache ⇒ Object
readonly
Returns the value of attribute token_cache.
Instance Method Summary collapse
- #auth ⇒ Object
-
#initialize(api_key, api_secret = nil, token_cache = nil) ⇒ Base
constructor
create a new flickr object.
- #photos ⇒ Object
-
#send_request(method, options = {}, http_method = :get) ⇒ Object
sends a request to the flcikr REST api.
- #sign_request(options, authorize = true) ⇒ Object
Constructor Details
#initialize(api_key, api_secret = nil, token_cache = nil) ⇒ Base
create a new flickr object
Params
-
api_key (Required)
The api key given to you by flickr.
-
api_secret (Optional)
The api secret given to you by flickr. This is used to generate a signiture for signed request.
-
token_cache (Optional)
File path to a cache file that holds a flickr token.
22 23 24 25 26 |
# File 'lib/flickr/base.rb', line 22 def initialize(api_key, api_secret = nil, token_cache = nil) @api_key = api_key @api_secret = api_secret @token_cache = token_cache end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/flickr/base.rb', line 7 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
7 8 9 |
# File 'lib/flickr/base.rb', line 7 def api_secret @api_secret end |
#token_cache ⇒ Object (readonly)
Returns the value of attribute token_cache.
7 8 9 |
# File 'lib/flickr/base.rb', line 7 def token_cache @token_cache end |
Instance Method Details
#photos ⇒ Object
65 |
# File 'lib/flickr/base.rb', line 65 def photos() @photos ||= Photos.new(self) end |
#send_request(method, options = {}, http_method = :get) ⇒ Object
sends a request to the flcikr REST api
Params
-
method (Required)
name of the flickr method (ex. flickr.photos.search)
-
options (Optional)
hash of query parameters, you do not need to include api_key, api_sig or auth_token because these are added automatically
-
http_method (Optional)
choose between a GET and POST http request. Valid options are: :get (DEFAULT) :post
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/flickr/base.rb', line 40 def send_request(method, = {}, http_method = :get) .merge!(:api_key => @api_key, :method => method) sign_request() if http_method == :get api_call = REST_ENDPOINT + "?" + .collect{|k,v| "#{k}=#{v}"}.join('&') rsp = Net::HTTP.get(URI.parse(api_call)) else rsp = Net::HTTP.post_form(URI.parse(REST_ENDPOINT), ).body end xm = XmlMagic.new(rsp) if xm[:stat] == 'ok' xm else raise "#{xm.err[:code]}: #{xm.err[:msg]}" end end |
#sign_request(options, authorize = true) ⇒ Object
60 61 62 63 |
# File 'lib/flickr/base.rb', line 60 def sign_request(, = true) .merge!(:auth_token => self.auth.token(false)) if and self.auth.token(false) .merge!(:api_sig => Digest::MD5.hexdigest(@api_secret + .keys.sort_by{|k| k.to_s}.collect{|k| k.to_s + [k].to_s}.join)) if @api_secret end |