Class: Net::Flickr

Inherits:
Object
  • Object
show all
Defined in:
lib/net/flickr.rb,
lib/net/flickr/tag.rb,
lib/net/flickr/auth.rb,
lib/net/flickr/list.rb,
lib/net/flickr/photo.rb,
lib/net/flickr/errors.rb,
lib/net/flickr/people.rb,
lib/net/flickr/person.rb,
lib/net/flickr/photos.rb,
lib/net/flickr/photolist.rb

Overview

Net::Flickr

This library implements Flickr’s REST API. Its usage should be pretty straightforward. See below for examples.

Author

Ryan Grove ([email protected])

Version

0.0.1

Copyright

Copyright © 2007-2008 Ryan Grove. All rights reserved.

License

New BSD License (opensource.org/licenses/bsd-license.php)

Website

code.google.com/p/net-flickr/

APIs not yet implemented

  • activity

  • blogs

  • contacts

  • favorites

  • groups

  • groups.pools

  • interestingness

  • photos.comments

  • photos.geo

  • photos.licenses

  • photos.notes

  • photos.transform

  • photos.upload

  • photosets

  • photosets.comments

  • reflection

  • tags

  • test

  • urls

Defined Under Namespace

Classes: APIError, Auth, InvalidResponse, List, ListError, People, Person, Photo, PhotoList, Photos, Tag

Constant Summary collapse

AUTH_URL =
'http://flickr.com/services/auth/'.freeze
REST_ENDPOINT =
'http://api.flickr.com/services/rest/'.freeze
VERSION =
'0.0.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret = nil) ⇒ Flickr

Creates a new Net::Flickr object that will use the specified api_key and api_secret to connect to Flickr. If you don’t already have a Flickr API key, you can get one at flickr.com/services/api/keys.

If you don’t provide an api_secret, you won’t be able to make API calls requiring authentication.



102
103
104
105
106
107
108
109
110
# File 'lib/net/flickr.rb', line 102

def initialize(api_key, api_secret = nil)
  @api_key    = api_key
  @api_secret = api_secret
  
  # Initialize dependent classes.
  @auth   = Auth.new(self)
  @people = People.new(self)
  @photos = Photos.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



94
95
96
# File 'lib/net/flickr.rb', line 94

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



94
95
96
# File 'lib/net/flickr.rb', line 94

def api_secret
  @api_secret
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#authObject

Returns a Net::Flickr::Auth instance.



113
114
115
# File 'lib/net/flickr.rb', line 113

def auth
  @auth
end

#parse_response(response_xml) ⇒ Object

Parses the specified Flickr REST response. If the response indicates a successful request, the response block will be returned as an Hpricot element. Otherwise, an error will be raised.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/net/flickr.rb', line 120

def parse_response(response_xml)
  begin
    xml = Hpricot::XML(response_xml)
  rescue => e
    raise InvalidResponse, 'Invalid Flickr API response'
  end
  
  unless rsp = xml.at('/rsp')
    raise InvalidResponse, 'Invalid Flickr API response'
  end
  
  if rsp['stat'] == 'ok'
    return rsp
  elsif rsp['stat'] == 'fail'
    raise APIError, rsp.at('/err')['msg']
  else
    raise InvalidResponse, 'Invalid Flickr API response'
  end
end

#peopleObject

Returns a Net::Flickr::People instance.



141
142
143
# File 'lib/net/flickr.rb', line 141

def people
  @people
end

#photosObject

Returns a Net::Flickr::Photos instance.



146
147
148
# File 'lib/net/flickr.rb', line 146

def photos
  @photos
end

#request(method, args = {}) ⇒ Object

Calls the specified Flickr REST API method with the supplied arguments and returns a Flickr REST response in XML format. If an API secret is set, the request will be properly signed.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/net/flickr.rb', line 153

def request(method, args = {})
  params  = args.merge({'method' => method, 'api_key' => @api_key})      
  url     = URI.parse(REST_ENDPOINT)
  http    = Net::HTTP.new(url.host, url.port)
  request = sign_request(Net::HTTP::Post.new(url.path), params)
  
  http.start do |http|
    if block_given?
      http.request(request) {|response| yield response }
    else
      response = http.request(request)
    
      # Raise a Net::HTTP error if the HTTP request failed.
      unless response.is_a?(Net::HTTPSuccess) || 
          response.is_a?(Net::HTTPRedirection)
        response.error!
      end
      
      # Return the parsed response.
      return parse_response(response.body)
    end
  end
end

#sign_request(request, params) ⇒ Object

Signs a Flickr API request with the API secret if set.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/net/flickr.rb', line 178

def sign_request(request, params)
  # If the secret isn't set, we can't sign anything.
  if @api_secret.nil?
    request.set_form_data(params)
    return request
  end
  
  # Add auth_token to the param list if we're already authenticated.
  params['auth_token'] = @auth.token unless @auth.token.nil?
  
  # Build a sorted, concatenated parameter list as described at
  # http://flickr.com/services/api/auth.spec.html
  paramlist = ''
  params.keys.sort.each {|key| paramlist << key << 
      URI.escape(params[key].to_s) }
  
  # Sign the request with a hash of the secret key and the concatenated
  # parameter list.
  params['api_sig'] = Digest::MD5.hexdigest(@api_secret + paramlist)
  request.set_form_data(params)
  
  return request      
end

#sign_url(url) ⇒ Object

Signs a Flickr URL with the API secret if set.



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/net/flickr.rb', line 203

def sign_url(url)
  return url if @api_secret.nil?

  uri = URI.parse(url)

  params = uri.query.split('&')
  params << 'api_sig=' + Digest::MD5.hexdigest(@api_secret +
      params.sort.join('').gsub('=', ''))

  uri.query = params.join('&')

  return uri.to_s
end