Class: Contacts::Flickr
- Inherits:
-
Object
- Object
- Contacts::Flickr
- Defined in:
- lib/contacts/flickr.rb
Constant Summary collapse
- DOMAIN =
'api.flickr.com'- ServicesPath =
'/services/rest/'
Class Method Summary collapse
- .authentication_url(key, secret) ⇒ Object
- .authentication_url_for_frob(frob, key, secret) ⇒ Object
- .frob_from_response(response) ⇒ Object
- .frob_url(key, secret) ⇒ Object
-
.get_signature(params, secret) ⇒ Object
Get the MD5 digest of the string to sign.
- .get_token_from_frob(key, secret, frob) ⇒ Object
- .http_start(ssl = false) ⇒ Object
- .inspect_response(response, out = $stderr) ⇒ Object
- .query_string(params) ⇒ Object
-
.sort_params(params) ⇒ Object
Use the key-sorted version of the parameters to construct a string, to which the secret is prepended.
- .string_to_sign(params, secret) ⇒ Object
- .token_url(key, secret, frob) ⇒ Object
- .url_for(params) ⇒ Object
Class Method Details
.authentication_url(key, secret) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/contacts/flickr.rb', line 32 def self.authentication_url(key, secret) response = http_start do |flickr| flickr.get(frob_url(key, secret)) end authentication_url_for_frob(frob_from_response(response), key, secret) end |
.authentication_url_for_frob(frob, key, secret) ⇒ Object
27 28 29 30 |
# File 'lib/contacts/flickr.rb', line 27 def self.authentication_url_for_frob(frob, key, secret) params = { :api_key => key, :secret => secret, :perms => 'read', :frob => frob } 'http://www.flickr.com/services/auth/?' + query_string(params) end |
.frob_from_response(response) ⇒ Object
22 23 24 25 |
# File 'lib/contacts/flickr.rb', line 22 def self.frob_from_response(response) doc = Hpricot::XML response.body doc.at('frob').inner_text end |
.frob_url(key, secret) ⇒ Object
18 19 20 |
# File 'lib/contacts/flickr.rb', line 18 def self.frob_url(key, secret) url_for(:api_key => key, :secret => secret, :method => 'flickr.auth.getFrob') end |
.get_signature(params, secret) ⇒ Object
Get the MD5 digest of the string to sign
70 71 72 |
# File 'lib/contacts/flickr.rb', line 70 def self.get_signature(params, secret) ::Digest::MD5.hexdigest(string_to_sign(params, secret)) end |
.get_token_from_frob(key, secret, frob) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/contacts/flickr.rb', line 44 def self.get_token_from_frob(key, secret, frob) response = http_start do |flickr| flickr.get(token_url(key, secret, frob)) end doc = Hpricot::XML response.body doc.at('token').inner_text end |
.http_start(ssl = false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/contacts/flickr.rb', line 88 def self.http_start(ssl = false) port = ssl ? Net::HTTP::https_default_port : Net::HTTP::http_default_port http = Net::HTTP.new(DOMAIN, port) redirects = 0 if ssl http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.start begin response = yield(http) loop do inspect_response(response) if Contacts::verbose? case response when Net::HTTPSuccess break response when Net::HTTPRedirection if redirects == TooManyRedirects::MAX_REDIRECTS raise TooManyRedirects.new(response) end location = URI.parse response['Location'] puts "Redirected to #{location}" response = http.get(location.path) redirects += 1 else response.error! end end ensure http.finish end end |
.inspect_response(response, out = $stderr) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/contacts/flickr.rb', line 124 def self.inspect_response(response, out = $stderr) out.puts response.inspect for name, value in response out.puts "#{name}: #{value}" end out.puts "----\n#{response.body}\n----" unless response.body.empty? end |
.query_string(params) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/contacts/flickr.rb', line 74 def self.query_string(params) secret = params.delete(:secret) params[:api_sig] = get_signature(params, secret) params.inject([]) do |arr, pair| key, value = pair arr << "#{key}=#{value}" end.join('&') end |
.sort_params(params) ⇒ Object
Use the key-sorted version of the parameters to construct a string, to which the secret is prepended.
56 57 58 59 60 |
# File 'lib/contacts/flickr.rb', line 56 def self.sort_params(params) params.sort do |a,b| a.to_s <=> b.to_s end end |
.string_to_sign(params, secret) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/contacts/flickr.rb', line 62 def self.string_to_sign(params, secret) string_to_sign = secret + sort_params(params).inject('') do |str, pair| key, value = pair str + key.to_s + value.to_s end end |
.token_url(key, secret, frob) ⇒ Object
39 40 41 42 |
# File 'lib/contacts/flickr.rb', line 39 def self.token_url(key, secret, frob) params = { :api_key => key, :secret => secret, :frob => frob, :method => 'flickr.auth.getToken' } url_for(params) end |
.url_for(params) ⇒ Object
84 85 86 |
# File 'lib/contacts/flickr.rb', line 84 def self.url_for(params) ServicesPath + '?' + query_string(params) end |