flickrie 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  TODO.md
2
2
  Gemfile.lock
3
3
  *.gem
4
+ development.rb
data/Gemfile CHANGED
@@ -5,4 +5,6 @@ gemspec
5
5
  group :development, :test do
6
6
  gem "rake"
7
7
  gem "activesupport"
8
+ gem "flickrie", :path => "~/Projects/flickrie"
9
+ gem "pry"
8
10
  end
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## About
4
4
 
5
- This gem is a nice wrapper for the Flickr API an intuitive interface.
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
8
  well maintained, or they were too literal in the sense that the response from
@@ -22,13 +22,14 @@ Then in your app you require it and set the API key.
22
22
 
23
23
  ```ruby
24
24
  require 'flickrie'
25
- Flickr.api_key = "some_api_key"
25
+ Flickrie.api_key = "<your_api_key>"
26
+ Flickrie.shared_secret = "<your_shared_secret>"
26
27
  ```
27
28
 
28
29
  Then you can search for photos.
29
30
 
30
31
  ```ruby
31
- photos = Flickr.photos_from_set(819234) # => [#<Photo: id="8232348", ...>, #<Photo: id="8194318", ...>, ...]
32
+ photos = Flickrie.photos_from_set(819234) # => [#<Photo: id="8232348", ...>, #<Photo: id="8194318", ...>, ...]
32
33
 
33
34
  photo = photos.first
34
35
  photo.id # => "8232348"
@@ -38,10 +39,14 @@ photo.owner # => #<User: nsid="67313352@N04", ...>
38
39
  photo.owner.nsid # => "67313352@N04"
39
40
  ```
40
41
 
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.
45
+
41
46
  You can also throw in some parameters to get more information about photos. For example,
42
47
 
43
48
  ```ruby
44
- photos = Flickr.photos_from_set(819234, :extras => 'owner_name,last_update,tags,views')
49
+ photos = Flickrie.photos_from_set(819234, :extras => 'owner_name,last_update,tags,views')
45
50
 
46
51
  photo = photos.first
47
52
  photo.tags # => "cave cold forrest"
@@ -55,14 +60,14 @@ On the list of available parameters you can read in the [Flickr API documentatio
55
60
  You can also get additional info on a single photo:
56
61
 
57
62
  ```ruby
58
- photo = Flickr.get_photo_info(8232348)
63
+ photo = Flickrie.get_photo_info(8232348)
59
64
 
60
65
  photo.description # => "In this photo, Samantha and me found a secret tunnel..."
61
66
  photo.comments_count # => 6
62
67
  photo.visibility.public? # => true
63
68
  photo.can_download? # => true
64
69
  photo.owner.real_name # => "John Smith"
65
- photo.location.country.name # => "United Stated"
70
+ photo.location.country.name # => "United States"
66
71
  ```
67
72
 
68
73
  You can also get this info on an existing photo:
@@ -76,7 +81,7 @@ photo.description # => "In this photo Peter said something really funny..."
76
81
  If you want to display photos from flickr in your app, this is probably the most useful part:
77
82
 
78
83
  ```ruby
79
- photo = Flickr.photo_get_sizes(8232348)
84
+ photo = Flickrie.get_photo_sizes(8232348)
80
85
 
81
86
  photo.medium!(800)
82
87
  photo.size # => "Medium 800"
@@ -91,23 +96,41 @@ photo.width # => 240
91
96
  photo.width # => 320
92
97
  ```
93
98
 
94
- If you want sizes to be available to photos you're fetching from a set, it's a bad idea to call `#get_info` on each photo. Instead, you should pass in these options:
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:
95
100
 
96
101
  ```ruby
97
- photos = Flickr.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')
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')
98
103
  photo = photos.first
99
104
  photo.medium!(640)
100
105
  photo.source_url # => "http://farm8.staticflickr.com/7049/6946979188_25bb44852b_z.jpg"
101
106
  ```
102
107
 
103
- These are just some of the cool things you can do. To see a full list of available methods, I encourage you to read the [wiki](https://github.com/janko-m/flickrie/wiki). I promise, I will document the methods properly in near future :)
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 :)
109
+
110
+ ## Authentication
111
+
112
+ ```ruby
113
+ require 'flickrie'
114
+
115
+ Flickrie.api_key = "<your api key>"
116
+ Flickrie.shared_secret = "<your shared secret>"
117
+
118
+ url = Flickrie.get_authorization_url
119
+ puts "Visit this url to authorize: #{url}"
120
+ puts "Then enter the code that was displayed: "
121
+ code = gets.strip
122
+ Flickrie.authorize!(code)
123
+ puts "You're all done! Now go make some authenticated requests. Make me proud, son."
124
+ ```
125
+
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`.
104
127
 
105
128
  ## A few words
106
129
 
107
- 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 `Flickr.client`, like this:
130
+ 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:
108
131
 
109
132
  ```ruby
