Class: FlickRaw::Flickr

Inherits:
Request show all
Defined in:
lib/flickraw/api.rb

Overview

Root class of the flickr api hierarchy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Request

build_request, flickr_methods, flickr_objects, request_name

Constructor Details

#initialize(api_key: FlickRaw.api_key, shared_secret: FlickRaw.shared_secret) ⇒ Flickr

Returns a new instance of Flickr.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flickraw/api.rb', line 39

def initialize(api_key: FlickRaw.api_key,
               shared_secret: FlickRaw.shared_secret)
  if api_key.nil?
    raise FlickrAppNotConfigured.new("No API key defined!")
  end
  if shared_secret.nil?
    raise FlickrAppNotConfigured.new("No shared secret defined!")
  end
  @oauth_consumer = OAuthClient.new(api_key, shared_secret)
  @oauth_consumer.proxy = FlickRaw.proxy
  @oauth_consumer.check_certificate = FlickRaw.check_certificate
  @oauth_consumer.ca_file = FlickRaw.ca_file
  @oauth_consumer.ca_path = FlickRaw.ca_path
  @oauth_consumer.user_agent = USER_AGENT
  @access_token = @access_secret = nil

  Flickr.build(call('flickr.reflection.getMethods')) if Flickr.flickr_objects.empty?
  super self
end

Instance Attribute Details

#access_secretObject

Authenticated access token secret



35
36
37
# File 'lib/flickraw/api.rb', line 35

def access_secret
  @access_secret
end

#access_tokenObject

Authenticated access token



32
33
34
# File 'lib/flickraw/api.rb', line 32

def access_token
  @access_token
end

Class Method Details

.build(methods) ⇒ Object



37
# File 'lib/flickraw/api.rb', line 37

def self.build(methods); methods.each { |m| build_request m } 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.



62
63
64
65
66
67
# File 'lib/flickraw/api.rb', line 62

def call(req, args={}, &block)
  oauth_args = args.delete(:oauth) || {}
  rest_path = FlickRaw.secure ? REST_PATH_SECURE : REST_PATH
  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)


88
89
90
91
92
93
# File 'lib/flickraw/api.rb', line 88

def get_access_token(token, secret, verify)
  flickr_oauth_access_token = FlickRaw.secure ? FLICKR_OAUTH_ACCESS_TOKEN_SECURE : FLICKR_OAUTH_ACCESS_TOKEN
  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')


80
81
82
83
# File 'lib/flickraw/api.rb', line 80

def get_authorize_url(token, args = {})
  flickr_oauth_authorize = FlickRaw.secure ? FLICKR_OAUTH_AUTHORIZE_SECURE : FLICKR_OAUTH_AUTHORIZE
  @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 => "http://example.com")


72
73
74
75
# File 'lib/flickraw/api.rb', line 72

def get_request_token(args = {})
  flickr_oauth_request_token = FlickRaw.secure ? FLICKR_OAUTH_REQUEST_TOKEN_SECURE : FLICKR_OAUTH_REQUEST_TOKEN
  @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.



110
111
112
113
# File 'lib/flickraw/api.rb', line 110

def replace_photo(file, args={})
  replace_path = FlickRaw.secure ? REPLACE_PATH_SECURE : REPLACE_PATH
  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.



100
101
102
103
# File 'lib/flickraw/api.rb', line 100

def upload_photo(file, args={})
  upload_path = FlickRaw.secure ? UPLOAD_PATH_SECURE : UPLOAD_PATH
  upload_flickr(upload_path, file, args)
end