flickrie 0.7.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +4 -0
  3. data/CHANGELOG.md +70 -30
  4. data/Gemfile +5 -4
  5. data/README.md +55 -53
  6. data/Rakefile +6 -9
  7. data/lib/flickrie/api_methods.rb +16 -3
  8. data/lib/flickrie/client.rb +11 -40
  9. data/lib/flickrie/instance.rb +0 -1
  10. data/lib/flickrie/location.rb +21 -18
  11. data/lib/flickrie/media.rb +5 -21
  12. data/lib/flickrie/media_count.rb +4 -0
  13. data/lib/flickrie/middleware.rb +71 -0
  14. data/lib/flickrie/oauth.rb +58 -49
  15. data/lib/flickrie/photo.rb +0 -2
  16. data/lib/flickrie/set.rb +0 -5
  17. data/lib/flickrie/upload_client.rb +45 -56
  18. data/lib/flickrie/user.rb +10 -10
  19. data/lib/flickrie/version.rb +1 -1
  20. data/lib/flickrie/video.rb +0 -2
  21. data/spec/files/photo.bla +0 -0
  22. data/spec/{api_methods_spec.rb → flickrie/api_methods_spec.rb} +14 -14
  23. data/spec/{error_spec.rb → flickrie/error_spec.rb} +1 -1
  24. data/spec/{instance_spec.rb → flickrie/instance_spec.rb} +2 -5
  25. data/spec/{license_spec.rb → flickrie/license_spec.rb} +1 -1
  26. data/spec/{location_spec.rb → flickrie/location_spec.rb} +12 -1
  27. data/spec/{media_count_spec.rb → flickrie/media_count_spec.rb} +7 -3
  28. data/spec/{media_spec.rb → flickrie/media_spec.rb} +25 -26
  29. data/spec/flickrie/oauth_spec.rb +25 -0
  30. data/spec/{photo_spec.rb → flickrie/photo_spec.rb} +1 -1
  31. data/spec/{set_spec.rb → flickrie/set_spec.rb} +2 -2
  32. data/spec/flickrie/upload_client_spec.rb +40 -0
  33. data/spec/flickrie/user_spec.rb +58 -0
  34. data/spec/{video_spec.rb → flickrie/video_spec.rb} +1 -1
  35. data/spec/spec_helper.rb +16 -13
  36. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/api_methods/asynchronous_upload.yml +60 -95
  37. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/api_methods/replace.yml +16 -16
  38. data/spec/{fixtures/vcr_cassettes/api_methods/tags.yml → vcr_cassettes/api_methods/tag_manipulation.yml} +24 -24
  39. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/api_methods/upload_and_delete.yml +21 -21
  40. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/error/a_request_was_made_and_failed.yml +9 -9
  41. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/instance/calling_api_methods.yml +32 -32
  42. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/license/get.yml +4 -4
  43. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/from_contacts.yml +8 -8
  44. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/from_set.yml +5 -5
  45. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/from_user.yml +5 -5
  46. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/get_context.yml +4 -4
  47. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/get_exif.yml +8 -8
  48. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/get_info.yml +18 -18
  49. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/public_from_user.yml +5 -5
  50. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media/search.yml +5 -5
  51. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/media_count/get.yml +8 -8
  52. data/spec/vcr_cassettes/o_auth/incorrect_credentials.yml +249 -0
  53. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/photo/get_info.yml +6 -6
  54. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/photo/get_sizes.yml +12 -12
  55. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/photo/search.yml +5 -5
  56. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/set/from_user.yml +16 -16
  57. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/set/get_info.yml +32 -32
  58. data/spec/vcr_cassettes/upload_client/content_type.yml +117 -0
  59. data/spec/vcr_cassettes/upload_client/invalid_credentials.yml +208 -0
  60. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/user/find_by_username_or_email.yml +8 -8
  61. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/user/get_info.yml +8 -8
  62. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/video/get_info.yml +4 -4
  63. data/spec/{fixtures/vcr_cassettes → vcr_cassettes}/video/get_sizes.yml +8 -8
  64. metadata +105 -99
  65. data/spec/fixtures/vcr_cassettes/api_methods/tag_manipulation.yml +0 -178
  66. data/spec/fixtures/vcr_cassettes/error/code.yml +0 -84
  67. data/spec/fixtures/vcr_cassettes/error/was_raised.yml +0 -84
  68. data/spec/fixtures/vcr_cassettes/o_auth/incorrect_credentials.yml +0 -167
  69. data/spec/oauth_spec.rb +0 -22
  70. data/spec/user_spec.rb +0 -53
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ wiki/
6
6
  .DS_Store
