Class: Flickr
- Inherits:
-
Object
show all
- Defined in:
- lib/flickr.rb,
lib/flickr/util.rb,
lib/flickr/errors.rb,
lib/flickr/version.rb,
lib/flickr/response.rb,
lib/flickr/oauth_client.rb,
lib/flickr/response_list.rb
Defined Under Namespace
Modules: Request, Util
Classes: Error, FailedResponse, FlickrAppNotConfigured, OAuthClient, Response, ResponseList
Constant Summary
collapse
- USER_AGENT =
"Flickr/#{VERSION} (+https://github.com/hanklords/flickraw)".freeze
- END_POINT =
'https://api.flickr.com/services'.freeze
- FLICKR_OAUTH_REQUEST_TOKEN =
(END_POINT + '/oauth/request_token').freeze
- FLICKR_OAUTH_AUTHORIZE =
(END_POINT + '/oauth/authorize').freeze
- FLICKR_OAUTH_ACCESS_TOKEN =
(END_POINT + '/oauth/access_token').freeze
- REST_PATH =
(END_POINT + '/rest/').freeze
- UPLOAD_PATH =
(END_POINT + '/upload/').freeze
- REPLACE_PATH =
(END_POINT + '/replace/').freeze
- PHOTO_SOURCE_URL =
'https://farm%s.staticflickr.com/%s/%s_%s%s.%s'.freeze
- URL_PROFILE =
'https://www.flickr.com/people/'.freeze
- URL_PHOTOSTREAM =
'https://www.flickr.com/photos/'.freeze
- URL_SHORT =
'https://flic.kr/p/'.freeze
- BASE58_ALPHABET =
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.freeze
- VERSION =
'2.0.0'
- @@initialized =
false
- @@mutex =
Mutex.new
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(api_key = ENV['FLICKR_API_KEY'], shared_secret = ENV['FLICKR_SHARED_SECRET']) ⇒ Flickr
Returns a new instance of Flickr.
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/flickr.rb', line 37
def initialize(api_key = ENV['FLICKR_API_KEY'], shared_secret = ENV['FLICKR_SHARED_SECRET'])
raise FlickrAppNotConfigured.new("No API key defined!") if api_key.nil?
raise FlickrAppNotConfigured.new("No shared secret defined!") if shared_secret.nil?
@access_token = @access_secret = nil
@oauth_consumer = oauth_consumer api_key, shared_secret
@@mutex.synchronize do
unless @@initialized
build_classes retrieve_endpoints
@@initialized = true
end
end
@client = self end
|
Class Attribute Details
.api_key ⇒ Object
209
210
211
|
# File 'lib/flickr.rb', line 209
def api_key
@api_key
end
|
.ca_file ⇒ Object
Set path of a CA certificate file in PEM format (ssl connection only)
224
225
226
|
# File 'lib/flickr.rb', line 224
def ca_file
@ca_file
end
|
.ca_path ⇒ Object
Set path to a directory of CA certificate files in PEM format (ssl connection only)
227
228
229
|
# File 'lib/flickr.rb', line 227
def ca_path
@ca_path
end
|
.cache ⇒ Object
Set path to a file that can be used to store endpoints
230
231
232
|
# File 'lib/flickr.rb', line 230
def cache
@cache
end
|
.check_certificate ⇒ Object
Check the server certificate (ssl connection only)
221
222
223
|
# File 'lib/flickr.rb', line 221
def check_certificate
@check_certificate
end
|
.proxy ⇒ Object
215
216
217
|
# File 'lib/flickr.rb', line 215
def proxy
@proxy
end
|
.secure ⇒ Object
218
219
220
|
# File 'lib/flickr.rb', line 218
def secure
@secure
end
|
.shared_secret ⇒ Object
212
213
214
|
# File 'lib/flickr.rb', line 212
def shared_secret
@shared_secret
end
|
Instance Attribute Details
#access_secret ⇒ Object
Authenticated access token secret
30
31
32
|
# File 'lib/flickr.rb', line 30
def access_secret
@access_secret
end
|
#access_token ⇒ Object
Authenticated access token
27
28
29
|
# File 'lib/flickr.rb', line 27
def access_token
@access_token
end
|
#client ⇒ Object
Returns the value of attribute client.
32
33
34
|
# File 'lib/flickr.rb', line 32
def client
@client
end
|
Class Method Details
.base58(id) ⇒ Object
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/flickr.rb', line 234
def base58(id)
id = id.to_i
alphabet = BASE58_ALPHABET.split(//)
base = alphabet.length
begin
id, m = id.divmod(base)
r = alphabet[m] + (r || '')
end while id > 0
r
end
|
.url(r) ⇒ Object
245
|
# File 'lib/flickr.rb', line 245
def url(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '', 'jpg'] end
|
.url_b(r) ⇒ Object
249
|
# File 'lib/flickr.rb', line 249
def url_b(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_b', 'jpg'] end
|
.url_c(r) ⇒ Object
253
|
# File 'lib/flickr.rb', line 253
def url_c(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_c', 'jpg'] end
|
.url_h(r) ⇒ Object
254
|
# File 'lib/flickr.rb', line 254
def url_h(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_h', 'jpg'] end
|
.url_k(r) ⇒ Object
255
|
# File 'lib/flickr.rb', line 255
def url_k(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_k', 'jpg'] end
|
.url_m(r) ⇒ Object
246
|
# File 'lib/flickr.rb', line 246
def url_m(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_m', 'jpg'] end
|
.url_n(r) ⇒ Object
252
|
# File 'lib/flickr.rb', line 252
def url_n(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_n', 'jpg'] end
|
.url_o(r) ⇒ Object
256
|
# File 'lib/flickr.rb', line 256
def url_o(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.originalsecret, '_o', r.originalformat] end
|
.url_photopage(r) ⇒ Object
258
|
# File 'lib/flickr.rb', line 258
def url_photopage(r); url_photostream(r) + r.id end
|
.url_photoset(r) ⇒ Object
260
|
# File 'lib/flickr.rb', line 260
def url_photoset(r); url_photosets(r) + r.id end
|
.url_photosets(r) ⇒ Object
259
|
# File 'lib/flickr.rb', line 259
def url_photosets(r); url_photostream(r) + 'sets/' end
|
.url_photostream(r) ⇒ Object
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/flickr.rb', line 267
def url_photostream(r)
URL_PHOTOSTREAM +
if r.respond_to?(:pathalias) && r.pathalias
r.pathalias
elsif r.owner.respond_to?(:nsid)
r.owner.nsid
else
r.owner
end + '/'
end
|
.url_profile(r) ⇒ Object
257
|
# File 'lib/flickr.rb', line 257
def url_profile(r); URL_PROFILE + (r.owner.respond_to?(:nsid) ? r.owner.nsid : r.owner) + '/' end
|
.url_q(r) ⇒ Object
251
|
# File 'lib/flickr.rb', line 251
def url_q(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_q', 'jpg'] end
|
.url_s(r) ⇒ Object
247
|
# File 'lib/flickr.rb', line 247
def url_s(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_s', 'jpg'] end
|
.url_short(r) ⇒ Object
261
|
# File 'lib/flickr.rb', line 261
def url_short(r); URL_SHORT + base58(r.id) end
|
.url_short_m(r) ⇒ Object
262
|
# File 'lib/flickr.rb', line 262
def url_short_m(r); URL_SHORT + 'img/' + base58(r.id) + '_m.jpg' end
|
.url_short_n(r) ⇒ Object
266
|
# File 'lib/flickr.rb', line 266
def url_short_n(r); URL_SHORT + 'img/' + base58(r.id) + '_n.jpg' end
|
.url_short_q(r) ⇒ Object
265
|
# File 'lib/flickr.rb', line 265
def url_short_q(r); URL_SHORT + 'img/' + base58(r.id) + '_q.jpg' end
|
.url_short_s(r) ⇒ Object
263
|
# File 'lib/flickr.rb', line 263
def url_short_s(r); URL_SHORT + 'img/' + base58(r.id) + '.jpg' end
|
.url_short_t(r) ⇒ Object
264
|
# File 'lib/flickr.rb', line 264
def url_short_t(r); URL_SHORT + 'img/' + base58(r.id) + '_t.jpg' end
|
.url_t(r) ⇒ Object
248
|
# File 'lib/flickr.rb', line 248
def url_t(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_t', 'jpg'] end
|
.url_z(r) ⇒ Object
250
|
# File 'lib/flickr.rb', line 250
def url_z(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, '_z', 'jpg'] end
|
Instance Method Details
#call(req, args = {}, &block) ⇒ Object
This is the central method. It does the actual request to the Flickr server.
Raises FailedResponse if the response status is failed.
57
58
59
60
61
|
# File 'lib/flickr.rb', line 57
def call(req, args={}, &block)
oauth_args = args.delete(:oauth) || {}
http_response = @oauth_consumer.post_form(REST_PATH, @access_secret, {:oauth_token => @access_token}.merge(oauth_args), build_args(args, req))
process_response(req, http_response.body)
end
|
#get_access_token(token, secret, verify) ⇒ Object
Get an oauth access token.
flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], oauth_verifier)
80
81
82
83
84
|
# File 'lib/flickr.rb', line 80
def get_access_token(token, secret, verify)
access_token = @oauth_consumer.access_token(FLICKR_OAUTH_ACCESS_TOKEN, secret, :oauth_token => token, :oauth_verifier => verify)
@access_token, @access_secret = access_token['oauth_token'], access_token['oauth_token_secret']
access_token
end
|
#get_authorize_url(token, args = {}) ⇒ Object
Get the oauth authorize url.
auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
73
74
75
|
# File 'lib/flickr.rb', line 73
def get_authorize_url(token, args = {})
@oauth_consumer.authorize_url(FLICKR_OAUTH_AUTHORIZE, args.merge(:oauth_token => token))
end
|
#get_request_token(args = {}) ⇒ Object
Get an oauth request token.
token = flickr.get_request_token(:oauth_callback => "https://example.com")
66
67
68
|
# File 'lib/flickr.rb', line 66
def get_request_token(args = {})
@oauth_consumer.request_token(FLICKR_OAUTH_REQUEST_TOKEN, args)
end
|
#replace_photo(file, args = {}) ⇒ Object
100
101
102
|
# File 'lib/flickr.rb', line 100
def replace_photo(file, args={})
upload_flickr(REPLACE_PATH, file, args)
end
|
#upload_photo(file, args = {}) ⇒ Object
Use this to upload the photo in file.
flickr.upload_photo '/path/to/the/photo', :title => 'Title', :description => 'This is the description'
See www.flickr.com/services/api/upload.api.html for more information on the arguments.
91
92
93
|
# File 'lib/flickr.rb', line 91
def upload_photo(file, args={})
upload_flickr(UPLOAD_PATH, file, args)
end
|