flickr-objects 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +49 -63
  3. data/lib/flickr/api/abstract/params_processor.rb +68 -0
  4. data/lib/flickr/api/abstract.rb +113 -0
  5. data/lib/flickr/api/general.rb +75 -0
  6. data/lib/flickr/api/person.rb +91 -39
  7. data/lib/flickr/api/photo.rb +213 -108
  8. data/lib/flickr/api/set.rb +114 -55
  9. data/lib/flickr/api/upload_ticket.rb +17 -6
  10. data/lib/flickr/api.rb +90 -35
  11. data/lib/flickr/attributes.rb +200 -0
  12. data/lib/flickr/base_converter.rb +29 -0
  13. data/lib/flickr/client/data.rb +47 -0
  14. data/lib/flickr/client/oauth.rb +44 -0
  15. data/lib/flickr/client/upload.rb +55 -0
  16. data/lib/flickr/client.rb +77 -33
  17. data/lib/flickr/configuration.rb +85 -14
  18. data/lib/flickr/error.rb +58 -0
  19. data/lib/flickr/middleware.rb +52 -28
  20. data/lib/flickr/oauth.rb +89 -74
  21. data/lib/flickr/object/attribute_locations/list.rb +25 -0
  22. data/lib/flickr/object/attribute_locations/location.rb +29 -0
  23. data/lib/flickr/object/attribute_locations/permissions.rb +31 -0
  24. data/lib/flickr/object/attribute_locations/person/upload_status.rb +32 -0
  25. data/lib/flickr/object/attribute_locations/person.rb +78 -0
  26. data/lib/flickr/object/attribute_locations/photo/exif.rb +29 -0
  27. data/lib/flickr/object/attribute_locations/photo/note.rb +36 -0
  28. data/lib/flickr/object/attribute_locations/photo/tag.rb +23 -0
  29. data/lib/flickr/object/attribute_locations/photo.rb +164 -0
  30. data/lib/flickr/object/attribute_locations/set.rb +53 -0
  31. data/lib/flickr/object/attribute_locations/upload_ticket.rb +30 -0
  32. data/lib/flickr/object/attribute_locations/visibility.rb +24 -0
  33. data/lib/flickr/object/list/kaminari.rb +30 -0
  34. data/lib/flickr/object/list/normal.rb +27 -0
  35. data/lib/flickr/object/list/will_paginate.rb +31 -0
  36. data/lib/flickr/object/list.rb +87 -0
  37. data/lib/flickr/object/location.rb +35 -0
  38. data/lib/flickr/object/permissions.rb +18 -0
  39. data/lib/flickr/object/person/upload_status.rb +22 -0
  40. data/lib/flickr/object/person.rb +93 -0
  41. data/lib/flickr/object/photo/exif.rb +35 -0
  42. data/lib/flickr/object/photo/note.rb +20 -0
  43. data/lib/flickr/object/photo/size.rb +67 -0
  44. data/lib/flickr/object/photo/tag.rb +23 -0
  45. data/lib/flickr/object/photo.rb +349 -0
  46. data/lib/flickr/object/set.rb +114 -0
  47. data/lib/flickr/object/upload_ticket.rb +31 -0
  48. data/lib/flickr/object/visibility.rb +16 -0
  49. data/lib/flickr/object.rb +118 -27
  50. data/lib/flickr/sanitized_file.rb +70 -0
  51. data/lib/flickr/version.rb +4 -2
  52. data/lib/flickr.rb +69 -15
  53. metadata +89 -103
  54. data/lib/flickr/api/api_methods/flickr.rb +0 -5
  55. data/lib/flickr/api/api_methods/person.rb +0 -14
  56. data/lib/flickr/api/api_methods/photo.rb +0 -26
  57. data/lib/flickr/api/api_methods/set.rb +0 -18
  58. data/lib/flickr/api/api_methods/upload_ticket.rb +0 -5
  59. data/lib/flickr/api/flickr.rb +0 -35
  60. data/lib/flickr/api_caller.rb +0 -98
  61. data/lib/flickr/client/methods_client.rb +0 -22
  62. data/lib/flickr/client/upload_client.rb +0 -62
  63. data/lib/flickr/errors.rb +0 -19
  64. data/lib/flickr/helpers/base_58.rb +0 -15
  65. data/lib/flickr/helpers/boolean.rb +0 -4
  66. data/lib/flickr/helpers/core_ext.rb +0 -5
  67. data/lib/flickr/object/attribute/converter.rb +0 -49
  68. data/lib/flickr/object/attribute/finder.rb +0 -32
  69. data/lib/flickr/object/attribute.rb +0 -45
  70. data/lib/flickr/objects/attribute_values/list.rb +0 -10
  71. data/lib/flickr/objects/attribute_values/location.rb +0 -14
  72. data/lib/flickr/objects/attribute_values/note.rb +0 -11
  73. data/lib/flickr/objects/attribute_values/permissions.rb +0 -12
  74. data/lib/flickr/objects/attribute_values/person/upload_status.rb +0 -16
  75. data/lib/flickr/objects/attribute_values/person.rb +0 -24
  76. data/lib/flickr/objects/attribute_values/photo.rb +0 -84
  77. data/lib/flickr/objects/attribute_values/set.rb +0 -17
  78. data/lib/flickr/objects/attribute_values/tag.rb +0 -9
  79. data/lib/flickr/objects/attribute_values/upload_ticket.rb +0 -9
  80. data/lib/flickr/objects/attribute_values/visibility.rb +0 -10
  81. data/lib/flickr/objects/list.rb +0 -86
  82. data/lib/flickr/objects/location.rb +0 -26
  83. data/lib/flickr/objects/note.rb +0 -14
  84. data/lib/flickr/objects/permissions.rb +0 -14
  85. data/lib/flickr/objects/person/upload_status.rb +0 -14
  86. data/lib/flickr/objects/person.rb +0 -43
  87. data/lib/flickr/objects/photo.rb +0 -113
  88. data/lib/flickr/objects/set.rb +0 -29
  89. data/lib/flickr/objects/tag.rb +0 -17
  90. data/lib/flickr/objects/upload_ticket.rb +0 -18
  91. data/lib/flickr/objects/visibility.rb +0 -12
  92. data/lib/flickr/objects.rb +0 -25
