flickrie 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/README.md +1 -1
  2. data/lib/flickrie.rb +18 -92
  3. data/lib/flickrie/api_methods.rb +759 -11
  4. data/lib/flickrie/base58.rb +15 -0
  5. data/lib/flickrie/callable.rb +78 -0
  6. data/lib/flickrie/client.rb +25 -17
  7. data/lib/flickrie/instance.rb +10 -15
  8. data/lib/flickrie/middleware.rb +4 -21
  9. data/lib/flickrie/middleware/fix_flickr_data.rb +215 -0
  10. data/lib/flickrie/middleware/retry.rb +23 -0
  11. data/lib/flickrie/{license.rb → objects/license.rb} +0 -0
  12. data/lib/flickrie/{location.rb → objects/location.rb} +13 -13
  13. data/lib/flickrie/{media.rb → objects/media.rb} +30 -25
  14. data/lib/flickrie/{media → objects/media}/exif.rb +0 -0
  15. data/lib/flickrie/{media → objects/media}/note.rb +11 -12
  16. data/lib/flickrie/{media → objects/media}/tag.rb +10 -9
  17. data/lib/flickrie/objects/media/visibility.rb +28 -0
  18. data/lib/flickrie/objects/media_context.rb +17 -0
  19. data/lib/flickrie/objects/media_count.rb +46 -0
  20. data/lib/flickrie/{photo.rb → objects/photo.rb} +2 -3
  21. data/lib/flickrie/{set.rb → objects/set.rb} +9 -30
  22. data/lib/flickrie/{ticket.rb → objects/ticket.rb} +0 -0
  23. data/lib/flickrie/{user.rb → objects/user.rb} +10 -50
  24. data/lib/flickrie/{user → objects/user}/upload_status.rb +0 -0
  25. data/lib/flickrie/{video.rb → objects/video.rb} +3 -4
  26. data/lib/flickrie/version.rb +1 -1
  27. metadata +38 -37
  28. data/lib/flickrie/api_methods/media.rb +0 -698
  29. data/lib/flickrie/api_methods/set.rb +0 -23
  30. data/lib/flickrie/api_methods/user.rb +0 -45
  31. data/lib/flickrie/media/class_methods.rb +0 -217
  32. data/lib/flickrie/media/visibility.rb +0 -29
  33. data/lib/flickrie/media_count.rb +0 -54
@@ -0,0 +1,23 @@
1
+ module Flickrie
2
+ module Middleware
3
+ class Retry < Faraday::Middleware
4
+ def initialize(app, retries = 2)
5
+ @retries = retries
6
+ super(app)
7
+ end
8
+
9
+ def call(env)
10
+ retries = @retries
11
+ begin
12
+ @app.call(env)
13
+ rescue Faraday::Error::TimeoutError
14
+ if retries > 0
15
+ retries -= 1
16
+ retry
17
+ end
18
+ raise
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -6,13 +6,13 @@ module Flickrie
6
6
  # :country, :place_id, :woeid, :hash
7
7
 
8
8
  # @return [Fixnum]
9
- def latitude() @info['latitude'] end
9
+ def latitude() @hash['latitude'] end
10
10
  # @return [Fixnum]
11
- def longitude() @info['longitude'] end
11
+ def longitude() @hash['longitude'] end
12
12
  # @return [String]
13
- def accuracy() @info['accuracy'] end
13
+ def accuracy() @hash['accuracy'] end
14
14
  # @return [Fixnum]
15
- def context() Integer(@info['context']) rescue nil end
15
+ def context() Integer(@hash['context']) rescue nil end
16
16
 
17
17
  # Returns a struct with attributes `#name`, `#place_id` and `#woeid`
18
18
  #
@@ -36,31 +36,31 @@ module Flickrie
36
36
  def country() new_area('country') end
37
37
 
38
38
  # @return [String]
39
- def place_id() @info['place_id'] end
39
+ def place_id() @hash['place_id'] end
40
40
  # @return [String]
41
- def woeid() @info['woeid'] end
41
+ def woeid() @hash['woeid'] end
42
42
 