7
7
  api_methods.md
8
8
  .bundle/
9
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/CHANGELOG.md CHANGED
@@ -1,82 +1,122 @@
1
1
  # Flickrie changelog
2
2
 
3
+ ## Version 1.0.0
4
+
5
+ - Changes that are backwards compatible:
6
+
7
+ -- Fixed content type not being passed to the file you're uploading
8
+ if you specified it directly.
9
+
10
+ -- The request will now be retried once more if it timeouts
11
+
12
+ -- In authentication:
13
+ --- you can now call `request_token.authorize_url` instead of `request_token.get_authorization_url`.
14
+ --- you can now call `request_token.get_access_token(code)` instead of `Flickrie::OAuth.get_access_token(code, request_token)`.
15
+ --- you also get the infomation about the user who just authenticated,
16
+ which you can then access with `access_token.user_info`
17
+ (it's a Hash with keys `:fullname`, `:user_nsid` and `:username`)
18
+
19
+ -- When calling `Flickrie.get_photos_counts`, the `Flickrie::MediaCount`
20
+ now also has attributes `#time_interval` (alias for `#date_range`),
21
+ `#from` and `#to`.
22
+
23
+ - Changes that are **not** backwards compatible:
24
+
25
+ -- If you're passing in the content type of the file you're uploading,
26
+ the parameter is now called `:content_type`, not `:mime_type`.
27
+
28
+ -- If there is a problem in obtaining the access token, `Flickrie::Error`
29
+ is now raised, instead of `Flickrie::OAuth::Error`.
30
+
31
+ -- When you're calling `request_token.get_authorization_url`, if you want to
32
+ specifiy permissions, you now have to pass the `:perms` option,
33
+ instead of `:permissions`. In this way you can pass any parameter,
34
+ and it will be appended to the URL (in case Flickr adds a new parameter).
35
+
36
+ -- When you have a `Flickrie::User` instance, the
37
+ `Flickrie::User#time_zone` now returns a struct with `#label` and
38
+ `#offset` attributes (before it returned a `Hash` with those keys).
39
+
40
+ -- When you call `Flickrie.get_media_context`, the result is now a
41
+ struct with attributes `#count`, `#previous`, `#next`.
42
+
3
43
  ## Version 0.7.3
4
44
 
5
- - covered "flickr.people.getPhotos" (I released it as a patch because
6
- it's important)
45
+ - Covered "flickr.people.getPhotos" (I released it as a patch because
46
+ it's important).
7
47
 
8
48
  ## Version 0.7.2
9
49
 
10
- - specified versions of dependencies and Ruby accurately
50
+ - Specified versions of dependencies and Ruby accurately.
11
51
 
12
52
  ## Version 0.7.1
13
53
 
14
- - fixed an oauth bug
15
- - added `#hash` method to photos, sets etc. which you can call to get the
54
+ - Fixed an oauth bug.
55
+ - Added `#hash` method to photos, sets etc. which you can call to get the
16
56
  raw data (in Ruby hash). This is useful if I accidentally left something out,
17
57
  or if Flickr added something new that I didn't cover yet, that you can
18
58
  still get to the data. `#[]` reads from the same hash.
19
59
 
20
60
  ## Version 0.7.0
21
61
 
22
- - covered `flickr.photos.getFavorites` and `flickr.test.login`
23
- - bug fixes (especially regarding the `Flickr::Instance`)
62
+ - Covered `flickr.photos.getFavorites` and `flickr.test.login`.
63
+ - Bug fixes (especially regarding the `Flickr::Instance`).
24
64
 
25
65
  ## Version 0.6.1
26
66
 
27
- - `Flickrie::Media::Ticket` is now called just `Flickrie::Ticket`
67
+ - `Flickrie::Media::Ticket` is now called just `Flickrie::Ticket`.
28
68
 
29
69
  ## Version 0.6.0
30
70
 
31
- - you can access the raw response hash with square brackets, if you notice I
71
+ - You can access the raw response hash with square brackets, if you notice I
32
72
  didn't cover some part of it with methods. So, for example, after calling
33
- `Flickrie.get_photo_info`, you can get the photo ID by calling `photo['id']`
73
+ `Flickrie.get_photo_info`, you can get the photo ID by calling `photo['id']`.
34
74
 
35
- - when `Flickrie::Error` is raised, you can now access its `#code`
75
+ - When `Flickrie::Error` is raised, you can now access its `#code`
36
76
  attribute. This makes better error handling, because error code
37
- is (supposed to be) unique, unlike the error message
77
+ is (supposed to be) unique, unlike the error message.
38
78
 
39
79
  ## Version 0.5.2
40
80
 
41
- - the gem now also works with Faraday 0.7 (I apologize for not
42
- making Faraday >= 0.8 a dependency before)
81
+ - The gem now also works with Faraday 0.7 (I apologize for not
82
+ making Faraday >= 0.8 a dependency before).
43
83
 
44
84
  ## Version 0.5.1
45
85
 
46
- - fixed a documentation error
86
+ - Fixed a documentation error.
47
87
 
48
88
  ## Version 0.5.0
49
89
 
50
- - covered `flickr.photos.getCounts`
51
- - covered `flickr.photos.getExif`
90
+ - Covered `flickr.photos.getCounts`.
91
+ - Covered `flickr.photos.getExif`.
52
92
 
53
93
  ## Version 0.4.1
54
94
 
55
- - handle file uploads for Rails and Sinatra smarter (see [this wiki](https://github.com/janko-m/flickrie/wiki/Some-tips.md), tip #7)
95
+ - Handle file uploads for Rails and Sinatra smarter (see [this wiki](https://github.com/janko-m/flickrie/wiki/Some-tips.md), tip #7)
56
96
 
57
97
  ## Version 0.4.0
58
98
 
59
- - covered `flickr.photos.getContactsPhotos`
60
- - covered `flickr.photos.getContactsPublicPhotos`
61
- - covered `flickr.photos.getContext`
62
- - implemented uploading photos
63
- - covered `flickr.photos.upload.checkTickets`
99
+ - Covered `flickr.photos.getContactsPhotos`.
100
+ - Covered `flickr.photos.getContactsPublicPhotos`.
101
+ - Covered `flickr.photos.getContext`.
102
+ - Implemented uploading photos.
103
+ - Covered `flickr.photos.upload.checkTickets`.
64
104
 
65
105
  ## Version 0.3.2
66
106
 
67
- - enabled passing params to `Flickrie::Set#photos`
107
+ - Enabled passing params to `Flickrie::Set#photos`.
68
108
 
69
109
  ## Version 0.3.1
70
110
 
71
- - fixed an error when accessing tags from photos fetched through
72
- certain API calls
111
+ - Fixed an error when accessing tags from photos fetched through
112
+ certain API calls.
73
113
 
74
114
  ## Version 0.3.0
75
115
 
76
- - the proper error is now raised on wrong access tokens
77
- - implemented a better way of using access tokens, using `Flickrie::Instance.new(access_token, access_secret)`
78
- (see [this wiki](https://github.com/janko-m/flickrie/wiki/Authentication-in-web-applications) for an example)
116
+ - The proper error is now raised on wrong access tokens.
117
+ - Implemented a better way of using access tokens, using `Flickrie::Instance.new(access_token, access_secret)`
118
+ (see [this wiki](https://github.com/janko-m/flickrie/wiki/Authentication-in-web-applications) for an example).
79
119
 
80
120
  ## Version 0.2.2
81
121
 
82
- - removed `addressable` gem as a dependency
122
+ - Removed `addressable` gem as a dependency.
data/Gemfile CHANGED
@@ -6,7 +6,8 @@ group :test do
6
6
  gem "faraday", '0.7.6'
7
7
  end
8
8
 
9
- # You can run `bundle [install] --without debugger pry` if
10
- # you don't want to install them
11
- gem "debugger", :group => :debugger
12
- gem "pry", :group => :pry
9
+ # You can run `bundle [install] --without extras` if you don't want to install them
10
+ group :extras do
11
+ gem "debugger"
12
+ gem "pry"
13
+ end
data/README.md CHANGED
@@ -1,31 +1,38 @@
1
- # Flickrie
1
+ # Flickrie [![Build Status](https://secure.travis-ci.org/janko-m/flickrie.png)](http://travis-ci.org/janko-m/flickrie)
2
2
 
3
3
  ## About
4
4
 
5
- This gem is a nice wrapper for the Flickr API with an intuitive interface.
6
-
7
- The reason why I did this gem is because the other ones either weren't
8
- well maintained, or they were too literal in the sense that the responses from
9
- the API calls weren't processed almost at all. It doesn't seem too bad
10
- at first, but it would be great that, for example, if you're reading a time attribute,
11
- such as `last_update`, you actually **get** the instance of `Time`, and
12
- not a string representing that time (often in a timestamp (integer) format).
13
- That's what wrappers should be for.
14
-
15
- The method names here aren't called the same as Flickr's API methods, but they follow a pattern which
16
- shouldn't be too difficult to learn. Take a look at the "Covered API
17
- methods" section of this readme.
18
-
19
- Also, some attribute names that you
20
- get from the response are changed. So, for example, the `last_update`
21
- attribute is called `updated_at`, and the `candownload` attribute is
22
- called `can_download?`. After all, this is a **ruby** wrapper, so it
23
- should be written in Ruby/Rails fashion :)
24
-
25
- There are 2 things that are different from the Flickr's API documentation:
26
- - "photoset" is here just "set". This is because the word "photoset" is actually
27
- incorrect, since sets can also hold videos.
28
- - "person" is here refered to as "user".
5
+ This gem is a nice wrapper for the Flickr API with an object-oriented interface.
6
+
7
+ The reason why I did this gem is because the existing ones either weren't
8
+ well maintained, or they weren't object oriented and provided a literal
9
+ wrapper over the API. That didn't statisfy me, because Flickr API
10
+ sometimes gives you the same information in different ways. And I want
11
+ to normalize that. Also, JSON responses can return "time" attributes
12
+ only in the string format. And I thought it would be nice that, if you're
13
+ accessing the `updated_at` attribute for example, you actually **get**
14
+ an instance of `Time`, instead of a string, or even integer. These are
15
+ just some of the reason I decided to make this gem.
16
+
17
+ The method names here aren't called the same as Flickr's API methods
18
+ (and they can't be), but they follow a pattern which
19
+ shouldn't be too difficult to predict. Take a look at the ["Currently covered API
20
+ methods"](https://github.com/janko-m/flickrie#currently-covered-api-methods) section of this readme.
21
+
22
+ Also, some attribute names that you get from the response are changed.
23
+ So, for example, the `last_update` attribute is called `updated_at`,
24
+ and the `candownload` attribute is called `can_download?`. Because this
25
+ is a **ruby** wrapper, I wanted to write it in Ruby/Rails fashion. At
26
+ first you might not approve of this, because then you might not know
27
+ what the methods name are if you know the name of the key in the
28
+ response hash. But, on the other hand, Flickr often gives you the same
29
+ information in different formats, so this also acts as a normalizer. The
30
+ downsize of this approach is that it may not be up to date when the
31
+ Flickr API changes, but that's why I have **you** to open a new issue
32
+ when you notice something isn't working :)
33
+
34
+ The gem works on Ruby versions `1.9.2` and `1.9.3`. Also, it follows
35
+ [Semantic versioning](http://semver.org/).
29
36
 
30
37
  ## Examples of usage
31
38
 
@@ -35,16 +42,25 @@ You first need to install the gem.
35
42
  [sudo] gem install flickrie
36
43
  ```
37
44
 
45
+ Then, if you're using Bundler in your project, put it into your `Gemfile`:
46
+
47
+ ```ruby
48
+ gem "flickrie"
49
+ ```
50
+
38
51
  Then in your app you set the API key and shared secret (if you don't have them
39
52
  already, you can apply for them [here](http://www.flickr.com/services/apps/create/apply)).
40
53
 
41
54
  ```ruby
42
55
  require 'flickrie'
56
+
43
57
  Flickrie.api_key = "API_KEY"
44
58
  Flickrie.shared_secret = "SHARED_SECRET"
45
59
  ```
46
60
 
47
- Then you can search for stuff.
61
+ If your in Rails, it's a good idea to put this into an initializer.
62
+
63
+ Now you can search for stuff.
48
64
 
49
65
  ```ruby
50
66
  set_id = 819234
@@ -64,7 +80,7 @@ You can also throw in some parameters to `Flickrie.photos_from_set` to get more
64
80
  photos = Flickrie.photos_from_set(819234, :extras => 'owner_name,last_update,tags,views')
65
81
 
66
82
  photo = photos.first
67
- photo.tags # => "cave cold forrest"
83
+ photo.tags.join(' ') # => "cave cold forrest"
68
84
  photo.owner.username # => "jsmith"
69
85
  photo.updated_at # => 2012-04-20 23:29:17 +0200
70
86
  photo.views_count # => 24
@@ -86,7 +102,7 @@ photo.owner.real_name # => "John Smith"
86
102
  photo.location.country.name # => "United States"
87
103
  ```
88
104
 
89
- If you already have an existing photo, you can also get info like this:
105
+ If you already have an existing photo (providing it holds its ID), you can also get info like this:
90
106
 
91
107
  ```ruby
92
108
  photo.description # => nil
@@ -133,15 +149,17 @@ Flickrie.api_key = "API_KEY"
133
149
  Flickrie.shared_secret = "SHARED_SECRET"
134
150
 
135
151
  request_token = Flickrie::OAuth.get_request_token
136
- url = request_token.get_authorization_url
152
+ url = request_token.authorize_url
137
153
  puts "Visit this url to authenticate: #{url}"
138
154
 
139
155
  print "If you agreed, the code was displayed afterwards. Enter it: "
140
156
  code = gets.strip
141
- access_token = Flickrie::OAuth.get_access_token(code, request_token)
157
+ access_token = request_token.get_access_token(code)
158
+ puts "You successfully authenticated!"
159
+
142
160
  Flickrie.access_token = access_token.token
143
161
  Flickrie.access_secret = access_token.secret
144
- puts "You successfully authenticated!"
162
+ access_token.user_params # => {:fullname => "John Smith", :user_nsid => "67131352@N03", :username => "jsmith"}
145
163
  ```
146
164
 
147
165
  When getting the authorization url, you can also call
@@ -151,8 +169,11 @@ request_token.get_authorization_url(:permissions => "read")
151
169
  to ask only for "read" permissions from the user. Available permissions
152
170
  are "read", "write" and "delete".
153
171
 
154
- If you want to make authentication in your web application, check out my [flickr_auth](https://github.com/janko-m/flickr_auth) gem.
155
- Or, if you want to do it manually, check out [this wiki](https://github.com/janko-m/flickrie/wiki/Authentication-in-web-applications) for instructions.
172
+ If you want to make authentication in your web application, check out my
173
+ [flickr_auth](https://github.com/janko-m/flickr_auth) gem.
174
+ Or, if you want to do it manually, check out
175
+ [this wiki](https://github.com/janko-m/flickrie/wiki/Authentication-in-web-applications)
176
+ for instructions.
156
177
 
157
178
  ## Photo upload
158
179
 
@@ -165,9 +186,7 @@ photo.title # => "A cow"
165
186
  For the list of parameters you can pass in when uploading a photo, see
166
187
  [this page](http://www.flickr.com/services/api/upload.api.html).
167
188
 
168
- Few notes:
169
- - Photo uploads require authentication with "write" permissions.
170
- - Path to the photo has to be absolute.
189
+ Note that photo uploads require authentication with "write" permissions.
171
190
 
172
191
  See [this wiki](https://github.com/janko-m/flickrie/wiki/Asynchronous-photo-upload) for an example
173
192
  of an asynchronous photo upload.
@@ -214,23 +233,6 @@ default.
214
233
  Please, feel free to post any issues that you're having, I will be happy
215
234
  to help. I will also be happy if you let me know about any bugs.
216
235
 
217
- ## How to contribute
218
-
219
- Nothing special, you just fork the project, and send me a pull request.
220
- Don't forget to cover the change with tests. If you don't know how to
221
- write tests, send the pull requests without them, it's ok.
222
-
223
- An important note: if you want to add a line in the tests which makes a **new** HTTP request,
224
- you'll have to get your own API key and access token. You can get the
225
- API key and secret [here](http://www.flickr.com/services/apps/create/apply)
226
- (assuming you already have a Flickr account), and you can then obtain an access token by using my
227
- authentication system (described above). You then have to assign these
228
- values to the environment variables `FLICKR_API_KEY`, `FLICKR_SHARED_SECRET`,
229
- `FLICKR_ACCESS_TOKEN` and `FLICKR_ACCESS_SECRET`. That's it, you should
230
- be able to run the tests normally now.
231
-
232
- Feel free to email me if you have any problems.
233
-
234
236
  ## Cedits
235
237
 
236
238
  Special thanks to @**mislav**, my brother, he helped me really a lot
data/Rakefile CHANGED
@@ -1,15 +1,17 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
+ task :default => :spec
5
+
4
6
  desc "Run the specs (use spec:name to run a single spec)"
5
7
  task :spec do |task, args|
6
- system "bundle exec rspec --color"
8
+ system "rspec -Ispec"
7
9
  end
8
10
 
9
11
  Dir["spec/*_spec.rb"].each do |spec|
10
12
  task_name = File.basename(spec)[/.+(?=_spec\.rb)/]
11
13
  task :"spec:#{task_name}" do
12
- system "bundle exec rspec #{spec} --color"
14
+ system "rspec -Ispec #{spec}"
13
15
  end
14
16
  end
15
17
 
@@ -25,18 +27,13 @@ task :console do
25
27
  end
26
28
  begin
27
29
  require 'pry'
28
- system "bundle exec pry --require 'flickrie' --require './credentials'"
30
+ system "pry --require 'flickrie' --require './credentials'"
29
31
  rescue LoadError
30
- system "bundle exec irb -r 'flickrie' -r './credentials'"
32
+ system "irb -r 'flickrie' -r './credentials'"
31
33
  end
32
34
  FileUtils.remove_file "credentials.rb"
33
35
  end
34
36
 
35
- task :rdoc do
36
- FileUtils.rm_rf "doc"
37
- system "rdoc lib/"
38
- end
39
-
40
37
  # copied from Rails
41
38
  begin
42
39
  require 'rails/source_annotation_extractor'
@@ -1,3 +1,4 @@
1
+ require 'flickrie/middleware'
1
2
  require 'flickrie/client'
2
3
  require 'flickrie/upload_client'
3
4
  require 'flickrie/oauth'
@@ -20,7 +21,6 @@ module Flickrie
20
21
  # photo.title # => "Me and Jessica"
21
22
  #
22
23
  # If the <tt>:async => 1</tt> option is passed, returns the ticket ID.
23
- #
24
24
  def upload(media, params = {})
25
25
  response = upload_client.upload(media, params)
26
26
  if params[:async] == 1
@@ -39,7 +39,6 @@ module Flickrie
39
39
  # photo.title # => "Me and Jessica"
40
40
  #
41
41
  # If the <tt>:async => 1</tt> option is passed, returns the ticket ID.
42
- #
43
42
  def replace(media, media_id, params = {})
44
43
  response = upload_client.replace(media, media_id, params)
45
44
  if params[:async] == 1
@@ -131,6 +130,12 @@ module Flickrie
131
130
  select { |media| media.is_a?(Photo) }
132
131
  end
133
132
 
133
+ # ==== Example
134
+ #
135
+ # context = Flickrie.get_photo_context(37124234)
136
+ # context.count # => 23
137
+ # context.previous # => #<Photo: id=2433240, ...>
138
+ # context.next # => #<Video: id=1282404, ...>
134
139
  def get_media_context(media_id, params = {})
135
140
  response = client.get_media_context(media_id, params)
136
141
  Media.from_context(response.body)
@@ -138,6 +143,7 @@ module Flickrie
138
143
  alias get_photo_context get_media_context
139
144
  alias get_video_context get_media_context
140
145
 
146
+ # Returns instances of Flickrie::MediaCount
141
147
  def get_media_counts(params = {})
142
148
  response = client.get_media_counts \
143
149
  MediaCount.ensure_utc(params)
@@ -147,19 +153,23 @@ module Flickrie
147
153
  alias get_photos_counts get_media_counts
148
154
  alias get_videos_counts get_media_counts
149
155
 
156
+ # See Flickrie::Media#exif for details
150
157
  def get_photo_exif(photo_id, params = {})
151
158
  response = client.get_media_exif(photo_id, params)
152
159
  Photo.from_exif(response.body['photo'])
153
160
  end
161
+ # See Flickrie::Media#exif for details
154
162
  def get_video_exif(video_id, params = {})
155
163
  response = client.get_media_exif(video_id, params)
156
164
  Video.from_exif(response.body['photo'])
157
165
  end
158
166
 
167
+ # Returns instances of Flickrie::User
159
168
  def get_photo_favorites(photo_id, params = {})
160
169
  response = client.get_media_favorites(photo_id, params)
161
170
  Photo.new(response.body['photo'])
162
171
  end
172
+ # Returns instances of Flickrie::User
163
173
  def get_video_favorites(video_id, params = {})
164
174
  response = client.get_media_favorites(video_id, params)
165
175
  Video.new(response.body['photo'])
@@ -169,14 +179,15 @@ module Flickrie
169
179
  response = client.get_media_info(media_id, params)
170
180
  Media.from_info(response.body['photo'])
171
181
  end
172
-
173
182
  alias get_photo_info get_media_info
174
183
  alias get_video_info get_media_info
175
184
 
185
+ # Returns an instance of Flickrie::Photo
176
186
  def get_photo_sizes(photo_id, params = {})
177
187
  response = client.get_media_sizes(photo_id, params)
178
188
  Photo.from_sizes(response.body['sizes'])
179
189
  end
190
+ # Returns an instance of Flickrie::Video
180
191
  def get_video_sizes(video_id, params = {})
181
192
  response = client.get_media_sizes(video_id, params)
182
193
  Video.from_sizes(response.body['sizes'])
@@ -244,6 +255,8 @@ module Flickrie
244
255
 
245
256
  #--
246
257
  # test
258
+ #++
259
+ # Returns an instance of Flickrie::User
247
260
  def test_login(params = {})
248
261
  response = client.test_login(params)
249
262
  User.from_test(response.body['user'])
@@ -1,10 +1,8 @@
1
1
  require 'faraday_middleware'
2
2
 
3
3
  module Flickrie
4
- DEFAULTS = {
5
- :open_timeout => 4,
6
- :timeout => 6
7
- }.freeze
4
+ OPEN_TIMEOUT = 4
5
+ TIMEOUT = 6
8
6
 
9
7
  class << self
10
8
  def self.attr_accessor_with_client_reset(*attributes) # :nodoc:
@@ -25,24 +23,24 @@ module Flickrie
25
23
  #
26
24
  # Flickrie.client.get "flickr.photos.licenses.getInfo"
27
25
  # Flickrie.client.post "flickr.photos.licenses.setLicense", :photo_id => 1241497, :license_id => 2
28
- #
29
26
  def client
30
27
  @client ||= new_client
31
28
  end
32
29
 
33
30
  def new_client(access_token_hash = {}) # :nodoc:
34
- Client.new(params) do |conn|
35
- conn.use FaradayMiddleware::OAuth,
31
+ Client.new(params) do |b|
32
+ b.use Middleware::Retry
33
+ b.use FaradayMiddleware::OAuth,
36
34
  :consumer_key => api_key,
37
35
  :consumer_secret => shared_secret,
38
36
  :token => access_token_hash[:token] || access_token,
39
37
  :token_secret => access_token_hash[:secret] || access_secret
40
38
 
41
- conn.use StatusCheck
42
- conn.use FaradayMiddleware::ParseJson, :content_type => /(text\/plain)|(json)$/
43
- conn.use OAuthStatusCheck
39
+ b.use Middleware::StatusCheck
40
+ b.use FaradayMiddleware::ParseJson
41
+ b.use Middleware::OAuthCheck
44
42
 
45
- conn.adapter :net_http
43
+ b.adapter :net_http
46
44
  end
47
45
  end
48
46
 
@@ -57,40 +55,13 @@ module Flickrie
57
55
  :api_key => api_key
58
56
  },
59
57
  :request => {
60
- :open_timeout => open_timeout || DEFAULTS[:open_timeout],
61
- :timeout => timeout || DEFAULTS[:timeout]
58
+ :open_timeout => open_timeout || OPEN_TIMEOUT,
59
+ :timeout => timeout || TIMEOUT
62
60
  }
63
61
  }
64
62
  end
65
63
  end
66
64
 
67
- class Error < StandardError
68
- attr_reader :code
69
-
70
- def initialize(message, code = nil)
71
- super(message)
72
- @code = code.to_i
73
- end
74
- end
75
-
76
- class StatusCheck < Faraday::Response::Middleware # :nodoc:
77
- def on_complete(env)
78
- if env[:body]['stat'] != 'ok'
79
- raise Error.new(env[:body]['message'], env[:body]['code']),
80
- env[:body]['message']
81
- end
82
- end
83
- end
84
-
85
- class OAuthStatusCheck < Faraday::Response::Middleware # :nodoc:
86
- def on_complete(env)
87
- if env[:status] != 200
88
- message = env[:body][/(?<=oauth_problem=)[^&]+/]
89
- raise Error, message.gsub('_', ' ').capitalize
90
- end
91
- end
92
- end
93
-
94
65
  class Client < Faraday::Connection # :nodoc:
95
66
  def get(method, params = {})
96
67
  super() do |req|
@@ -12,7 +12,6 @@ module Flickrie
12
12
  #
13
13
  # flickrie.client.get "flickr.photos.licenses.getInfo"
14
14
  # flickrie.client.post "flickr.photos.licenses.setLicense", :photo_id => 1241497, :license_id => 2
15
- #
16
15
  def client
17
16
  @client ||= Flickrie.new_client(access_token_hash)
18
17
  end
@@ -1,12 +1,18 @@
1
1
  module Flickrie
2
2
  class Location
3
- attr_reader :latitude, :longitude, :accuracy, :context, :place_id, :woeid
3
+ def latitude() @info['latitude'] end
4
+ def longitude() @info['longitude'] end
5
+ def accuracy() @info['accuracy'] end
6
+ def context() Integer(@info['context']) rescue nil end
4
7
 
5
- def neighbourhood() place('neighbourhood') end
6
- def locality() place('locality') end
7
- def county() place('county') end
8
- def region() place('region') end
9
- def country() place('country') end
8
+ def neighbourhood() new_area('neighbourhood') end
9
+ def locality() new_area('locality') end
10
+ def county() new_area('county') end
11
+ def region() new_area('region') end
12
+ def country() new_area('country') end
13
+
14
+ def place_id() @info['place_id'] end
15
+ def woeid() @info['woeid'] end
10
16
 
11
17
  def [](key) @info[key] end
12
18
  def hash() @info end
@@ -15,21 +21,18 @@ module Flickrie
15
21
 
16
22
  def initialize(info = {})
17
23
  raise ArgumentError if info.nil?
18
-
19
24
  @info = info
20
-
21
- %w[latitude longitude accuracy place_id woeid].each do |attribute|
22
- instance_variable_set "@#{attribute}", @info[attribute]
23
- end
24
- @context = Integer(@info['context']) rescue nil
25
25
  end
26
26
 
27
- def place(place_name)
28
- if @info[place_name]
29
- Struct.new(:name, :place_id, :woeid).new \
30
- @info[place_name]['_content'],
31
- @info[place_name]['place_id'],
32
- @info[place_name]['woeid']
27
+ def new_area(area_name)
28
+ if @info[area_name]
29
+ area_class = Class.new(Struct.new(:name, :place_id, :woeid)) do
30
+ def to_s
31
+ name
32
+ end
33
+ end
34
+ info = @info[area_name]
35
+ area_class.new(info['_content'], info['place_id'], info['woeid'])
33
36
  end
34
37
  end
35
38
  end