110
- response = Flickr.client.get("flickr.photos.getContext", :photo_id => 2842732)
133
+ response = Flickrie.client.get("flickr.photos.getContext", :photo_id => 2842732)
111
134
  reponse.body # =>
112
135
  # {
113
136
  # "count" => {"_content" => 99},
@@ -124,13 +147,19 @@ reponse.body # =>
124
147
  # }
125
148
  ```
126
149
 
127
- It's not nearly as pretty, but at least you can get to the data
150
+ It's not nearly as pretty, but at least you can get to the data.
128
151
 
129
152
  ## Issues
130
153
 
131
154
  Please, feel free to post any issues that you're having, I will try to
132
155
  help you in any way I can.
133
156
 
157
+ ## Cedits
158
+
159
+ Special thanks to @**mislav**, my brother, he helped me really a lot
160
+ with getting started with ruby. And he also helped me with the
161
+ basis of this gem.
162
+
134
163
  ## Currently covered API methods
135
164
 
136
165
  ### people
data/flickrie.gemspec CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../lib/flickrie/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Janko Marohnić"]
6
6
  gem.email = ["janko.marohnic@gmail.com"]
7
- gem.description = %q{This gem is a nice wrapper for the Flickr API an intuitive interface.}
7
+ gem.description = %q{This gem is a nice wrapper for the Flickr API with an intuitive interface.}
8
8
  gem.summary = %q{The reason why I did this gem is because the other ones either weren't well maintained, or they were too literal in the sense that the response from the API call wasn't processed almost at all. It doesn't seem too bad at first, but after a while you realize it's not pretty. So I wanted to make it pretty :)}
9
9
  gem.homepage = "https://github.com/janko-m/flickrie"
10
10
 
@@ -15,5 +15,6 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Flickrie::VERSION
17
17
 
18
- gem.add_dependency 'faraday-stack'
18
+ gem.add_dependency 'faraday_middleware'
19
+ gem.add_dependency 'simple_oauth'
19
20
  end
@@ -1,31 +1,44 @@
1
- require 'faraday_stack'
1
+ require 'faraday_middleware'
2
+ require 'simple_oauth'
2
3
 
3
4
  module Flickrie
4
5
  class << self
5
- attr_reader :api_key, :timeout, :open_timeout
6
-
7
- def api_key=(api_key); @client = nil; @api_key = api_key end
8
- def timeout=(api_key); @client = nil; @timeout = api_key end
9
- def open_timeout=(api_key); @client = nil; @open_timeout = api_key end
6
+ attr_accessor :api_key, :shared_secret, :timeout, :open_timeout,
7
+ :token, :token_secret
10
8
 
11
9
  def client
12
10
  @client ||= begin
13
- client = FaradayStack.build Client,
14
- :url => 'http://api.flickr.com/services/rest/',
15
- :params => {
16
- :format => 'json',
17
- :nojsoncallback => '1',
18
- :api_key => self.api_key
19
- },
20
- :request => {
21
- :open_timeout => self.open_timeout || 8,
22
- :timeout => self.timeout || 8
23
- }
24
-
25
- client.builder.insert_before FaradayStack::ResponseJSON, StatusCheck
11
+ client = Client.new(params) do |conn|
12
+ conn.request :oauth,
13
+ :consumer_key => api_key,
14
+ :consumer_secret => shared_secret,
15
+ :token => token,
16
+ :token_secret => token_secret
17
+ conn.response :json, :content_type => /\bjson$/
18
+ conn.adapter Faraday.default_adapter
19
+ end
20
+
21
+ client.builder.insert_before FaradayMiddleware::ParseJson, StatusCheck
26
22
  client
27
23
  end
28
24
  end
25
+
26
+ private
27
+
28
+ def params
29
+ {
30
+ :url => 'http://api.flickr.com/services/rest/',
31
+ :params => {
32
+ :format => 'json',
33
+ :nojsoncallback => '1',
34
+ :api_key => api_key
35
+ },
36
+ :request => {
37
+ :open_timeout => open_timeout || 8,
38
+ :timeout => timeout || 8
39
+ }
40
+ }
41
+ end
29
42
  end
30
43
 
31
44
  class Error < StandardError
@@ -47,6 +60,13 @@ module Flickrie
47
60
  end
48
61
  end
49
62
 
63
+ def post(method, params = {})
64
+ super() do |req|
65
+ req.params[:method] = method
66
+ req.params.update(params)
67
+ end
68
+ end
69
+
50
70
  # people
51
71
  def find_user_by_email(email)
52
72
  get 'flickr.people.findByEmail', :find_email => email
@@ -0,0 +1,79 @@
1
+ module Flickrie
2
+ module OAuth
3
+ class RequestToken
4
+ attr_reader :token, :secret
5
+
6
+ def initialize(response_body)
7
+ @token = response_body[/(?<=oauth_token=)[^&]+/]
8
+ @secret = response_body[/(?<=oauth_token_secret=)[^&]+/]
9
+ end
10
+
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
16
+ end
17
+ end
18
+
19
+ class Consumer
20
+ def initialize(api_key, shared_secret)
21
+ @api_key, @shared_secret = api_key, shared_secret
22
+ end
23
+
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
31
+
32
+ response = connection.get("request_token") do |req|
33
+ req.params[:oauth_callback] = 'oob'
34
+ end
35
+
36
+ RequestToken.new(response.body)
37
+ end
38
+
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
48
+
49
+ response = connection.get "access_token" do |req|
50
+ req.params[:oauth_verifier] = oauth_verifier
51
+ end
52
+
53
+ [
54
+ response.body[/(?<=oauth_token=)[^&]+/],
55
+ response.body[/(?<=oauth_token_secret=)[^&]+/]
56
+ ]
57
+ end
58
+ end
59
+ end
60
+ end
61
+
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)
67
+ end
68
+
69
+ def authorize!(code)
70
+ token, token_secret = consumer.get_access_token(code, @request_token)
71
+ end
72
+
73
+ private
74
+
75
+ def consumer
76
+ @consumer ||= OAuth::Consumer.new(api_key, shared_secret)
77
+ end
78
+ end
79
+ end
@@ -1,3 +1,3 @@
1
1
  module Flickrie
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/flickrie.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'flickrie/client'
2
+ require 'flickrie/oauth'
2
3
  require 'flickrie/license'
3
4
  require 'flickrie/user'
4
5
  require 'flickrie/media'
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'flickrie'
3
+
4
+ class OAuthTest < Test::Unit::TestCase
5
+ def setup
6
+ @photo_id = 6946979188
7
+ Flickrie.api_key = ENV['FLICKR_API_KEY']
8
+ Flickrie.shared_secret = ENV['FLICKR_SHARED_SECRET']
9
+ Flickrie.token = ENV['FLICKR_TOKEN']
10
+ Flickrie.token_secret = ENV['FLICKR_TOKEN_SECRET']
11
+ end
12
+
13
+ def test_permissions
14
+ assert_nothing_raised do
15
+ Flickrie.client.get "flickr.photos.getPerms", :photo_id => @photo_id
16
+ Flickrie.client.post "flickr.photos.setSafetyLevel", :photo_id => @photo_id, :safety_level => 1
17
+ end
18
+ assert_raises(Flickrie::Error) do
19
+ Flickrie.client.get "flickr.photos.delete", :photo_id => @photo_id
20
+ end
21
+ end
22
+ 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.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-21 00:00:00.000000000 Z
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: faraday-stack
16
- requirement: &70253217952440 !ruby/object:Gem::Requirement
15
+ name: faraday_middleware
16
+ requirement: &70166247860000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,8 +21,19 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70253217952440
25
- description: This gem is a nice wrapper for the Flickr API an intuitive interface.
24
+ version_requirements: *70166247860000
25
+ - !ruby/object:Gem::Dependency
26
+ name: simple_oauth
27
+ requirement: &70166247859580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70166247859580
36
+ description: This gem is a nice wrapper for the Flickr API with an intuitive interface.
26
37
  email:
27
38
  - janko.marohnic@gmail.com
28
39
  executables: []
@@ -43,6 +54,7 @@ files:
43
54
  - lib/flickrie/media.rb
44
55
  - lib/flickrie/media/note.rb
45
56
  - lib/flickrie/media/visibility.rb
57
+ - lib/flickrie/oauth.rb
46
58
  - lib/flickrie/photo.rb
47
59
  - lib/flickrie/set.rb
48
60
  - lib/flickrie/user.rb
@@ -52,6 +64,7 @@ files:
52
64
  - test/license_test.rb
53
65
  - test/location_test.rb
54
66
  - test/media_test.rb
67
+ - test/oauth_test.rb
55
68
  - test/photo_test.rb
56
69
  - test/set_test.rb
57
70
  - test/user_test.rb
@@ -88,6 +101,7 @@ test_files:
88
101
  - test/license_test.rb
89
102
  - test/location_test.rb
90
103
  - test/media_test.rb
104
+ - test/oauth_test.rb
91
105
  - test/photo_test.rb
92
106
  - test/set_test.rb
93
107
  - test/user_test.rb