ruby-flickr 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/flickr/base.rb +7 -1
- data/lib/flickr/photos.rb +306 -191
- data/lib/ruby_flickr.rb +1 -0
- metadata +1 -1
data/lib/flickr/base.rb
CHANGED
@@ -5,7 +5,12 @@ module Flickr
|
|
5
5
|
|
6
6
|
class Base
|
7
7
|
ENDPOINT = 'http://api.flickr.com/services/rest/'
|
8
|
-
|
8
|
+
|
9
|
+
# create a new flickr object
|
10
|
+
#
|
11
|
+
# Params
|
12
|
+
# * api_key (Required)
|
13
|
+
# * api_secret (Optional)
|
9
14
|
def initialize(api_key, api_secret = nil)
|
10
15
|
@api_key = api_key
|
11
16
|
@api_secret = api_secret
|
@@ -13,6 +18,7 @@ module Flickr
|
|
13
18
|
|
14
19
|
def send_request(method, options = {})
|
15
20
|
options.merge!(:api_key => @api_key, :method => method)
|
21
|
+
options.merge!(:api_sig => Digest::MD5.hexdigest(@api_secret + options.keys.sort_by{|k| k.to_s}.collect{|k| k.to_s + options[k]}.join)) if @api_secret
|
16
22
|
|
17
23
|
api_call = ENDPOINT + "?" + options.collect{|k,v| "#{k}=#{v}"}.join('&')
|
18
24
|
rsp = open(api_call).read
|
data/lib/flickr/photos.rb
CHANGED
@@ -136,230 +136,345 @@ class Flickr::Photos < Flickr::Base
|
|
136
136
|
returning PhotoResponse.new(:page => rsp.photos[:page], :pages => rsp.photos[:pages], :per_page => rsp.photos[:perpage], :total => rsp.photos[:total], :photos => [], :api => self, :method => 'flickr.photos.search', :options => options) do |photos|
|
137
137
|
rsp.photos.photo.each do |photo|
|
138
138
|
all_attributes = {:id => photo[:id],
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
139
|
+
:owner => photo[:owner],
|
140
|
+
:secret => photo[:secret],
|
141
|
+
:server => photo[:server],
|
142
|
+
:farm => photo[:farm],
|
143
|
+
:title => photo[:title],
|
144
|
+
:is_public => photo[:ispublic],
|
145
|
+
:is_friend => photo[:isfriend],
|
146
|
+
:is_family => photo[:isfamily],
|
147
|
+
:license => photo[:license],
|
148
|
+
:uploaded_at => (Time.at(photo[:dateupload].to_i) rescue nil),
|
149
|
+
:taken_at => (Time.parse(photo[:datetaken]) rescue nil),
|
150
|
+
:owner_name => photo[:ownername],
|
151
|
+
:icon_server => photo[:icon_server],
|
152
|
+
:original_format => photo[:originalformat],
|
153
|
+
:updated_at => (Time.at(photo[:lastupdate].to_i) rescue nil),
|
154
|
+
:geo => photo[:geo],
|
155
|
+
:tags => photo[:tags],
|
156
|
+
:machine_tags => photo[:machine_tags],
|
157
|
+
:o_dims => photo[:o_dims],
|
158
|
+
:views => photo[:views]}
|
159
|
+
|
160
|
+
used_attributes = {}
|
161
|
+
|
162
|
+
all_attributes.each do |k,v|
|
163
|
+
used_attributes[k] = v if v
|
164
|
+
end
|
165
|
+
|
166
|
+
photos << Photo.new(@flickr, used_attributes)
|
167
|
+
end if rsp.photos.photo
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# wrapping class to hold a photos response from the flickr api
|
172
|
+
class PhotoResponse
|
173
|
+
attr_accessor :page, :pages, :per_page, :total, :photos, :api, :method, :options
|
174
|
+
|
175
|
+
# creates an object to hold the search response.
|
176
|
+
#
|
177
|
+
# Params
|
178
|
+
# * attributes (Required)
|
179
|
+
# a hash of attributes used to set the initial values of the response object
|
180
|
+
def initialize(attributes)
|
181
|
+
attributes.each do |k,v|
|
182
|
+
send("#{k}=", v)
|
168
183
|
end
|
169
184
|
end
|
170
185
|
|
171
|
-
#
|
172
|
-
|
173
|
-
|
186
|
+
# Add a Flickr::Photos::Photo object to the photos array. It does nothing if you pass a non photo object
|
187
|
+
def <<(photo)
|
188
|
+
self.photos ||= []
|
189
|
+
self.photos << photo if photo.is_a?(Flickr::Photos::Photo)
|
190
|
+
end
|
174
191
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
end
|
192
|
+
# gets the next page from flickr if there are anymore pages in the current photos object
|
193
|
+
def next_page
|
194
|
+
api.send(self.method.split('.').last, options.merge(:page => self.page.to_i + 1)) if self.page.to_i < self.pages.to_i
|
195
|
+
end
|
180
196
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
197
|
+
# gets the previous page from flickr if there is a previous page in the current photos object
|
198
|
+
def previous_page
|
199
|
+
api.send(self.method.split('.').last, options.merge(:page => self.page.to_i - 1)) if self.page.to_i > 1
|
200
|
+
end
|
201
|
+
|
202
|
+
# passes all unknown request to the photos array if it responds to the method
|
203
|
+
def method_missing(method, *args, &block)
|
204
|
+
self.photos.respond_to?(method) ? self.photos.send(method, *args, &block) : super
|
205
|
+
end
|
206
|
+
end
|
186
207
|
|
187
|
-
|
188
|
-
|
189
|
-
|
208
|
+
# wrapping class to hold an flickr photo
|
209
|
+
class Photo
|
210
|
+
attr_accessor :id, :owner, :secret, :server, :farm, :title, :is_public, :is_friend, :is_family # standard attributes
|
211
|
+
attr_accessor :license, :uploaded_at, :taken_at, :owner_name, :icon_server, :original_format, :updated_at, :geo, :tags, :machine_tags, :o_dims, :views # extra attributes
|
212
|
+
attr_accessor :info_added, :description, :original_secret, :owner_username, :owner_realname, :url_photopage, :notes # info attributes
|
213
|
+
attr_accessor :sizes_added, :sizes, :url_square, :url_thumbnail, :url_small, :url_medium, :url_large, :url_original # size attributes
|
214
|
+
attr_accessor :comments_added, :comments # comment attributes
|
215
|
+
|
216
|
+
# create a new instance of a flickr photo.
|
217
|
+
#
|
218
|
+
# Params
|
219
|
+
# * flickr (Required)
|
220
|
+
# the flickr object
|
221
|
+
# * attributes (Required)
|
222
|
+
# a hash of attributes used to set the initial values of the photo object
|
223
|
+
def initialize(flickr, attributes)
|
224
|
+
@flickr = flickr
|
225
|
+
attributes.each do |k,v|
|
226
|
+
send("#{k}=", v)
|
190
227
|
end
|
228
|
+
end
|
191
229
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
230
|
+
# retreive the url to the image stored on flickr
|
231
|
+
#
|
232
|
+
# == Params
|
233
|
+
# * size (Optional)
|
234
|
+
# the size of the image to return. Optional sizes are:
|
235
|
+
# :square - square 75x75
|
236
|
+
# :thumbnail - 100 on longest side
|
237
|
+
# :small - 240 on longest side
|
238
|
+
# :medium - 500 on longest side
|
239
|
+
# :large - 1024 on longest side (only exists for very large original images)
|
240
|
+
# :original - original image, either a jpg, gif or png, depending on source format
|
241
|
+
#
|
242
|
+
def url(size = :medium)
|
243
|
+
attach_sizes
|
244
|
+
send("url_#{size}")
|
245
|
+
end
|
196
246
|
|
197
|
-
|
198
|
-
|
247
|
+
# save the current photo to the local computer
|
248
|
+
#
|
249
|
+
# == Params
|
250
|
+
# * filename (Required)
|
251
|
+
# name of the new file omiting the extention (ex. photo_1)
|
252
|
+
# * size (Optional)
|
253
|
+
# the size of the image to return. Optional sizes are:
|
254
|
+
# :small - square 75x75
|
255
|
+
# :thumbnail - 100 on longest side
|
256
|
+
# :small - 240 on longest side
|
257
|
+
# :medium - 500 on longest side
|
258
|
+
# :large - 1024 on longest side (only exists for very large original images)
|
259
|
+
# :original - original image, either a jpg, gif or png, depending on source format
|
260
|
+
#
|
261
|
+
def save_as(filename, size = :medium)
|
262
|
+
format = size.to_sym == :original ? self.original_format : 'jpg'
|
263
|
+
filename = "#{filename}.#{format}"
|
264
|
+
|
265
|
+
if File.exists?(filename) or not self.url(size)
|
266
|
+
false
|
267
|
+
else
|
268
|
+
f = File.new(filename, 'w+')
|
269
|
+
f.puts open(self.url(size)).read
|
270
|
+
f.close
|
271
|
+
true
|
199
272
|
end
|
200
273
|
end
|
201
274
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
attr_accessor :info_added, :description, :original_secret, :owner_username, :owner_realname, :url_photopage # info attributes
|
207
|
-
attr_accessor :sizes_added, :url_square, :url_thumbnail, :url_small, :url_medium, :url_large, :url_original # size attributes
|
208
|
-
|
209
|
-
def initialize(flickr, attributes)
|
210
|
-
@flickr = flickr
|
211
|
-
attributes.each do |k,v|
|
212
|
-
send("#{k}=", v)
|
213
|
-
end
|
214
|
-
end
|
275
|
+
def description # :nodoc:
|
276
|
+
attach_info
|
277
|
+
@description
|
278
|
+
end
|
215
279
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
# the size of the image to return. Optional sizes are:
|
221
|
-
# :square - square 75x75
|
222
|
-
# :thumbnail - 100 on longest side
|
223
|
-
# :small - 240 on longest side
|
224
|
-
# :medium - 500 on longest side
|
225
|
-
# :large - 1024 on longest side (only exists for very large original images)
|
226
|
-
# :original - original image, either a jpg, gif or png, depending on source format
|
227
|
-
#
|
228
|
-
def url(size = :medium)
|
229
|
-
attach_sizes
|
230
|
-
send("url_#{size}")
|
231
|
-
end
|
280
|
+
def original_secret # :nodoc:
|
281
|
+
attach_info
|
282
|
+
@original_secret
|
283
|
+
end
|
232
284
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
# name of the new file omiting the extention (ex. photo_1)
|
238
|
-
# * size (Optional)
|
239
|
-
# the size of the image to return. Optional sizes are:
|
240
|
-
# :small - square 75x75
|
241
|
-
# :thumbnail - 100 on longest side
|
242
|
-
# :small - 240 on longest side
|
243
|
-
# :medium - 500 on longest side
|
244
|
-
# :large - 1024 on longest side (only exists for very large original images)
|
245
|
-
# :original - original image, either a jpg, gif or png, depending on source format
|
246
|
-
#
|
247
|
-
def save_as(filename, size = :medium)
|
248
|
-
format = size.to_sym == :original ? self.original_format : 'jpg'
|
249
|
-
filename = "#{filename}.#{format}"
|
250
|
-
|
251
|
-
if File.exists?(filename) or not self.url(size)
|
252
|
-
false
|
253
|
-
else
|
254
|
-
f = File.new(filename, 'w+')
|
255
|
-
f.puts open(self.url(size)).read
|
256
|
-
f.close
|
257
|
-
true
|
258
|
-
end
|
259
|
-
end
|
285
|
+
def owner_username # :nodoc:
|
286
|
+
attach_info
|
287
|
+
@owner_username
|
288
|
+
end
|
260
289
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
290
|
+
def owner_realname # :nodoc:
|
291
|
+
attach_info
|
292
|
+
@owner_realname
|
293
|
+
end
|
265
294
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
295
|
+
def url_photopage # :nodoc:
|
296
|
+
attach_info
|
297
|
+
@url_photopage
|
298
|
+
end
|
270
299
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
300
|
+
def comments # :nodoc:
|
301
|
+
attach_comments
|
302
|
+
@comments
|
303
|
+
end
|
304
|
+
|
305
|
+
def sizes # :nodoc:
|
306
|
+
attach_sizes
|
307
|
+
@sizes
|
308
|
+
end
|
275
309
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
310
|
+
def notes # :nodoc:
|
311
|
+
attach_info
|
312
|
+
@notes
|
313
|
+
end
|
280
314
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
protected
|
287
|
-
def url_square
|
288
|
-
attach_sizes
|
289
|
-
@url_square
|
290
|
-
end
|
315
|
+
protected
|
316
|
+
def url_square # :nodoc:
|
317
|
+
attach_sizes
|
318
|
+
@url_square
|
319
|
+
end
|
291
320
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
321
|
+
def url_thumbnail # :nodoc:
|
322
|
+
attach_sizes
|
323
|
+
@url_thumbnail
|
324
|
+
end
|
296
325
|
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
326
|
+
def url_small # :nodoc:
|
327
|
+
attach_sizes
|
328
|
+
@url_small
|
329
|
+
end
|
301
330
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
331
|
+
def url_medium # :nodoc:
|
332
|
+
attach_sizes
|
333
|
+
@url_medium
|
334
|
+
end
|
335
|
+
|
336
|
+
def url_large # :nodoc:
|
337
|
+
attach_sizes
|
338
|
+
@url_large
|
339
|
+
end
|
340
|
+
|
341
|
+
def url_original # :nodoc:
|
342
|
+
attach_sizes
|
343
|
+
@url_original
|
344
|
+
end
|
306
345
|
|
307
|
-
|
308
|
-
|
309
|
-
|
346
|
+
private
|
347
|
+
attr_accessor :comment_count
|
348
|
+
|
349
|
+
# convert the size to the key used in the flickr url
|
350
|
+
def size_key(size)
|
351
|
+
case size.to_sym
|
352
|
+
when :square : 's'
|
353
|
+
when :thumb, :thumbnail : 't'
|
354
|
+
when :small : 'm'
|
355
|
+
when :medium : '-'
|
356
|
+
when :large : 'b'
|
357
|
+
when :original : 'o'
|
358
|
+
else ''
|
310
359
|
end
|
360
|
+
end
|
311
361
|
|
312
|
-
|
313
|
-
|
314
|
-
|
362
|
+
# loads photo info when a field is requested that requires additional info
|
363
|
+
def attach_info
|
364
|
+
unless self.info_added
|
365
|
+
rsp = @flickr.send_request('flickr.photos.getInfo', :photo_id => self.id, :secret => self.secret)
|
366
|
+
|
367
|
+
self.info_added = true
|
368
|
+
self.description = rsp.photo.description.to_s
|
369
|
+
self.original_secret = rsp.photo[:originalsecret]
|
370
|
+
self.owner_username = rsp.photo.owner[:username]
|
371
|
+
self.owner_realname = rsp.photo.owner[:realname]
|
372
|
+
self.url_photopage = rsp.photo.urls.url.to_s
|
373
|
+
self.comment_count = rsp.photo.comments.to_s.to_i
|
374
|
+
|
375
|
+
self.notes = []
|
376
|
+
|
377
|
+
rsp.photo.notes.note.each do |note|
|
378
|
+
self.notes << Note.new(:id => note[:id],
|
379
|
+
:note => note.to_s,
|
380
|
+
:author => note[:author],
|
381
|
+
:author_name => note[:authorname],
|
382
|
+
:x => note[:x],
|
383
|
+
:y => note[:y],
|
384
|
+
:width => note[:w],
|
385
|
+
:height => note[:h])
|
386
|
+
end if rsp.photo.notes.note
|
315
387
|
end
|
388
|
+
end
|
389
|
+
|
390
|
+
# loads picture sizes only after one has been requested
|
391
|
+
def attach_sizes
|
392
|
+
unless self.sizes_added
|
393
|
+
rsp = @flickr.send_request('flickr.photos.getSizes', :photo_id => self.id)
|
394
|
+
|
395
|
+
self.sizes_added = true
|
396
|
+
self.sizes = []
|
397
|
+
|
398
|
+
rsp.sizes.size.each do |size|
|
399
|
+
send("url_#{size[:label].downcase}=", size[:source])
|
316
400
|
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
when :thumb, :thumbnail : 't'
|
323
|
-
when :small : 'm'
|
324
|
-
when :medium : '-'
|
325
|
-
when :large : 'b'
|
326
|
-
when :original : 'o'
|
327
|
-
else ''
|
401
|
+
self.sizes << Size.new(:label => size[:label],
|
402
|
+
:width => size[:width],
|
403
|
+
:height => size[:height],
|
404
|
+
:source => size[:source],
|
405
|
+
:url => size[:url])
|
328
406
|
end
|
329
407
|
end
|
408
|
+
end
|
330
409
|
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
410
|
+
# loads comments once they have been requested
|
411
|
+
def attach_comments
|
412
|
+
if @comment_count == 0
|
413
|
+
self.comments = []
|
414
|
+
self.comments_added = true
|
415
|
+
elsif not self.comments_added
|
416
|
+
rsp = @flickr.send_request('flickr.photos.comments.getList', :photo_id => self.id)
|
417
|
+
|
418
|
+
self.comments = []
|
419
|
+
self.comments_added = true
|
420
|
+
|
421
|
+
rsp.comments.comment.each do |comment|
|
422
|
+
self.comments << Comment.new(:id => comment[:id],
|
423
|
+
:comment => comment.to_s,
|
424
|
+
:author => comment[:author],
|
425
|
+
:author_name => comment[:authorname],
|
426
|
+
:permalink => comment[:permalink],
|
427
|
+
:created_at => (Time.at(comment[:datecreate].to_i) rescue nil))
|
342
428
|
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
# wrapping class to hold a flickr comment
|
434
|
+
class Comment
|
435
|
+
attr_accessor :id, :comment, :author, :author_name, :created_at, :permalink
|
436
|
+
|
437
|
+
# create a new instance of a flickr comment.
|
438
|
+
#
|
439
|
+
# Params
|
440
|
+
# * attributes (Required)
|
441
|
+
# a hash of attributes used to set the initial values of the comment object
|
442
|
+
def initialize(attributes)
|
443
|
+
attributes.each do |k,v|
|
444
|
+
send("#{k}=", v)
|
343
445
|
end
|
446
|
+
end
|
447
|
+
end
|
344
448
|
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
449
|
+
# wrapping class to hold a flickr size
|
450
|
+
class Size
|
451
|
+
attr_accessor :label, :width, :height, :source, :url
|
452
|
+
|
453
|
+
# create a new instance of a flickr size.
|
454
|
+
#
|
455
|
+
# Params
|
456
|
+
# * attributes (Required)
|
457
|
+
# a hash of attributes used to set the initial values of the size object
|
458
|
+
def initialize(attributes)
|
459
|
+
attributes.each do |k,v|
|
460
|
+
send("#{k}=", v)
|
461
|
+
end
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
# wrapping class to hold a flickr note
|
466
|
+
class Note
|
467
|
+
attr_accessor :id, :note, :author, :author_name, :x, :y, :width, :height
|
468
|
+
|
469
|
+
# create a new instance of a flickr note.
|
470
|
+
#
|
471
|
+
# Params
|
472
|
+
# * attributes (Required)
|
473
|
+
# a hash of attributes used to set the initial values of the note object
|
474
|
+
def initialize(attributes)
|
475
|
+
attributes.each do |k,v|
|
476
|
+
send("#{k}=", v)
|
363
477
|
end
|
364
478
|
end
|
365
479
|
end
|
480
|
+
end
|
data/lib/ruby_flickr.rb
CHANGED