43
- def [](key) @info[key] end
43
+ def [](key) @hash[key] end
44
44
  # Returns the raw hash from the response. Useful if something isn't available by methods.
45
45
  #
46
46
  # @return [Hash]
47
- def hash() @info end
47
+ def hash() @hash end
48
48
 
49
49
  private
50
50
 
51
- def initialize(info = {})
52
- raise ArgumentError if info.nil?
53
- @info = info
51
+ def initialize(hash)
52
+ raise ArgumentError if hash.nil?
53
+ @hash = hash
54
54
  end
55
55
 
56
56
  def new_area(area_name)
57
- if @info[area_name]
57
+ if @hash[area_name]
58
58
  area_class = Class.new(Struct.new(:name, :place_id, :woeid)) do
59
59
  def to_s
60
60
  name
61
61
  end
62
62
  end
63
- info = @info[area_name]
63
+ info = @hash[area_name]
64
64
  area_class.new(info['_content'], info['place_id'], info['woeid'])
65
65
  end
66
66
  end
@@ -1,9 +1,8 @@
1
- require 'flickrie/media/class_methods'
2
- require 'flickrie/media/visibility'
3
- require 'flickrie/media/note'
4
- require 'flickrie/media/tag'
5
- require 'flickrie/media/exif'
6
- require 'flickrie/location'
1
+ require 'flickrie/objects/media/visibility'
2
+ require 'flickrie/objects/media/note'
3
+ require 'flickrie/objects/media/tag'
4
+ require 'flickrie/objects/media/exif'
5
+ require 'flickrie/objects/location'
7
6
  require 'date'
8
7
 
9
8
  module Flickrie
@@ -61,7 +60,7 @@ module Flickrie
61
60
  def geo_permissions() Visibility.new(@hash['geoperms']) rescue nil end
62
61
 
63
62
  # @return [Array<Flickrie::Media::Tag>]
64
- def tags() @hash['tags'].map { |info| Tag.new(info) } rescue nil end
63
+ def tags() @hash['tags'].map { |info| Tag.new(info, @api_caller) } rescue nil end
65
64
  # @return [Array<Flickrie::Media::Tag>]
66
65
  def machine_tags() tags.select { |tag| tag.machine_tag? } rescue nil end
67
66
 
@@ -80,7 +79,7 @@ module Flickrie
80
79
  def taken_at_granularity() Integer(@hash['dates']['takengranularity']) rescue nil end
81
80
 
82
81
  # @return [Flickrie::User]
83
- def owner() User.new(@hash['owner']) rescue nil end
82
+ def owner() User.new(@hash['owner'], @api_caller) if @hash['owner'] end
84
83
 
85
84
  # @return [Fixnum]
86
85
  def safety_level() Integer(@hash['safety_level']) rescue nil end
@@ -145,14 +144,14 @@ module Flickrie
145
144
  def faved?() Integer(@hash['is_faved']) == 1 rescue nil end
146
145
 
147
146
  # @return [Array<Flickrie::Media::Note>]
148
- def notes() @hash['notes']['note'].map { |info| Note.new(info) } rescue nil end
147
+ def notes() @hash['notes']['note'].map { |info| Note.new(info, @api_caller) } rescue nil end
149
148
 
150
149
  # @return [Integer]
151
150
  def content_type() Integer(@hash['content_type']) rescue nil end
152
151
 
153
152
  # @return [Flickrie::Collection<Flickrie::User>]
154
153
  def favorites
155
- collection = @hash['person'].map { |info| User.new(info) }
154
+ collection = @hash['person'].map { |info| User.new(info, @api_caller) }
156
155
  Collection.new(@hash).replace(collection)
157
156
  rescue
158
157
  nil
@@ -168,8 +167,7 @@ module Flickrie
168
167
  #
169
168
  # @return [self]
170
169
  def get_info(params = {})
171
- media = Flickrie.send("get_#{media_type}_info", id, params)
172
- @hash.deep_merge!(media.hash)
170
+ @hash.deep_merge!(@api_caller.send("get_#{media_type}_info", id, params).hash)
173
171
  self
