oauth2 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -46,7 +46,24 @@ Below is a fully functional example of a Sinatra application that would authenti
46
46
  uri.to_s
47
47
  end
48
48
 
49
- That's all there is to it! You can use the access token like you would with the OAuth gem, calling HTTP verbs on it etc. You can view more examples on the OAuth2 Wiki (http://wiki.github.com/intridea/oauth2/examples)
49
+ That's all there is to it! You can use the access token like you would with the
50
+ OAuth gem, calling HTTP verbs on it etc. You can view more examples on the OAuth2
51
+ Wiki (http://wiki.github.com/intridea/oauth2/examples)
52
+
53
+ == JSON Parsing
54
+
55
+ Because JSON has become the standard format of the OAuth 2.0 specification,
56
+ the <tt>oauth2</tt> gem contains a mode that will perform automatic parsing
57
+ of JSON response bodies, returning a hash instead of a string. To enable this
58
+ mode, simply add the <tt>:parse_json</tt> option to your client initialization:
59
+
60
+ client = OAuth2::Client.new('app_id', 'app_secret',
61
+ :site => 'https://example.com',
62
+ :parse_json => true
63
+ )
64
+
65
+ # Obtain an access token using the client
66
+ token.get('/some/url.json') # {"some" => "hash"}
50
67
 
51
68
  == Note on Patches/Pull Requests
52
69
 
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/intridea/oauth2"
12
12
  gem.authors = ["Michael Bleigh"]
13
13
  gem.add_dependency 'faraday', '~> 0.4.1'
14
+ gem.add_dependency 'faraday-middleware'
14
15
  gem.add_dependency 'multi_json', '>= 0.0.4'
15
16
  gem.add_development_dependency "rspec", ">= 1.2.9"
16
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
data/lib/oauth2.rb CHANGED
@@ -8,4 +8,5 @@ require 'oauth2/client'
8
8
  require 'oauth2/strategy/base'
9
9
  require 'oauth2/strategy/web_server'
10
10
  require 'oauth2/access_token'
11
- require 'oauth2/response_string'
11
+ require 'oauth2/response_string'
12
+ require 'oauth2/response_hash'
data/lib/oauth2/client.rb CHANGED
@@ -15,6 +15,7 @@ module OAuth2
15
15
  end
16
16
 
17
17
  attr_accessor :id, :secret, :site, :connection, :options
18
+ attr_writer :json
18
19
 
19
20
  # Instantiate a new OAuth 2.0 client using the
20
21
  # client ID and client secret registered to your
@@ -34,6 +35,8 @@ module OAuth2
34
35
  self.site = opts.delete(:site) if opts[:site]
35
36
  self.options = opts
36
37
  self.connection = Faraday::Connection.new(site)
38
+ self.json = opts.delete(:parse_json)
39
+
37
40
  if adapter && adapter != :test
38
41
  connection.build { |b| b.adapter(adapter) }
39
42
  end
@@ -59,7 +62,12 @@ module OAuth2
59
62
  resp = connection.run_request(verb, url, params, headers)
60
63
  end
61
64
  case resp.status
62
- when 200...201 then ResponseString.new(resp)
65
+ when 200...201
66
+ if json? && resp.headers['Content-Type'] == 'application/json'
67
+ ResponseHash.new(resp)
68
+ else
69
+ ResponseString.new(resp)
70
+ end
63
71
  when 401
64
72
  e = OAuth2::AccessDenied.new("Received HTTP 401 during request.")
65
73
  e.response = resp
@@ -70,6 +78,8 @@ module OAuth2
70
78
  raise e
71
79
  end
72
80
  end
81
+
82
+ def json?; !!@json end
73
83
 
74
84
  def web_server; OAuth2::Strategy::WebServer.new(self) end
75
85
  end
@@ -0,0 +1,13 @@
1
+ class ResponseHash < Hash
2
+ def initialize(response)
3
+ self.response = response
4
+
5
+ body = MultiJson.decode(response.body)
6
+ body.keys.each{|k| self[k] = body[k]}
7
+ end
8
+
9
+ attr_accessor :response
10
+
11
+ def status; response.status end
12
+ def headers; response.headers end
13
+ end
@@ -10,11 +10,6 @@ class ResponseString < String
10
10
 
11
11
  attr_accessor :response
12
12
 
13
- def status
14
- response.status
15
- end
16
-
17
- def headers
18
- response.headers
19
- end
13
+ def status; response.status end
14
+ def headers; response.headers end
20
15
  end
data/oauth2.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{oauth2}
8
- s.version = "0.0.10"
8
+ s.version = "0.0.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Bleigh"]
12
- s.date = %q{2010-06-19}
12
+ s.date = %q{2010-08-17}
13
13
  s.description = %q{A Ruby wrapper for the OAuth 2.0 protocol built with a similar style to the original OAuth gem.}
14
14
  s.email = %q{michael@intridea.com}
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/oauth2.rb",
28
28
  "lib/oauth2/access_token.rb",
29
29
  "lib/oauth2/client.rb",
30
+ "lib/oauth2/response_hash.rb",
30
31
  "lib/oauth2/response_string.rb",
31
32
  "lib/oauth2/strategy/base.rb",
32
33
  "lib/oauth2/strategy/web_server.rb",
