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

#initializeFlickr

:nodoc:


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

def initialize # :nodoc:
  if FlickRaw.api_key.nil?
    raise FlickrAppNotConfigured.new("No API key defined!")
  end
  @oauth_consumer = OAuthClient.new(FlickRaw.api_key, FlickRaw.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.


58
59
60
61
62
63
# File 'lib/flickraw/api.rb', line 58

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)

84
85
86
87
88
89
# File 'lib/flickraw/api.rb', line 84

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

76
77
78
79
# File 'lib/flickraw/api.rb', line 76

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

68
69
70
71
# File 'lib/flickraw/api.rb', line 68

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.


106
107
108
109
# File 'lib/flickraw/api.rb', line 106

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.


96
97
98
99
# File 'lib/flickraw/api.rb', line 96

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