Class: Flickr

Inherits:
Object
  • 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/cyclotron3k/flickr)".freeze
END_POINT =
'https://api.flickr.com/services'.freeze
UPLOAD_END_POINT =
'https://up.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 =
(UPLOAD_END_POINT + '/upload/').freeze
REPLACE_PATH =
(UPLOAD_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.1.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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flickr.rb', line 38

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 # used for propagating the client to sub-classes
end

Class Attribute Details

.api_keyObject

Your flickr API key, see www.flickr.com/services/api/keys for more information



210
211
212
# File 'lib/flickr.rb', line 210

def api_key
  @api_key
end

.ca_fileObject

Set path of a CA certificate file in PEM format (ssl connection only)



225
226
227
# File 'lib/flickr.rb', line 225

def ca_file
  @ca_file
end

.ca_pathObject

Set path to a directory of CA certificate files in PEM format (ssl connection only)



228
229
230
# File 'lib/flickr.rb', line 228

def ca_path
  @ca_path
end

.cacheObject

Set path to a file that can be used to store endpoints



231
232
233
# File 'lib/flickr.rb', line 231

def cache
  @cache
end

.check_certificateObject

Check the server certificate (ssl connection only)



222
223
224
# File 'lib/flickr.rb', line 222

def check_certificate
  @check_certificate
end

.proxyObject

Use a proxy



216
217
218
# File 'lib/flickr.rb', line 216

def proxy
  @proxy
end

.secureObject

Use ssl connection



219
220
221
# File 'lib/flickr.rb', line 219

def secure
  @secure
end

.shared_secretObject

The shared secret of api_key, see www.flickr.com/services/api/keys for more information



213
214
215
# File 'lib/flickr.rb', line 213

def shared_secret
  @shared_secret
end

Instance Attribute Details

#access_secretObject

Authenticated access token secret



31
32
33
# File 'lib/flickr.rb', line 31

def access_secret
  @access_secret
end

#access_tokenObject

Authenticated access token



28
29
30
# File 'lib/flickr.rb', line 28

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



33
34
35
# File 'lib/flickr.rb', line 33

def client
  @client
end

Class Method Details

.base58(id) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/flickr.rb', line 235

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

.gen_url(r, type) ⇒ Object



246
247
248
# File 'lib/flickr.rb', line 246

def gen_url(r, type)
  PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.secret, type, 'jpg']
end

.url(r) ⇒ Object



250
# File 'lib/flickr.rb', line 250

def url(r); gen_url(r, '') end

.url_o(r) ⇒ Object



258
# File 'lib/flickr.rb', line 258

def url_o(r); PHOTO_SOURCE_URL % [r.farm, r.server, r.id, r.originalsecret, '_o', r.originalformat] end

.url_photopage(r) ⇒ Object



260
# File 'lib/flickr.rb', line 260

def url_photopage(r); url_photostream(r) + r.id end

.url_photoset(r) ⇒ Object



262
# File 'lib/flickr.rb', line 262

def url_photoset(r); url_photosets(r) + r.id end

.url_photosets(r) ⇒ Object



261
# File 'lib/flickr.rb', line 261

def url_photosets(r); url_photostream(r) + 'sets/' end

.url_photostream(r) ⇒ Object



269
270
271
272
273
274
275
276
277
278
# File 'lib/flickr.rb', line 269

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



259
# File 'lib/flickr.rb', line 259

def url_profile(r); URL_PROFILE + (r.owner.respond_to?(:nsid) ? r.owner.nsid : r.owner) + '/' end

.url_short(r) ⇒ Object



263
# File 'lib/flickr.rb', line 263

def url_short(r); URL_SHORT + base58(r.id) end

.url_short_m(r) ⇒ Object



264
# File 'lib/flickr.rb', line 264

def url_short_m(r); URL_SHORT + 'img/' + base58(r.id) + '_m.jpg' end

.url_short_n(r) ⇒ Object



268
# File 'lib/flickr.rb', line 268

def url_short_n(r); URL_SHORT + 'img/' + base58(r.id) + '_n.jpg' end

.url_short_q(r) ⇒ Object



267
# File 'lib/flickr.rb', line 267

def url_short_q(r); URL_SHORT + 'img/' + base58(r.id) + '_q.jpg' end

.url_short_s(r) ⇒ Object



265
# File 'lib/flickr.rb', line 265

def url_short_s(r); URL_SHORT + 'img/' + base58(r.id) + '.jpg'   end

.url_short_t(r) ⇒ Object



266
# File 'lib/flickr.rb', line 266

def url_short_t(r); URL_SHORT + 'img/' + base58(r.id) + '_t.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.



58
59
60
61
62
# File 'lib/flickr.rb', line 58

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)


81
82
83
84
85
# File 'lib/flickr.rb', line 81

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')


74
75
76
# File 'lib/flickr.rb', line 74

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")


67
68
69
# File 'lib/flickr.rb', line 67

def get_request_token(args = {})
  @oauth_consumer.request_token(FLICKR_OAUTH_REQUEST_TOKEN, args)
end

#replace_photo(file, args = {}) ⇒ Object

Use this to replace the photo with :photo_id with the photo in file.

flickr.replace_photo '/path/to/the/photo', :photo_id => id

See www.flickr.com/services/api/replace.api.html for more information on the arguments.



101
102
103
# File 'lib/flickr.rb', line 101

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.



92
93
94
# File 'lib/flickr.rb', line 92

def upload_photo(file, args = {})
  upload_flickr(UPLOAD_PATH, file, args)
end