@@ -42,7 +43,7 @@ Gem::Specification.new do |s|
42
43
  s.homepage = %q{http://github.com/intridea/oauth2}
43
44
  s.rdoc_options = ["--charset=UTF-8"]
44
45
  s.require_paths = ["lib"]
45
- s.rubygems_version = %q{1.3.6}
46
+ s.rubygems_version = %q{1.3.7}
46
47
  s.summary = %q{A Ruby wrapper for the OAuth 2.0 protocol.}
47
48
  s.test_files = [
48
49
  "spec/oauth2/access_token_spec.rb",
@@ -56,17 +57,20 @@ Gem::Specification.new do |s|
56
57
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
58
  s.specification_version = 3
58
59
 
59
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
61
  s.add_runtime_dependency(%q<faraday>, ["~> 0.4.1"])
62
+ s.add_runtime_dependency(%q<faraday-middleware>, [">= 0"])
61
63
  s.add_runtime_dependency(%q<multi_json>, [">= 0.0.4"])
62
64
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
63
65
  else
64
66
  s.add_dependency(%q<faraday>, ["~> 0.4.1"])
67
+ s.add_dependency(%q<faraday-middleware>, [">= 0"])
65
68
  s.add_dependency(%q<multi_json>, [">= 0.0.4"])
66
69
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
67
70
  end
68
71
  else
69
72
  s.add_dependency(%q<faraday>, ["~> 0.4.1"])
73
+ s.add_dependency(%q<faraday-middleware>, [">= 0"])
70
74
  s.add_dependency(%q<multi_json>, [">= 0.0.4"])
71
75
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
72
76
  end
@@ -8,6 +8,7 @@ describe OAuth2::Client do
8
8
  stub.get('/success') { |env| [200, {'Content-Type' => 'text/awesome'}, 'yay'] }
9
9
  stub.get('/unauthorized') { |env| [401, {}, ''] }
10
10
  stub.get('/error') { |env| [500, {}, ''] }
11
+ stub.get('/json') { |env| [200, {'Content-Type' => 'application/json'}, '{"abc":"def"}']}
11
12
  end
12
13
  end
13
14
  cli
@@ -66,4 +67,31 @@ describe OAuth2::Client do
66
67
  it '#web_server should instantiate a WebServer strategy with this client' do
67
68
  subject.web_server.should be_kind_of(OAuth2::Strategy::WebServer)
68
69
  end
70
+
71
+ context 'with JSON parsing' do
72
+ before do
73
+ subject.json = true
74
+ end
75
+
76
+ describe '#request' do
77
+ it 'should return a response hash' do
78
+ response = subject.request(:get, '/json', {}, {})
79
+ response.should be_kind_of(ResponseHash)
80
+ response['abc'].should == 'def'
81
+ end
82
+
83
+ it 'should only try to decode application/json' do
84
+ subject.request(:get, '/success').should == 'yay'
85
+ end
86
+ end
87
+
88
+ it 'should set json? based on the :parse_json option' do
89
+ OAuth2::Client.new('abc','def', :site => 'http://example.com', :parse_json => true).should be_json
90
+ OAuth2::Client.new('abc','def', :site => 'http://example.com', :parse_json => false).should_not be_json
91
+ end
92
+
93
+ after do
94
+ subject.json = false
95
+ end
96
+ end
69
97
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 9
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 10
9
- version: 0.0.10
9
+ - 11
10
+ version: 0.0.11
10
11
  platform: ruby
11
12
  authors:
12
13
  - Michael Bleigh
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-06-19 00:00:00 -04:00
18
+ date: 2010-08-17 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: faraday
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ~>
26
28
  - !ruby/object:Gem::Version
29
+ hash: 13
27
30
  segments:
28
31
  - 0
29
32
  - 4
@@ -32,33 +35,51 @@ dependencies:
32
35
  type: :runtime
33
36
  version_requirements: *id001
34
37
  - !ruby/object:Gem::Dependency
35
- name: multi_json
38
+ name: faraday-middleware
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: multi_json
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
38
56
  requirements:
39
57
  - - ">="
40
58
  - !ruby/object:Gem::Version
59
+ hash: 23
41
60
  segments:
42
61
  - 0
43
62
  - 0
44
63
  - 4
45
64
  version: 0.0.4
46
65
  type: :runtime
47
- version_requirements: *id002
66
+ version_requirements: *id003
48
67
  - !ruby/object:Gem::Dependency
49
68
  name: rspec
50
69
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
52
72
  requirements:
53
73
  - - ">="
54
74
  - !ruby/object:Gem::Version
75
+ hash: 13
55
76
  segments:
56
77
  - 1
57
78
  - 2
58
79
  - 9
59
80
  version: 1.2.9
60
81
  type: :development
61
- version_requirements: *id003
82
+ version_requirements: *id004
62
83
  description: A Ruby wrapper for the OAuth 2.0 protocol built with a similar style to the original OAuth gem.
63
84
  email: michael@intridea.com
64
85
  executables: []
@@ -79,6 +100,7 @@ files:
79
100
  - lib/oauth2.rb
80
101
  - lib/oauth2/access_token.rb
81
102
  - lib/oauth2/client.rb
103
+ - lib/oauth2/response_hash.rb
82
104
  - lib/oauth2/response_string.rb
83
105
  - lib/oauth2/strategy/base.rb
84
106
  - lib/oauth2/strategy/web_server.rb
@@ -100,23 +122,27 @@ rdoc_options:
100
122
  require_paths:
101
123
  - lib
102
124
  required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
103
126
  requirements:
104
127
  - - ">="
105
128
  - !ruby/object:Gem::Version
129
+ hash: 3
106
130
  segments:
107
131
  - 0
108
132
  version: "0"
109
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
110
135
  requirements:
111
136
  - - ">="
112
137
  - !ruby/object:Gem::Version
138
+ hash: 3
113
139
  segments:
114
140
  - 0
115
141
  version: "0"
116
142
  requirements: []
117
143
 
118
144
  rubyforge_project:
119
- rubygems_version: 1.3.6
145
+ rubygems_version: 1.3.7
120
146
  signing_key:
121
147
  specification_version: 3
122
148
  summary: A Ruby wrapper for the OAuth 2.0 protocol.