@@ -0,0 +1,67 @@
1
+ module Flickr
2
+ class Object
3
+ class Photo
4
+
5
+ ##
6
+ # Encapsulates the logic of managing sizes, including comparison,
7
+ # to make it easier to add dynamic size-changing methods to
8
+ # Flickr::Object::Photo.
9
+ #
10
+ # @private
11
+ #
12
+ class Size
13
+
14
+ include Comparable
15
+
16
+ NAMES = Photo::SIZES
17
+
18
+ ##
19
+ # Used by Flickr for hash key names.
20
+ #
21
+ ABBREVIATIONS = %w[sq t q s n m z c l h k o]
22
+
23
+ ##
24
+ # Used by Flickr in response from "flickr.photos.getSizes".
25
+ #
26
+ LABELS = [
27
+ "Square", "Thumbnail", "Large Square",
28
+ "Small", "Small 320",
29
+ "Medium", "Medium 640", "Medium 800",
30
+ "Large", "Large 1600", "Large 2048",
31
+ "Original",
32
+ ]
33
+
34
+ def self.all
35
+ NAMES.map { |name| new(name) }
36
+ end
37
+
38
+ def self.types
39
+ all.map(&:type).uniq
40
+ end
41
+
42
+ def self.exists?(name)
43
+ all.include? new(name)
44
+ end
45
+
46
+ attr_reader :name, :type, :number, :abbreviation, :label
47
+
48
+ def initialize(name)
49
+ @name = name
50
+ @type, @number = name.split
51
+ @abbreviation = ABBREVIATIONS[NAMES.index(name)]
52
+ @label = LABELS[NAMES.index(name)]
53
+ end
54
+
55
+ ##
56
+ # {NAMES} orders the sizes from smallest to largest, so we're
57
+ # using that here.
58
+ #
59
+ def <=>(other)
60
+ NAMES.index(self.name) <=> NAMES.index(other.name)
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,23 @@
1
+ module Flickr
2
+ class Object
3
+ class Photo
4
+
5
+ class Tag < Flickr::Object
6
+
7
+ attribute :id, String
8
+ attribute :author, Person
9
+ attribute :raw, String
10
+ attribute :content, String
11
+ attribute :machine_tag, Boolean
12
+
13
+ def to_s
14
+ content
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+
23
+ require_relative "../attribute_locations/photo/tag"
@@ -0,0 +1,349 @@
1
+ module Flickr
2
+ class Object
3
+
4
+ ##
5
+ # Probably the most important class in this library.
6
+ #
7
+ # ## Sizes
8
+ #
9
+ # It has an interface for assigning the photo's size. Current Flickr's photo sizes
10
+ # are:
11
+ #
12
+ # - Square 75/150
13
+ # - Thumbnail
14
+ # - Medium 500/640/800
15
+ # - Large 1024/1600/2048
16
+ # - Original
17
+ #
18
+ # **Important**: When fetching photos, the information about the sizes (URL, width, height)
19
+ # isn't automatically available. If you're fetching multiple photos (e.g. using
20
+ # `Flickr.photos.search`), you need to pass `:sizes => true`. For individual photos,
21
+ # use #get_sizes.
22
+ #
23
+ # Any of the following ways will change the photo's size to "Medium 500":
24
+ #
25
+ # photo.medium500!
26
+ # photo.medium!(500)
27
+ # photo.medium!("500")
28
+ #
29
+ # A change to photo's size affects the following attributes: {#source_url}, {#width} and
30
+ # {#height}.
31
+ #
32
+ # You may often just want to assign the largest possible size to the photo:
33
+ #
34
+ # photo.largest!
35
+ #
36
+ # You may also want to assign the largest size to the photo, but that it's not
37
+ # larger/smaller than "Medium 500":
38
+ #
39
+ # photo.medium500_or_smaller!
40
+ # photo.medium500_at_least!
41
+ #
42
+ # Note: The corresponding non-bang versions of the methods are also available:
43
+ #
44
+ # photo.medium500
45
+ # photo.medium(500)
46
+ # photo.medium("500")
47
+ # photo.largest
48
+ # photo.medium500_or_smaller
49
+ # photo.medium500_at_least
50
+ #
51
+ # However, these are not recommended, since they duplicate the photo, taking up
52
+ # twice as much memory. Use them only if you really have to.
53
+ #
54
+ class Photo < Flickr::Object
55
+
56
+ autoload_names :Note, :Tag, :Exif, :Size
57
+
58
+ attribute :id, String
59
+ attribute :secret, String
60
+ attribute :server, String
61
+ attribute :farm, Integer
62
+ attribute :title, String
63
+ attribute :description, String
64
+ attribute :license, Integer
65
+ attribute :visibility, Visibility
66
+
67
+ attribute :safety_level, Integer
68
+ attribute :safe, Boolean
69
+ attribute :moderate, Boolean
70
+ attribute :restricted, Boolean
71
+
72
+ attribute :url, String
73
+ attribute :short_url, String
74
+
75
+ attribute :owner, Person
76
+
77
+ attribute :uploaded_at, Time
78
+ attribute :posted_at, Time
79
+ attribute :taken_at, Time
80
+ attribute :taken_at_granularity, Integer
81
+ attribute :updated_at, Time
82
+
83
+ attribute :views_count, Integer
84
+ attribute :comments_count, Integer
85
+
86
+ attribute :editability, Permissions
87
+ attribute :public_editability, Permissions
88
+ attribute :usage, Permissions
89
+
90
+ attribute :notes, List[Note]
91
+ attribute :tags, List[Tag]
92
+
93
+ attribute :camera, String
94
+ attribute :exif, Exif
95
+
96
+ attribute :has_people, Boolean
97
+ attribute :favorite, Boolean
98
+
99
+ attribute :path_alias, String
100
+
101
+ attribute :location, Location
102
+ attribute :location_visibility, Visibility
103
+
104
+ attribute :rotation, Integer
105
+
106
+ attribute :available_sizes, Array[String]
107
+ attribute :largest_size, String
108
+
109
+ attribute :source_url, String
110
+ attribute :height, Integer
111
+ attribute :width, Integer
112
+
113
+ SIZES = [
114
+ "Square 75", "Thumbnail", "Square 150",
115
+ "Small 240", "Small 320",
116
+ "Medium 500", "Medium 640", "Medium 800",
117
+ "Large 1024", "Large 1600", "Large 2048",
118
+ "Original",
119
+ ]
120
+
121
+ ##
122
+ # The size of the photo, can be changed with modifer methods (see the
123
+ # definition of this class for details).
124
+ #
125
+ # @return [String]
126
+ #
127
+ def size
128
+ @size.name if @size
129
+ end
130
+
131
+ Size.all.each do |size|
132
+ type, number = size.type, size.number
133
+
134
+ ##
135
+ # medium500!
136
+ # medium500
137
+ #
138
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
139
+ def #{type.downcase}#{number}!
140
+ size! "#{size.name}"
141
+ end
142
+
143
+ def #{type.downcase}#{number}
144
+ dup.#{type.downcase}#{number}!
145
+ end
146
+ RUBY
147
+ end
148
+
149
+ Size.types.each do |type|
150
+ ##
151
+ # medium!(500)
152
+ # medium(500)
153
+ #
154
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
155
+ def #{type.downcase}!(number = nil)
156
+ size! (number ? "#{type} \#{number}" : "#{type}")
157
+ end
158
+
159
+ def #{type.downcase}(number = nil)
160
+ dup.#{type.downcase}!(number)
161
+ end
162
+ RUBY
163
+ end
164
+
165
+ Size.all.each do |size|
166
+ type, number = size.type, size.number
167
+
168
+ ##
169
+ # medium500_or_smaller
170
+ # medium500_or_smaller!
171
+ #
172
+ # medium500_at_least
173
+ # medium500_at_least!
174
+ #
175
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
176
+ def #{type.downcase}#{number}_or_smaller!
177
+ upper_bound = "#{size.name}"
178
+ new_size = available_sizes.reverse.find do |name|
179
+ Size.new(name) <= Size.new(upper_bound)
180
+ end
181
+ size! new_size
182
+ end
183
+
184
+ def #{type.downcase}#{number}_or_smaller
185
+ dup.#{type.downcase}#{number}_or_smaller!
186
+ end
187
+
188
+ def #{type.downcase}#{number}_at_least!
189
+ lower_bound = "#{size.name}"
190
+ new_size = available_sizes.reverse.find do |name|
191
+ Size.new(name) >= Size.new(lower_bound)
192
+ end
193
+ size! new_size
194
+ end
195
+
196
+ def #{type.downcase}#{number}_at_least
197
+ dup.#{type.downcase}#{number}_at_least!
198
+ end
199
+ RUBY
200
+ end
201
+
202
+ ##
203
+ # @return [self]
204
+ #
205
+ def largest!
206
+ size! largest_size
207
+ end
208
+
209
+ ##
210
+ # @return [self]
211
+ #
212
+ def largest
213
+ dup.largest!
214
+ end
215
+
216
+ ##
217
+ # @return [self]
218
+ # @see Flickr::Api::Photo#get_info
219
+ #
220
+ def get_info!(params = {})
221
+ photo = api.get_info(id, params)
222
+ update(photo.attributes)
223
+ end
224
+
225
+ ##
226
+ # @return [self]
227
+ # @see Flickr::Api::Photo#get_sizes
228
+ #
229
+ def get_sizes!(params = {})
230
+ photo = api.get_sizes(id, params)
231
+ update(photo.attributes)
232
+ end
233
+
234
+ ##
235
+ # @return [self]
236
+ # @see Flickr::Api::Photo#get_exif
237
+ #
238
+ def get_exif!(params = {})
239
+ photo = api.get_exif(id, params)
240
+ update(photo.attributes)
241
+ end
242
+
243
+ ##
244
+ # @return [Flickr::Object::List<Flickr::Object::Person>]
245
+ # @see Flickr::Api::Photo#get_favorites
246
+ #
247
+ def get_favorites(params = {})
248
+ api.get_favorites(id, params)
249
+ end
250
+
251
+ ##
252
+ # @return [response]
253
+ # @see Flickr::Api::Photo#delete
254
+ #
255
+ def delete(params = {})
256
+ api.delete(id, params)
257
+ end
258
+
259
+ ##
260
+ # @return [response]
261
+ # @see Flickr::Api::Photo#set_content_type
262
+ #
263
+ def set_content_type(content_type, params = {})
264
+ api.set_content_type(id, content_type, params)
265
+ end
266
+ alias content_type= set_content_type
267
+
268
+ ##
269
+ # @return [response]
270
+ # @see Flickr::Api::Photo#set_tags
271
+ #
272
+ def set_tags(tags, params = {})
273
+ api.set_tags(id, tags, params)
274
+ end
275
+ alias tags= set_tags
276
+
277
+ ##
278
+ # @return [response]
279
+ # @see Flickr::Api::Photo#add_tags
280
+ #
281
+ def add_tags(tags, params = {})
282
+ api.add_tags(id, tags, params)
283
+ end
284
+
285
+ ##
286
+ # @return [response]
287
+ # @see Flickr::Api::Photo#remove_tag
288
+ #
289
+ def remove_tag(tag_or_id, params = {})
290
+ api.remove_tag(id, tag_or_id, params)
291
+ end
292
+
293
+ ##
294
+ # @return [response]
295
+ # @see Flickr::Api::Photo#set_dates
296
+ #
297
+ def set_dates(params = {})
298
+ api.set_dates(id, params)
299
+ end
300
+
301
+ ##
302
+ # @return [response]
303
+ # @see Flickr::Api::Photo#set_meta
304
+ #
305
+ def set_meta(params = {})
306
+ api.set_meta(id, params)
307
+ end
308
+
309
+ ##
310
+ # @return [response]
311
+ # @see Flickr::Api::Photo#set_permissions
312
+ #
313
+ def set_permissions(params = {})
314
+ api.set_permissions(id, params)
315
+ end
316
+
317
+ ##
318
+ # @return [response]
319
+ # @see Flickr::Api::Photo#set_safety_level
320
+ #
321
+ def set_safety_level(params = {})
322
+ api.set_safety_level(id, params)
323
+ end
324
+
325
+ private
326
+
327
+ ##
328
+ # Changes the size of the photo.
329
+ #
330
+ def size!(name)
331
+ if name != nil and not Size.exists?(name)
332
+ raise ArgumentError, "\"#{name}\" isn't a valid photo size"
333
+ end
334
+
335
+ if available_sizes.include? name
336
+ @size = Size.new(name)
337
+ else
338
+ @size = nil
339
+ end
340
+
341
+ self
342
+ end
343
+
344
+ end
345
+
346
+ end
347
+ end
348
+
349
+ require_relative "attribute_locations/photo"
@@ -0,0 +1,114 @@
1
+ module Flickr
2
+ class Object
3
+
4
+ class Set < Flickr::Object
5
+
6
+ attribute :id, String
7
+ attribute :secret, String
8
+ attribute :server, String
9
+ attribute :farm, Integer
10
+ attribute :url, String
11
+ attribute :title, String
12
+ attribute :description, String
13
+
14
+ attribute :owner, Person
15
+
16
+ attribute :photos_count, Integer
17
+ attribute :views_count, Integer
18
+ attribute :comments_count, Integer
19
+
20
+ attribute :permissions, Permissions
21
+
22
+ attribute :created_at, Time
23
+ attribute :updated_at, Time
24
+
25
+ attribute :primary_photo, Photo
26
+
27
+ ##
28
+ # @return [response]
29
+ # @see Flickr::Api::Set#delete
30
+ #
31
+ def delete(params = {})
32
+ api.delete(id, params)
33
+ end
34
+
35
+ ##
36
+ # @return [response]
37
+ # @see Flickr::Api::Set#edit_photos
38
+ #
39
+ def edit_photos(params = {})
40
+ api.edit_photos(id, params)
41
+ end
42
+
43
+ ##
44
+ # @return [self]
45
+ # @see Flickr::Api::Set#get_info
46
+ #
47
+ def get_info!(params = {})
48
+ set = api.get_info(id, params)
49
+ update(set.attributes)
50
+ end
51
+
52
+ ##
53
+ # @return [Flickr::Object::List<Flickr::Object::Photo>]
54
+ # @see Flickr::Api::Set#get_photos
55
+ #
56
+ def get_photos(params = {})
57
+ api.get_photos(id, params)
58
+ end
59
+
60
+ ##
61
+ # @return [response]
62
+ # @see Flickr::Api::Set#add_photo
63
+ #
64
+ def add_photo(photo_or_id, params = {})
65
+ api.add_photo(id, photo_or_id, params)
66
+ end
67
+
68
+ ##
69
+ # @return [response]
70
+ # @see Flickr::Api::Set#remove_photos
71
+ #
72
+ def remove_photos(photo_ids, params = {})
73
+ api.remove_photos(id, photo_ids, params)
74
+ end
75
+
76
+ ##
77
+ # @return [response]
78
+ # @see Flickr::Api::Set#remove_photo
79
+ #
80
+ def remove_photo(photo_or_id, params = {})
81
+ api.remove_photo(id, photo_or_id, params)
82
+ end
83
+
84
+ ##
85
+ # @return [response]
86
+ # @see Flickr::Api::Set#edit_meta
87
+ #
88
+ def edit_meta(params = {})
89
+ api.edit_meta(id, params)
90
+ end
91
+
92
+ ##
93
+ # @return [response]
94
+ # @see Flickr::Api::Set#reorder_photos
95
+ #
96
+ def reorder_photos(photo_ids, params = {})
97
+ api.reorder_photos(id, photo_ids, params)
98
+ end
99
+
100
+ ##
101
+ # @return [response]
102
+ # @see Flickr::Api::Set#set_primary_photo
103
+ #
104
+ def set_primary_photo(photo_or_id, params = {})
105
+ api.set_primary_photo(id, photo_or_id, params)
106
+ end
107
+ alias primary_photo= set_primary_photo
108
+
109
+ end
110
+
111
+ end
112
+ end
113
+
114
+ require_relative "attribute_locations/set"
@@ -0,0 +1,31 @@
1
+ module Flickr
2
+ class Object
3
+
4
+ class UploadTicket < Flickr::Object
5
+
6
+ attribute :id, String
7
+
8
+ attribute :status, Integer
9
+ attribute :complete, Boolean
10
+ attribute :failed, Boolean
11
+
12
+ attribute :valid, Boolean
13
+ attribute :invalid, Boolean
14
+
15
+ attribute :photo, Photo
16
+
17
+ ##
18
+ # @return [self]
19
+ # @see Flickr::Api::UploadTicket#check
20
+ #
21
+ def get_info!(params = {})
22
+ ticket = api.check(id, params).first
23
+ update(ticket.attributes)
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ require_relative "attribute_locations/upload_ticket"
@@ -0,0 +1,16 @@
1
+ module Flickr
2
+ class Object
3
+
4
+ class Visibility < Flickr::Object
5
+
6
+ attribute :public, Boolean
7
+ attribute :friends, Boolean
8
+ attribute :family, Boolean
9
+ attribute :contacts, Boolean
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ require_relative "attribute_locations/visibility"