174
172
  end
175
173
 
@@ -177,8 +175,7 @@ module Flickrie
177
175
  #
178
176
  # @return [self]
179
177
  def get_exif(params = {})
180
- media = Flickrie.send("get_#{media_type}_exif", id, params)
181
- @hash.deep_merge!(media.hash)
178
+ @hash.deep_merge!(@api_caller.send("get_#{media_type}_exif", id, params).hash)
182
179
  self
183
180
  end
184
181
 
@@ -186,13 +183,13 @@ module Flickrie
186
183
  #
187
184
  # @return [self]
188
185
  def get_favorites(params = {})
189
- media = Flickrie.send("get_#{media_type}_favorites", id, params)
190
- @hash.deep_merge!(media.hash)
186
+ @hash.deep_merge!(@api_caller.send("get_#{media_type}_favorites", id, params).hash)
191
187
  self
192
188
  end
193
189
 
194
- def initialize(hash = {})
190
+ def initialize(hash, api_caller)
195
191
  @hash = hash
192
+ @api_caller = api_caller
196
193
  end
197
194
 
198
195
  private
@@ -201,16 +198,24 @@ module Flickrie
201
198
  self.class.name.split('::').last.downcase
202
199
  end
203
200
 
204
- BASE58_ALPHABET = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.chars.to_a.freeze
201
+ module ClassMethods
202
+ def new_collection(hash, api_caller)
203
+ collection = hash.delete('photo').map { |info| new(info, api_caller) }
204
+ Collection.new(hash).replace(collection)
205
+ end
206
+ end
207
+ extend ClassMethods
205
208
 
206
- def to_base58(id)
207
- id = Integer(id)
208
- begin
209
- id, remainder = id.divmod(58)
210
- result = BASE58_ALPHABET[remainder] + (result || '')
211
- end while id > 0
209
+ # @private
210
+ def self.included(klass)
211
+ klass.extend(ClassMethods)
212
+ end
212
213
 
213
- result
214
+ # @private
215
+ def self.new(hash, api_caller)
216
+ eval(hash['media'].capitalize).new(hash, api_caller)
214
217
  end
218
+
219
+ include Base58
215
220
  end
216
221
  end
@@ -6,34 +6,33 @@ module Flickrie
6
6
  # :height, :hash
7
7
 
8
8
  # @return [String]
9
- def id() @info['id'] end
9
+ def id() @hash['id'] end
10
10
  # @return [Flickrie::User]
11
- def author() User.new('nsid' => @info['author'], 'username' => @info['authorname']) end
11
+ def author() User.new({'nsid' => @hash['author'], 'username' => @hash['authorname']}, @api_caller) end
12
12
  # @return [String]
13
- def content() @info['_content'] end
13
+ def content() @hash['_content'] end
14
14
  # Returns a 2-element array, representing a point.
15
15
  #
16
16
  # @return [Array<Fixnum>]
17
- def coordinates() [@info['x'].to_i, @info['y'].to_i] end
17
+ def coordinates() [@hash['x'].to_i, @hash['y'].to_i] end
18
18
  # @return [Fixnum]
19
- def width() @info['w'].to_i end
19
+ def width() @hash['w'].to_i end
20
20
  # @return [Fixnum]
21
- def height() @info['h'].to_i end
21
+ def height() @hash['h'].to_i end
22
22
 
23
23
  def to_s
24
24
  content
25
25
  end
26
26
 
27
- def [](key) @info[key] end
27
+ def [](key) @hash[key] end
28
28
  # @return [Fixnum]
29
- def hash() @info end
29
+ def hash() @hash end
30
30
 
31
31
  private
32
32
 
33
- def initialize(info)
34
- raise ArgumentError if info.nil?
35
-
36
- @info = info
33
+ def initialize(hash, api_caller)
34
+ @hash = hash
35
+ @api_caller = api_caller
37
36
  end
38
37
  end
39
38
  end
