Class: Flickr::Base
Constant Summary collapse
- REST_ENDPOINT =
'http://api.flickr.com/services/rest/'
- AUTH_ENDPOINT =
'http://flickr.com/services/auth/'
- UPLOAD_ENDPOINT =
'http://api.flickr.com/services/upload/'
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
creates and/or returns the Flickr::Auth object.
-
#initialize(config_file, token_cache = nil) ⇒ Base
constructor
create a new flickr object.
-
#people ⇒ Object
creates and/or returns the Flickr::People object.
-
#photos ⇒ Object
creates and/or returns the Flickr::Photos object.
-
#send_request(method, options = {}, http_method = :get, endpoint = REST_ENDPOINT) ⇒ Object
sends a request to the flcikr REST api.
-
#sign_request(options, authorize = true) ⇒ Object
alters your api parameters to include a signiture and authorization token.
-
#uploader ⇒ Object
creates and/or returns the Flickr::Uploader object.
Constructor Details
#initialize(config_file, token_cache = nil) ⇒ Base
create a new flickr object
Params
-
config_file (Required)
yaml file to load configuration from
-
token_cache (Optional)
location of the token cache file. This will override the setting in the config file
Config Example (yaml file)
key: YOUR_API_KEY secret: YOUR_API_SECRET token_cache: token.yml
28 29 30 31 32 33 34 35 36 |
# File 'lib/flickr/base.rb', line 28 def initialize(config_file, token_cache = nil) config = YAML.load_file(config_file) @api_key = config['key'] @api_secret = config['secret'] @token_cache = token_cache || config['token_cache'] raise 'flickr config file must contain an api key and secret' unless @api_key and @api_secret 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
#auth ⇒ Object
creates and/or returns the Flickr::Auth object
93 |
# File 'lib/flickr/base.rb', line 93 def auth() @auth ||= Auth.new(self) end |
#people ⇒ Object
creates and/or returns the Flickr::People object
90 |
# File 'lib/flickr/base.rb', line 90 def people() @people ||= People.new(self) end |
#photos ⇒ Object
creates and/or returns the Flickr::Photos object
87 |
# File 'lib/flickr/base.rb', line 87 def photos() @photos ||= Photos.new(self) end |
#send_request(method, options = {}, http_method = :get, endpoint = REST_ENDPOINT) ⇒ 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
-
endpoint (Optional)
url of the api endpoint
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flickr/base.rb', line 52 def send_request(method, = {}, http_method = :get, endpoint = REST_ENDPOINT) .merge!(:api_key => @api_key, :method => method) sign_request() if http_method == :get api_call = 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
alters your api parameters to include a signiture and authorization token
Params
-
options (Required)
the hash of parameters to be passed to the send_request
-
authorize (Optional)
boolean value to determine if the call with include an auth_token (Defaults to true)
80 81 82 83 84 |
# File 'lib/flickr/base.rb', line 80 def sign_request(, = true) .merge!(:auth_token => self.auth.token(false).to_s) if and self.auth.token(false) .delete(:api_sig) .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 |