flickrie 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,10 +5,19 @@
5
5
  This gem is a nice wrapper for the Flickr API with an intuitive interface.
6
6
 
7
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 response from
9
- the API call wasn't processed almost at all. It doesn't seem too bad
10
- at first, but after a while you realize it's not pretty. So I wanted to
11
- make it pretty :)
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. Also, some attribute names that you
17
+ get from the response are changed. So, for example, the `last_update`
18
+ attribute is called `updated_at`, and the `candownload` attribute is
19
+ called `can_download?`. After all, this is a **ruby** wrapper, so it
20
+ should be written in Ruby/Rails fashion :)
12
21
 
13
22
  ## Examples of usage
14
23
 
@@ -18,18 +27,20 @@ You first need to install the gem.
18
27
  [sudo] gem install flickrie
19
28
  ```
20
29
 
21
- Then in your app you require it and set the API key.
30
+ Then in your app you set the API key and secret (which you can apply
31
+ for [here](http://www.flickr.com/services/apps/create/apply)).
22
32
 
23
33
  ```ruby
24
34
  require 'flickrie'
25
- Flickrie.api_key = "<your_api_key>"
26
- Flickrie.shared_secret = "<your_shared_secret>"
35
+ Flickrie.api_key = "your api key"
36
+ Flickrie.shared_secret = "your shared secret"
27
37
  ```
28
38
 
29
- Then you can search for photos.
39
+ Then you can search for stuff.
30
40
 
31
41
  ```ruby
32
- photos = Flickrie.photos_from_set(819234) # => [#<Photo: id="8232348", ...>, #<Photo: id="8194318", ...>, ...]
42
+ set_id = 819234
43
+ photos = Flickrie.photos_from_set(set_id) # => [#<Photo: id="8232348", ...>, #<Photo: id="8194318", ...>, ...]
33
44
 
34
45
  photo = photos.first
35
46
  photo.id # => "8232348"
@@ -39,11 +50,11 @@ photo.owner # => #<User: nsid="67313352@N04", ...>
39
50
  photo.owner.nsid # => "67313352@N04"
40
51
  ```
41
52
 
42
- Note that what Flickr refers to as "photoset" in its documentation, I
43
- refer to as "set". This is because the word "photoset" would be wrong,
44
- since sets can also hold videos.
53
+ Note that, what Flickr refers to as "photoset" in its documentation, I
54
+ refer to as "set". This is because the word "photoset" is actually
55
+ incorrect, since sets can also hold videos.
45
56
 
46
- You can also throw in some parameters to get more information about photos. For example,
57
+ You can also throw in some parameters to `.photos_from_set` to get more information about photos. For example,
47
58
 
48
59
  ```ruby
49
60
  photos = Flickrie.photos_from_set(819234, :extras => 'owner_name,last_update,tags,views')
@@ -55,7 +66,8 @@ photo.updated_at # => 2012-04-20 23:29:17 +0200
55
66
  photo.views_count # => 24
56
67
  ```
57
68
 
58
- On the list of available parameters you can read in the [Flickr API documentation](http://www.flickr.com/services/api/), under the corresponding API method name (in the above case it's `flickr.photosets.getPhotos`).
69
+ On the list of available parameters you can read in the [Flickr API documentation](http://www.flickr.com/services/api/),
70
+ under the corresponding API method name (in the above case the method name would be `flickr.photosets.getPhotos`).
59
71
 
60
72
  You can also get additional info on a single photo:
61
73
 
@@ -70,7 +82,7 @@ photo.owner.real_name # => "John Smith"
70
82
  photo.location.country.name # => "United States"
71
83
  ```
72
84
 
73
- You can also get this info on an existing photo:
85
+ If you already have an existing photo, you can also get info like this:
74
86
 
75
87
  ```ruby
76
88
  photo.description # => nil
@@ -78,10 +90,11 @@ photo.get_info
78
90
  photo.description # => "In this photo Peter said something really funny..."
79
91
  ```
80
92
 
81
- If you want to display photos from flickr in your app, this is probably the most useful part:
93
+ You'll also probably want to display these photos in your app.
82
94
 