@@ -5,30 +5,31 @@ module Flickrie
5
5
  # :id, :raw, :content, :machine_tag?, :author, :hash
6
6
 
7
7
  # @return [String]
8
- def id() @info['id'] end
8
+ def id() @hash['id'] end
9
9
  # @return [String]
10
- def raw() @info['raw'] end
10
+ def raw() @hash['raw'] end
11
11
  # @return [String]
12
- def content() @info['_content'] end
12
+ def content() @hash['_content'] end
13
13
 
14
14
  # @return [Boolean]
15
15
  def machine_tag?
16
- @info['machine_tag'].to_i == 1 if @info['machine_tag']
16
+ @hash['machine_tag'].to_i == 1 if @hash['machine_tag']
17
17
  end
18
18
 
19
19
  # @return [Flickrie::User]
20
20
  def author
21
- User.new('nsid' => @info['author']) if @info['author']
21
+ User.new({'nsid' => @hash['author']}, @api_caller) if @hash['author']
22
22
  end
23
23
 
24
- def [](key) @info[key] end
24
+ def [](key) @hash[key] end
25
25
  # @return [Hash]
26
- def hash() @info end
26
+ def hash() @hash end
27
27
 
28
28
  private
29
29
 
30
- def initialize(info)
31
- @info = info
30
+ def initialize(hash, api_caller)
31
+ @hash = hash
32
+ @api_caller = api_caller
32
33
  end
33
34
 
34
35
  def to_s
@@ -0,0 +1,28 @@
1
+ module Flickrie
2
+ module Media
3
+ class Visibility
4
+ # @!parse attr_reader \
5
+ # :public?, :friends?, :family?, :contacts?, :hash
6
+
7
+ # @return [Boolean]
8
+ def public?() @hash['ispublic'].to_i == 1 if @hash['ispublic'] end
9
+ # @return [Boolean]
10
+ def friends?() @hash['isfriend'].to_i == 1 if @hash['isfriend'] end
11
+ # @return [Boolean]
12
+ def family?() @hash['isfamily'].to_i == 1 if @hash['isfamily'] end
13
+ # @return [Boolean]
14
+ def contacts?() @hash['iscontact'].to_i == 1 if @hash['iscontact'] end
15
+
16
+ def [](key) @hash[key] end
17
+ # @return [Hash]
18
+ def hash() @hash end
19
+
20
+ private
21
+
22
+ def initialize(hash)
23
+ raise ArgumentError if hash.nil?
24
+ @hash = hash
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module Flickrie
2
+ class MediaContext
3
+ # @return [Fixnum]
4
+ attr_reader :count
5
+ # @return [Flickrie::Photo, Flickrie::Video]
6
+ attr_reader :previous
7
+ # @return [Flickrie::Photo, Flickrie::Video]
8
+ attr_reader :next
9
+
10
+ def initialize(hash, api_caller)
11
+ count = hash['count']['_content'].to_i
12
+ previous = Media.new(hash['prevphoto'], api_caller) rescue nil
13
+ next_ = Media.new(hash['nextphoto'], api_caller) rescue nil
14
+ @count, @previous, @next = count, previous, next_
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ require 'date'
2
+
3
+ module Flickrie
4
+ class MediaCount
5
+ # @!parse attr_reader \
6
+ # :value, :date_range, :from, :to, :hash
7
+
8
+ def value() Integer(@hash['count']) rescue nil end
9
+
10
+ # @return [Range]
11
+ def date_range
12
+ dates = []
13
+ ['fromdate', 'todate'].each do |key|
14
+ if @hash[key] == @hash[key].to_i.to_s
15
+ dates << Time.at(Integer(@hash[key]))
16
+ else
17
+ dates << DateTime.parse(@hash[key]).to_time
18
+ end
19
+ end
20
+
21
+ dates.first..dates.last
22
+ end
23
+ alias time_interval date_range
24
+
25
+ # @return [Time]
26
+ def from() date_range.begin end
27
+ # @return [Time]
28
+ def to() date_range.end end
29
+
30
+ def [](key) @hash[key] end
31
+ # Returns the raw hash from the response. Useful if something isn't available by methods.
32
+ #
33
+ # @return [Hash]
34
+ def hash() @hash end
35
+
36
+ private
37
+
38
+ def initialize(hash)
39
+ @hash = hash
40
+ end
41
+
42
+ def self.new_collection(hash)
43
+ hash['photocount'].map { |info| new(info) }
44
+ end
45
+ end
46
+ end
@@ -116,14 +116,13 @@ module Flickrie
116
116
  #
