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 ⇒ Object
readonly
Returns the value of attribute token.
-
#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.
-
#contacts ⇒ Object
creates and/or returns the Flickr::Contacts object.
-
#initialize(config_param, options_param = {}) ⇒ 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.
-
#photosets ⇒ 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 flickr REST api.
-
#sign_request(options, authorize = true) ⇒ Object
alters your api parameters to include a signiture and authorization token.
-
#test ⇒ Object
creates and/or returns the Flickr::Test object.
-
#uploader ⇒ Object
creates and/or returns the Flickr::Uploader object.
-
#urls ⇒ Object
creates and/or returns the Flickr::Urls object.
Constructor Details
#initialize(config_param, options_param = {}) ⇒ Base
create a new flickr object
You can either pass a hash with the following attributes:
-
:key (Required)
the API key
-
:secret (Required)
the API secret
-
:token (Optional)
Flickr::Auth::Token object
or:
-
config_file (Required)
yaml file to load configuration from
-
options (Optional)
hash containing any of the two options * token_cache location of the token cache file. This will override the setting in the config file * environment section in the config file that flickr_fu should look for the API key and secret Useful when using with Rails
Config Example (yaml file)
key: YOUR_API_KEY
secret: YOUR_API_SECRET
token_cache: token.yml
Example config file with two environments:
development:
key: YOUR_DEVELOPMENT_API_KEY
secret: YOUR_DEVELOPMENT_API_SECRET
production:
key: YOUR_PRODUCTION_API_KEY
secret: YOUR_PRODUCTION_API_SECRET
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/flickr/base.rb', line 50 def initialize(config_param, = {}) if .is_a? String = {:token_cache => } else = end if config_param.is_a? String config = YAML.load_file(config_param) config = config[[:environment]] if .has_key? :environment else config = config_param end @api_key = config[:key] || config["key"] @api_secret = config[:secret] || config["secret"] @token_cache = [:token_cache] || config["token_cache"] @token = config[:token] || [:token] raise 'config file must contain an api key and secret' unless @api_key and @api_secret raise 'you cannot specify both the token and token_cache' if @token and @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 ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/flickr/base.rb', line 7 def token @token 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
126 |
# File 'lib/flickr/base.rb', line 126 def auth() @auth ||= Flickr::Auth.new(self) end |
#contacts ⇒ Object
creates and/or returns the Flickr::Contacts object
132 |
# File 'lib/flickr/base.rb', line 132 def contacts() @contacts ||= Flickr::Contacts.new(self) end |
#people ⇒ Object
creates and/or returns the Flickr::People object
123 |
# File 'lib/flickr/base.rb', line 123 def people() @people ||= Flickr::People.new(self) end |
#photos ⇒ Object
creates and/or returns the Flickr::Photos object
117 |
# File 'lib/flickr/base.rb', line 117 def photos() @photos ||= Flickr::Photos.new(self) end |
#photosets ⇒ Object
creates and/or returns the Flickr::Photos object
120 |
# File 'lib/flickr/base.rb', line 120 def photosets() @photosets ||= Flickr::Photosets.new(self) end |
#send_request(method, options = {}, http_method = :get, endpoint = REST_ENDPOINT) ⇒ Object
sends a request to the flickr 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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/flickr/base.rb', line 84 def send_request(method, = {}, http_method = :get, endpoint = REST_ENDPOINT) .merge!(:api_key => @api_key, :method => method) sign_request() rsp = request_over_http(, http_method, endpoint) rsp = '<rsp stat="ok"></rsp>' if rsp == "" xm = XmlMagic.new(rsp) if xm[:stat] == 'ok' xm else raise Flickr::Errors.error_for(xm.err[:code].to_i, 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)
107 108 109 110 111 |
# File 'lib/flickr/base.rb', line 107 def sign_request(, = true) .merge!(:auth_token => self.auth.token(false).to_s, :api_key => @api_key) if and self.auth.token(false) .delete(:api_sig) .merge!(:api_sig => Digest::MD5.hexdigest(@api_secret + .to_a.sort_by{|k| k[0].to_s}.flatten.join)) if @api_secret end |
#test ⇒ Object
creates and/or returns the Flickr::Test object
114 |
# File 'lib/flickr/base.rb', line 114 def test() @test ||= Flickr::Test.new(self) end |