83
95
  ```ruby
84
96
  photo = Flickrie.get_photo_sizes(8232348)
97
+ # or "photo.get_sizes" on an existing photo
85
98
 
86
99
  photo.medium!(800)
87
100
  photo.size # => "Medium 800"
@@ -96,41 +109,42 @@ photo.width # => 240
96
109
  photo.width # => 320
97
110
  ```
98
111
 
99
- If you want sizes to be available to photos you're fetching from a set, it's not a good idea to call `#get_sizes` on each photo, because that will make an HTTP request on each photo, which can be very slow. Instead, you should pass in these options:
100
-
101
- ```ruby
102
- photos = Flickrie.photos_from_set(1242379, :extras => 'url_sq,url_q,url_t,url_s,url_n,url_m,url_z,url_c,url_l,url_o')
103
- photo = photos.first
104
- photo.medium!(640)
105
- photo.source_url # => "http://farm8.staticflickr.com/7049/6946979188_25bb44852b_z.jpg"
106
- ```
107
-
108
- To see a full list of available methods, I see the [wiki](https://github.com/janko-m/flickrie/wiki). I promise, I will document the methods properly in near future :)
112
+ You can see the full list of available methods and attributes in the
113
+ [documentation](http://rubydoc.info/gems/flickrie/0.1.2/frames).
109
114
 
110
115
  ## Authentication
111
116
 
112
117
  ```ruby
113
118
  require 'flickrie'
114
119
 
115
- Flickrie.api_key = "<your api key>"
116
- Flickrie.shared_secret = "<your shared secret>"
120
+ Flickrie.api_key = "your api key"
121
+ Flickrie.shared_secret = "your shared secret"
122
+
123
+ request_token = Flickrie::OAuth.get_request_token
124
+ url = request_token.get_authorization_url
125
+ puts "Visit this url to authenticate: #{url}"
117
126
 
118
- url = Flickrie.get_authorization_url
119
- puts "Visit this url to authorize: #{url}"
120
- puts "If you agreed, the code was displayed afterwards. Enter it: "
127
+ print "If you agreed, the code was displayed afterwards. Enter it: "
121
128
  code = gets.strip
122
- Flickrie.authorize!(code)
123
- puts "You're all done! Now go make some authenticated requests. Make me proud, son."
129
+ access_token = Flickrie::OAuth.get_access_token(code)
130
+ Flickrie.access_token = access_token.token
131
+ Flickrie.access_secret = access_token.secret
132
+ puts "You successfully authenticated!"
124
133
  ```
125
134
 
126
- When calling `Flickrie.get_authorization_url`, you can also pass in the option `:permissions => "<perms>"`, where instead of `<perms>` you write either `read`, `write` or `delete`.
135
+ When getting the authorization url, you can also call
136
+ ```ruby
137
+ request_token.get_authorization_url(:permissions => "read")
138
+ ```
139
+ to ask only for "read" permissions from the user. Available permissions
140
+ are "read", "write" and "delete".
127
141
 
128
142
  ## A few words
129
143
 
130
144
  Now, I covered only a few out of many Flickr's API methods using this approach, but I'll constantly update this gem with new API methods. For all of the methods I didn't cover, you can call them using `Flickrie.client`, like this:
131
145
 
132
146
  ```ruby
133
- response = Flickrie.client.get("flickr.photos.getContext", :photo_id => 2842732)
147
+ response = Flickrie.client.get "flickr.photos.getContext", :photo_id => 2842732
134
148
  reponse.body # =>
135
149
  # {
136
150
  # "count" => {"_content" => 99},
@@ -151,8 +165,8 @@ It's not nearly as pretty, but at least you can get to the data.
151
165
 
152
166
  ## Issues
153
167
 
154
- Please, feel free to post any issues that you're having, I will try to
155
- help you in any way I can.
168
+ Please, feel free to post any issues that you're having, I will be happy
169
+ to help. I will also be happy if you let me know about any bugs.
156
170
 
157
171
  ## Cedits
158
172
 
@@ -169,8 +183,11 @@ basis of this gem.
169
183
  - `flickr.people.getPublicPhotos`
170
184
 
171
185
  ### photos
186
+ - `flickr.photos.addTags`
187
+ - `flickr.photos.delete`
172
188
  - `flickr.photos.getInfo`
173
189
  - `flickr.photos.getSizes`
190
+ - `flickr.photos.removeTag`
174
191
  - `flickr.photos.search`
175
192
 
176
193
  ### photos.licenses
data/flickrie.gemspec CHANGED
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_dependency 'faraday_middleware'
19
19
  gem.add_dependency 'simple_oauth'
20
+ gem.add_dependency 'addressable'
20
21
  end
@@ -4,7 +4,7 @@ require 'simple_oauth'
4
4
  module Flickrie
5
5
  class << self
6
6
  attr_accessor :api_key, :shared_secret, :timeout, :open_timeout,
7
- :token, :token_secret
7
+ :access_token, :access_secret
8
8
 
9
9
  def client
10
10
  @client ||= begin
@@ -12,8 +12,8 @@ module Flickrie
12
12
  conn.request :oauth,
13
13
  :consumer_key => api_key,
14
14
  :consumer_secret => shared_secret,
15
- :token => token,
16
- :token_secret => token_secret
15
+ :token => access_token,
16
+ :token_secret => access_secret
17
17
  conn.response :json, :content_type => /(text\/plain)|(json)$/
18
18
  conn.adapter Faraday.default_adapter
19
19
  end
@@ -82,8 +82,7 @@ module Flickrie
82
82
 
83
83
  def public_media_from_user(user_nsid, params = {})
84
84
  params = {:user_id => user_nsid}.merge(params)
85
- params[:extras] = [params[:extras], 'media'].compact.join(',')
86
- get 'flickr.people.getPublicPhotos', params
85
+ get 'flickr.people.getPublicPhotos', ensure_media(params)
87
86
  end
88
87
 
89
88
  # photos
@@ -91,6 +90,10 @@ module Flickrie
91
90
  post 'flickr.photos.addTags', :photo_id => media_id, :tags => tags
92
91
  end
93
92
 
93
+ def delete_media(media_id)
94
+ post 'flickr.photos.delete', :photo_id => media_id
95
+ end
96
+
94
97
  def get_media_info(media_id)
95
98
  get 'flickr.photos.getInfo', :photo_id => media_id
96
99
  end
@@ -104,8 +107,7 @@ module Flickrie
104
107
  end
105
108
 
106
109
  def search_media(params = {})
107
- params[:extras] = [params[:extras], 'media'].compact.join(',')
108
- get 'flickr.photos.search', params
110
+ get 'flickr.photos.search', ensure_media(params)
109
111
  end
110
112
 
111
113
  # licenses
@@ -124,8 +126,14 @@ module Flickrie
124
126
 
125
127
  def media_from_set(set_id, params = {})
126
128
  params = {:photoset_id => set_id}.merge(params)
129
+ get 'flickr.photosets.getPhotos', ensure_media(params)
130
+ end
131
+
132
+ private
133
+
134
+ def ensure_media(params)
127
135
  params[:extras] = [params[:extras], 'media'].compact.join(',')
128
- get 'flickr.photosets.getPhotos', params
136
+ params
129
137
  end
130
138
  end
131
139
  end
@@ -1,79 +1,95 @@
1
+ require 'faraday_middleware'
2
+ require 'faraday_middleware/response_middleware'
3
+ require 'simple_oauth'
4
+
1
5
  module Flickrie
2
6
  module OAuth
3
- class RequestToken
4
- attr_reader :token, :secret
7
+ URL = 'http://www.flickr.com/services/oauth'.freeze
8
+ NO_CALLBACK = 'oob'.freeze
5
9
 
6
- def initialize(response_body)
7
- @token = response_body[/(?<=oauth_token=)[^&]+/]
8
- @secret = response_body[/(?<=oauth_token_secret=)[^&]+/]
9
- end
10
+ def self.new_connection(additional_oauth_params = {})
11
+ oauth_params = {
12
+ :consumer_key => Flickrie.api_key,
13
+ :consumer_secret => Flickrie.shared_secret
14
+ }.merge(additional_oauth_params)
15
+
16
+ Faraday.new(URL) do |connection|
17
+ connection.request :oauth, oauth_params
18
+ connection.use ParseFlickrResponse
19
+ connection.adapter Faraday.default_adapter
20
+ end.
21
+ tap do |connection|
22
+ connection.builder.insert_before ParseFlickrResponse, StatusCheck
23
+ end
24
+ end
10
25
 
11
- def authorization_url(options = {})
12
- url = "http://www.flickr.com/services/oauth/authorize?oauth_token=#{token}"
13
- permissions = options[:permissions] || options[:perms]
14
- url.concat("&perms=#{permissions}") if permissions
15
- url
26
+ class StatusCheck < Faraday::Response::Middleware
27
+ def on_complete(env)
28
+ if env[:status] != 200
29
+ raise Error, env[:body]['oauth_problem'].gsub('_', ' ').capitalize
30
+ end
16
31
  end
17
32
  end
18
33
 
19
- class Consumer
20
- def initialize(api_key, shared_secret)
21
- @api_key, @shared_secret = api_key, shared_secret
34
+ class Error < StandardError
35
+ end
36
+
37
+ class ParseFlickrResponse < FaradayMiddleware::ResponseMiddleware
38
+ dependency do
39
+ require 'addressable/uri' unless defined?(Addressable)
22
40
  end
23
41
 
24
- def get_request_token
25
- connection = Faraday.new "http://www.flickr.com/services/oauth" do |conn|
26
- conn.request :oauth,
27
- :consumer_key => @api_key,
28
- :consumer_secret => @shared_secret
29
- conn.adapter :net_http
30
- end
42
+ define_parser do |body|
43
+ parser = Addressable::URI.new
44
+ parser.query = body
45
+ parser.query_values
46
+ end
47
+ end
31
48
 
32
- response = connection.get("request_token") do |req|
33
- req.params[:oauth_callback] = 'oob'
34
- end
49
+ def self.get_request_token(callback = nil)
50
+ connection = new_connection
35
51
 
36
- RequestToken.new(response.body)
52
+ response = connection.get "request_token" do |req|
53
+ req.params[:oauth_callback] = callback || NO_CALLBACK
37
54
  end
38
55
 
39
- def get_access_token(oauth_verifier, request_token)
40
- connection = Faraday.new "http://www.flickr.com/services/oauth" do |conn|
41
- conn.request :oauth,
42
- :consumer_key => @api_key,
43
- :consumer_secret => @shared_secret,
44
- :token => request_token.token,
45
- :token_secret => request_token.secret
46
- conn.adapter :net_http
47
- end
56
+ RequestToken.from_response(response.body)
57
+ end
48
58
 
49
- response = connection.get "access_token" do |req|
50
- req.params[:oauth_verifier] = oauth_verifier
51
- end
59
+ def self.get_access_token(verifier, request_token)
60
+ connection = new_connection \
61
+ :token => request_token.token,
62
+ :token_secret => request_token.secret
52
63
 
53
- [
54
- response.body[/(?<=oauth_token=)[^&]+/],
55
- response.body[/(?<=oauth_token_secret=)[^&]+/]
56
- ]
64
+ response = connection.get "access_token" do |req|
65
+ req.params[:oauth_verifier] = verifier
57
66
  end
58
- end
59
- end
60
- end
61
67
 
62
- module Flickrie
63
- class << self
64
- def get_authorization_url(options = {})
65
- @request_token = consumer.get_request_token
66
- @request_token.authorization_url(options)
68
+ AccessToken.from_response(response.body)
67
69
  end
68
70
 
69
- def authorize!(code)
70
- token, token_secret = consumer.get_access_token(code, @request_token)
71
+ module Token
72
+ def from_response(body)
73
+ new(body['oauth_token'], body['oauth_token_secret'])
74
+ end
71
75
  end
72
76
 
73
- private
77
+ class RequestToken < Struct.new(:token, :secret)
78
+ extend Token
79
+
80
+ def get_authorization_url(options = {})
81
+ url = Addressable::URI.parse(URL)
82
+ url.path += "/authorize"
83
+ url.query_values = {
84
+ :oauth_token => token,
85
+ :perms => options[:permissions] || options[:perms]
86
+ }
87
+ url.to_s
88
+ end
89
+ end
74
90
 
75
- def consumer
76
- @consumer ||= OAuth::Consumer.new(api_key, shared_secret)
91
+ class AccessToken < Struct.new(:token, :secret)
92
+ extend Token
77
93
  end
78
94
  end
79
95
  end
@@ -1,3 +1,3 @@
1
1
  module Flickrie
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/flickrie.rb CHANGED
@@ -47,6 +47,12 @@ module Flickrie
47
47
  alias add_photo_tags add_media_tags
48
48
  alias add_video_tags add_media_tags
49
49
 
50
+ def delete_media(media_id)
51
+ client.delete_media(media_id)
52
+ end
53
+ alias delete_photo delete_media
54
+ alias delete_video delete_media
55
+
50
56
  def get_media_info(media_id)
51
57
  response = client.get_media_info(media_id)
52
58
  Media.from_info(response.body['photo'])
data/test/media_test.rb CHANGED
@@ -12,8 +12,8 @@ class MediaTest < Test::Unit::TestCase
12
12
  def setup
13
13
  Flickrie.api_key = ENV['FLICKR_API_KEY']
14
14
  Flickrie.shared_secret = ENV['FLICKR_SHARED_SECRET']
15
- Flickrie.token = ENV['FLICKR_TOKEN']
16
- Flickrie.token_secret = ENV['FLICKR_TOKEN_SECRET']
15
+ Flickrie.access_token = ENV['FLICKR_ACCESS_TOKEN']
16
+ Flickrie.access_secret = ENV['FLICKR_ACCESS_SECRET']
17
17
  @media_id = 6946979188
18
18
  @set_id = 72157629851991663
19
19
  @user_nsid = '67131352@N04'
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'flickrie/client'
3
+ require 'flickrie/oauth'
4
+
5
+ class OAuthTest < Test::Unit::TestCase
6
+ def test_raising_errors
7
+ Flickrie.api_key = "foo"
8
+ Flickrie.shared_secret = "foo"
9
+
10
+ assert_raises(Flickrie::OAuth::Error) do
11
+ Flickrie::OAuth.get_request_token
12
+ end
13
+
14
+ Flickrie.api_key = ENV['FLICKR_API_KEY']
15
+ assert_raises(Flickrie::OAuth::Error) do
16
+ Flickrie::OAuth.get_request_token
17
+ end
18
+
19
+ Flickrie.shared_secret = ENV['FLICKR_SHARED_SECRET']
20
+ assert_nothing_raised { Flickrie::OAuth.get_request_token }
21
+
22
+ request_token = Flickrie::OAuth.get_request_token
23
+ assert_raises(Flickrie::OAuth::Error) do
24
+ Flickrie::OAuth.get_access_token("foo", request_token)
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flickrie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday_middleware
16
- requirement: &70309320225020 !ruby/object:Gem::Requirement
16
+ requirement: &70240604302180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70309320225020
24
+ version_requirements: *70240604302180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: simple_oauth
27
- requirement: &70309320251620 !ruby/object:Gem::Requirement
27
+ requirement: &70240604300760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,18 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70309320251620
35
+ version_requirements: *70240604300760
36
+ - !ruby/object:Gem::Dependency
37
+ name: addressable
38
+ requirement: &70240604298180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70240604298180
36
47
  description: This gem is a nice wrapper for the Flickr API with an intuitive interface.
37
48
  email:
38
49
  - janko.marohnic@gmail.com
@@ -65,6 +76,7 @@ files:
65
76
  - test/license_test.rb
66
77
  - test/location_test.rb
67
78
  - test/media_test.rb
79
+ - test/oauth_test.rb
68
80
  - test/photo_test.rb
69
81
  - test/set_test.rb
70
82
  - test/user_test.rb
@@ -101,6 +113,7 @@ test_files:
101
113
  - test/license_test.rb
102
114
  - test/location_test.rb
103
115
  - test/media_test.rb
116
+ - test/oauth_test.rb
104
117
  - test/photo_test.rb
105
118
  - test/set_test.rb
106
119
  - test/user_test.rb