117
117
  # @return [self]
118
118
  def get_sizes(params = {})
119
- photo = Flickrie.get_photo_sizes(id, params)
120
- @hash.deep_merge!(photo.hash)
119
+ @hash.deep_merge!(@api_caller.get_photo_sizes(id, params).hash)
121
120
  largest!
122
121
  end
123
122
 
124
123
  private
125
124
 
126
- def initialize(info = {})
125
+ def initialize(*args)
127
126
  super
128
127
  @size = largest_size
129
128
  end
@@ -39,25 +39,25 @@ module Flickrie
39
39
  end
40
40
 
41
41
  # @return [Flickrie::User]
42
- def owner() User.new('nsid' => @hash['owner']) if @hash['owner'] end
42
+ def owner() User.new({'nsid' => @hash['owner']}, @api_caller) if @hash['owner'] end
43
43
 
44
44
  # Same as calling `Flickrie.photos_from_set(set.id)`.
45
45
  #
46
46
  # @return [Flickrie::Collection<Flickrie::Photo>]
47
47
  def photos(params = {})
48
- Flickrie.photos_from_set(id, params)
48
+ @api_caller.photos_from_set(id, params)
49
49
  end
50
50
  # Same as calling `Flickrie.videos_from_set(set.id)`.
51
51
  #
52
52
  # @return [Flickrie::Collection<Flickrie::Video>]
53
53
  def videos(params = {})
54
- Flickrie.videos_from_set(id, params)
54
+ @api_caller.videos_from_set(id, params)
55
55
  end
56
56
  # Same as calling `Flickrie.media_from_set(set.id)`.
57
57
  #
58
58
  # @return [Flickrie::Collection<Flickrie::Photo, Flickrie::Video>]
59
59
  def media(params = {})
60
- Flickrie.media_from_set(id, params)
60
+ @api_caller.media_from_set(id, params)
61
61
  end
62
62
 
63
63
  # @return [Boolean]
@@ -84,40 +84,19 @@ module Flickrie
84
84
  #
85
85
  # @return [self]
86
86
  def get_info(params = {})
87
- hash ||= Flickrie.client.get_set_info(id, params).body['photoset']
88
- self.class.fix_info(hash)
89
- @hash.update(hash)
90
-
87
+ @hash.deep_merge!(@api_caller.get_set_info(id, params).hash)
91
88
  self
92
89
  end
93
90
 
94
91
  private
95
92
 
96
- def initialize(hash = {})
93
+ def initialize(hash, api_caller)
97
94
  @hash = hash
95
+ @api_caller = api_caller
98
96
  end
99
97
 
100
- def self.from_info(hash)
101
- fix_info(hash)
102
- new(hash)
103
- end
104
-
105
- def self.fix_info(hash)
106
- hash['title'] = hash['title']['_content']
107
- hash['description'] = hash['description']['_content']
108
- end
109
-
110
- def self.from_user(hash, user_nsid)
111
- collection = hash.delete('photoset').map do |set_hash|
112
- set_hash['count_photos'] = set_hash.delete('photos')
113
- set_hash['count_videos'] = set_hash.delete('videos')
114
- set_hash['title'] = set_hash['title']['_content']
115
- set_hash['description'] = set_hash['description']['_content']
116
- set_hash['owner'] = user_nsid
117
-
118
- new(set_hash)
119
- end
120
-
98
+ def self.new_collection(hash, api_caller)
99
+ collection = hash.delete('photoset').map { |info| new(info, api_caller) }
121
100
  Collection.new(hash).replace(collection)
122
101
  end
123
102
  end