omni_api 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/omni_api/config.rb +10 -7
- data/lib/omni_api/resources/{omni_api_connection.rb → connection.rb} +4 -6
- data/lib/omni_api/resources/oauth2/token.rb +3 -1
- data/lib/omni_api/resources/user_authorization_error_handler.rb +13 -0
- data/lib/omni_api/resources/user_authorized_resource.rb +13 -0
- data/lib/omni_api/version.rb +1 -1
- data/spec/OmniApi/resources/connection_spec.rb +74 -0
- data/spec/OmniApi/resources/oauth2/token_spec.rb +59 -0
- data/spec/OmniApi/resources/user/user_resource_spec.rb +6 -0
- data/spec/OmniApi/resources/user_authorization_error_handler_spec.rb +68 -0
- data/spec/OmniApi/resources/user_authorized_resource_spec.rb +8 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8464dd48ed93791d548c2f5b3eaa744aec2d20f5
|
4
|
+
data.tar.gz: 4a8c63a501d5283c8fa9c3bfaa11ca902b56d0c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 279d94e3a2869c510244bec6cf3cd3664615f6e2397fcc8265c563cc81c45a016a650ebce12fc9b6e0609dffe00dbe9832b36644a4a7bc997b9bf2bbec0bf1c2
|
7
|
+
data.tar.gz: 9f8563f868fdcfeddc75ea8c4a85ce4cadf37810fb1fac1bf43f72a07d1deed89d9c3740745b9eb2c97141a28ff26c5ac221d6782f0dc361e5481604970dc599
|
data/lib/omni_api/config.rb
CHANGED
@@ -11,21 +11,24 @@ module OmniApi
|
|
11
11
|
mattr_accessor :client_secret
|
12
12
|
self.client_secret = ''
|
13
13
|
|
14
|
+
mattr_accessor :user_access_token
|
15
|
+
self.user_access_token = ''
|
16
|
+
|
17
|
+
mattr_accessor :user_refresh_token
|
18
|
+
self.user_refresh_token = ''
|
19
|
+
|
20
|
+
mattr_accessor :base_url
|
21
|
+
self.base_url = ''
|
22
|
+
|
14
23
|
# rubocop:disable Style/ClassVars
|
15
24
|
def self.client_access_token=(token)
|
16
25
|
@@client_access_token = "bearer #{token}"
|
17
26
|
end
|
18
27
|
|
19
|
-
mattr_accessor :user_access_token
|
20
|
-
self.user_access_token = ''
|
21
|
-
|
22
28
|
# rubocop:disable Style/ClassVars
|
23
29
|
def self.user_access_token=(token)
|
24
30
|
@@user_access_token = "bearer #{token}"
|
25
|
-
|
26
|
-
|
27
|
-
mattr_accessor :base_url
|
28
|
-
self.base_url = ''
|
31
|
+
end
|
29
32
|
|
30
33
|
def self.config
|
31
34
|
self
|
@@ -2,19 +2,17 @@ require 'active_resource/connection'
|
|
2
2
|
|
3
3
|
module OmniApi
|
4
4
|
module Resources
|
5
|
-
class
|
5
|
+
class Connection < ActiveResource::Connection
|
6
6
|
def initialize(site, format = ActiveResource::Formats::JsonFormat)
|
7
7
|
super
|
8
8
|
end
|
9
9
|
|
10
10
|
attr_accessor :error_handler
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
def request(method, path, *arguments)
|
12
|
+
def request(*arguments)
|
15
13
|
super
|
16
|
-
rescue
|
17
|
-
error_handler.present? ? error_handler.handle(e) : raise
|
14
|
+
rescue => e
|
15
|
+
error_handler.present? ? error_handler.handle(e, self, arguments) : raise
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -21,10 +21,12 @@ module OmniApi
|
|
21
21
|
instance
|
22
22
|
end
|
23
23
|
|
24
|
-
def self.
|
24
|
+
def self.refresh_for(refresh_token)
|
25
25
|
instance = self.new
|
26
26
|
instance.attributes[:grant_type] = OmniApi::Resources::Oauth2::GrantTypes::REFRESH_TOKEN
|
27
|
+
instance.attributes[:client_id] = OmniApi.config.client_id
|
27
28
|
instance.attributes[:refresh_token] = refresh_token
|
29
|
+
instance.attributes[:resource_type] = OmniApi::Resources::Oauth2::ResourceTypes::USER
|
28
30
|
instance.save
|
29
31
|
instance
|
30
32
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module OmniApi
|
2
|
+
module Resources
|
3
|
+
class UserAuthorizationErrorHandler
|
4
|
+
def handle(error, connection, arguments)
|
5
|
+
raise error unless error.is_a?(ActiveResource::UnauthorizedAccess)
|
6
|
+
Oauth2::Token.refresh_for(OmniApi.config.user_refresh_token)
|
7
|
+
new_connection = connection.clone
|
8
|
+
new_connection.error_handler = nil
|
9
|
+
new_connection.request(*arguments)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -12,6 +12,19 @@ module OmniApi
|
|
12
12
|
new_headers['Authorization'] = OmniApi.config.user_access_token
|
13
13
|
new_headers
|
14
14
|
end
|
15
|
+
|
16
|
+
def self.connection(refresh = false)
|
17
|
+
@connection = create_new_connection if refresh || @connection.nil?
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def self.create_new_connection
|
24
|
+
connection = Connection.new(site, format)
|
25
|
+
connection.error_handler = UserAuthorizationErrorHandler.new
|
26
|
+
connection
|
27
|
+
end
|
15
28
|
end
|
16
29
|
end
|
17
30
|
end
|
data/lib/omni_api/version.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OmniApi::Resources::Connection do
|
4
|
+
let(:instance) { OmniApi::Resources::Connection.new('http://123.com') }
|
5
|
+
|
6
|
+
subject { instance }
|
7
|
+
|
8
|
+
it { is_expected.to respond_to(:error_handler=) }
|
9
|
+
it { is_expected.to respond_to(:request) }
|
10
|
+
|
11
|
+
describe '#request' do
|
12
|
+
let(:path) { 'http://123.com/test' }
|
13
|
+
|
14
|
+
subject { instance.request(:get, 'http://123.com/test', {}) }
|
15
|
+
|
16
|
+
context 'the underlying connection fails to resolve the request' do
|
17
|
+
before { ActiveResource::HttpMock.respond_to { |mock| mock.get path, {}, nil, 400 } }
|
18
|
+
|
19
|
+
context 'an error handler is not set' do
|
20
|
+
before { instance.error_handler = nil }
|
21
|
+
|
22
|
+
it 'does not swallow the resulting exception' do
|
23
|
+
expect { subject }.to raise_error(ActiveResource::BadRequest)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'an error handler is set' do
|
28
|
+
let(:error_handler) { double('error_handler') }
|
29
|
+
|
30
|
+
before { instance.error_handler = error_handler }
|
31
|
+
|
32
|
+
it 'calls the handle method of the error handler' do
|
33
|
+
expect(error_handler).to receive(:handle).with(ActiveResource::BadRequest, instance, [:get, 'http://123.com/test', {}])
|
34
|
+
|
35
|
+
subject
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'the error handler handles the error successfully' do
|
39
|
+
let(:response) { ActiveResource::Response.new(body: 'testR') }
|
40
|
+
before { allow(error_handler).to receive(:handle).and_return(response) }
|
41
|
+
|
42
|
+
it { is_expected.to be response }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'a user_authorization_error_handler is used' do
|
47
|
+
let(:error_handler) { OmniApi::Resources::UserAuthorizationErrorHandler.new }
|
48
|
+
|
49
|
+
before { instance.error_handler = error_handler }
|
50
|
+
|
51
|
+
context 'refreshing the access token works but the request always fails with 401' do
|
52
|
+
before do
|
53
|
+
token_request_headers = {'Content-Type' => 'application/json', 'Authorization' => 'bearer random'}
|
54
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
55
|
+
mock.get path, {}, nil, 401
|
56
|
+
mock.post '/api/v1/oauth2/token.json', token_request_headers, nil, 200
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'raises the first exception encountered after refreshing the token without causing a stack overflow' do
|
61
|
+
expect { subject }.to raise_error(ActiveResource::UnauthorizedAccess)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'the underlying connection resolves the request' do
|
68
|
+
before { ActiveResource::HttpMock.respond_to { |mock | mock.get path, {}, 'testR', 200 } }
|
69
|
+
|
70
|
+
it { is_expected.to be_a(ActiveResource::Response) }
|
71
|
+
its(:body) { is_expected.to eq 'testR' }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -6,4 +6,63 @@ describe OmniApi::Resources::Oauth2::Token do
|
|
6
6
|
|
7
7
|
it { is_expected.to eq('token') }
|
8
8
|
end
|
9
|
+
|
10
|
+
def setup_successful_token_request
|
11
|
+
body = {}
|
12
|
+
headers = {'Content-Type' => 'application/json', 'Authorization' => 'bearer random'}
|
13
|
+
ActiveResource::HttpMock.respond_to { |mock| mock.post '/api/v1/oauth2/token.json', headers, body.to_json, 200 }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.create_for' do
|
17
|
+
let(:email) { 'someUser@email.com' }
|
18
|
+
|
19
|
+
subject { OmniApi::Resources::Oauth2::Token.create_for(email) }
|
20
|
+
|
21
|
+
it 'tries to create a new access token for the user with the given email for the the current client using client credentials' do
|
22
|
+
setup_successful_token_request
|
23
|
+
|
24
|
+
subject
|
25
|
+
|
26
|
+
expected_body = {
|
27
|
+
'grant_type' => 'client_credentials',
|
28
|
+
'client_id' => '',
|
29
|
+
'client_secret' => '',
|
30
|
+
'resource_type' => 'user',
|
31
|
+
'resource_id' => 'someUser@email.com'
|
32
|
+
}
|
33
|
+
expect(ActiveResource::HttpMock.requests.first.body).to eq expected_body.to_json
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'creating the token is successful' do
|
37
|
+
before { setup_successful_token_request }
|
38
|
+
|
39
|
+
it { is_expected.to be_a(OmniApi::Resources::Oauth2::Token) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '.refresh_for' do
|
44
|
+
let(:refresh_token) { 'someRefreshToken' }
|
45
|
+
|
46
|
+
subject { OmniApi::Resources::Oauth2::Token.refresh_for(refresh_token) }
|
47
|
+
|
48
|
+
it 'tries to refresh the current user access token using the current user refresh token' do
|
49
|
+
setup_successful_token_request
|
50
|
+
|
51
|
+
subject
|
52
|
+
|
53
|
+
expected_body = {
|
54
|
+
'grant_type' => 'refresh_token',
|
55
|
+
'client_id' => '',
|
56
|
+
'refresh_token' => 'someRefreshToken',
|
57
|
+
'resource_type' => 'user'
|
58
|
+
}
|
59
|
+
expect(ActiveResource::HttpMock.requests.first.body).to eq expected_body.to_json
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'refreshing the token is successful' do
|
63
|
+
before { setup_successful_token_request }
|
64
|
+
|
65
|
+
it { is_expected.to be_a(OmniApi::Resources::Oauth2::Token) }
|
66
|
+
end
|
67
|
+
end
|
9
68
|
end
|
@@ -2,6 +2,12 @@ require 'spec_helper'
|
|
2
2
|
require 'uri'
|
3
3
|
|
4
4
|
describe OmniApi::Resources::User::UserResource do
|
5
|
+
describe '.connection' do
|
6
|
+
subject { OmniApi::Resources::User::UserResource.connection }
|
7
|
+
|
8
|
+
it { is_expected.to be_a(OmniApi::Resources::Connection) }
|
9
|
+
end
|
10
|
+
|
5
11
|
describe '.site' do
|
6
12
|
subject { OmniApi::Resources::User::UserResource.site }
|
7
13
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_resource/http_mock'
|
3
|
+
|
4
|
+
describe OmniApi::Resources::UserAuthorizationErrorHandler do
|
5
|
+
let(:instance) { OmniApi::Resources::UserAuthorizationErrorHandler.new }
|
6
|
+
|
7
|
+
describe '#handle' do
|
8
|
+
let(:error) { Exception.new('some message') }
|
9
|
+
let(:connection) { OmniApi::Resources::Connection.new('http://123.com') }
|
10
|
+
let(:arguments) { [:get, 'http://123.com', {}] }
|
11
|
+
|
12
|
+
subject { instance.handle(error, connection, arguments) }
|
13
|
+
|
14
|
+
context 'the given error is not an ActiveResource::UnauthorizedAccess error' do
|
15
|
+
it 'rethrows the given error' do
|
16
|
+
expect { subject }.to raise_error(error)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'the given error is an ActiveResource::UnauthorizedAccess error' do
|
21
|
+
let(:error) { ActiveResource::UnauthorizedAccess.new('nah') }
|
22
|
+
|
23
|
+
it 'tries to refresh the current user access token' do
|
24
|
+
OmniApi.config.user_refresh_token = 'refreshToken'
|
25
|
+
expect(OmniApi::Resources::Oauth2::Token).to receive(:refresh_for).with('refreshToken')
|
26
|
+
|
27
|
+
subject rescue nil
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'refreshing the token works' do
|
31
|
+
let(:updated_token) { OmniApi::Resources::Oauth2::Token.new({access_token: 'test'}) }
|
32
|
+
|
33
|
+
before do
|
34
|
+
allow(updated_token).to receive(:persisted?).and_return(true)
|
35
|
+
allow(OmniApi::Resources::Oauth2::Token).to receive(:refresh_for).and_return(updated_token)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'tries to perform the original request again' do
|
39
|
+
expect(connection).to receive(:request).with(*arguments)
|
40
|
+
|
41
|
+
subject
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'retrying the request is successful' do
|
45
|
+
let(:response) { ActiveResource::Response.new(body: 'test1') }
|
46
|
+
|
47
|
+
before { allow(connection).to receive(:request).and_return(response) }
|
48
|
+
|
49
|
+
it { is_expected.to be response}
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'retrying the request fails' do
|
53
|
+
before { allow(connection).to receive(:request).and_raise(ActiveResource::BadRequest.new('test')) }
|
54
|
+
|
55
|
+
it 'rethrows the error' do
|
56
|
+
expect { subject }.to raise_error(ActiveResource::BadRequest)
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'the request fails again with ActiveResource::UnauthorizedAccess' do
|
60
|
+
before { allow(connection).to receive(:request).and_raise(ActiveResource::BadRequest.new('test')) }
|
61
|
+
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -16,4 +16,12 @@ describe OmniApi::Resources::UserAuthorizedResource do
|
|
16
16
|
its(['Authorization']) { is_expected.to eq('bearer ') }
|
17
17
|
end
|
18
18
|
end
|
19
|
+
|
20
|
+
describe '.connection' do
|
21
|
+
subject { OmniApi::Resources::UserAuthorizedResource.connection }
|
22
|
+
|
23
|
+
it { is_expected.to be_a(OmniApi::Resources::Connection) }
|
24
|
+
|
25
|
+
its(:error_handler) { is_expected.to be_a(OmniApi::Resources::UserAuthorizationErrorHandler) }
|
26
|
+
end
|
19
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omni_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cristi Badila
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-09-
|
12
|
+
date: 2015-09-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activeresource
|
@@ -129,10 +129,10 @@ files:
|
|
129
129
|
- lib/omni_api/resources/base_client_model.rb
|
130
130
|
- lib/omni_api/resources/base_model.rb
|
131
131
|
- lib/omni_api/resources/client.rb
|
132
|
+
- lib/omni_api/resources/connection.rb
|
132
133
|
- lib/omni_api/resources/oauth2/grant_types.rb
|
133
134
|
- lib/omni_api/resources/oauth2/resource_types.rb
|
134
135
|
- lib/omni_api/resources/oauth2/token.rb
|
135
|
-
- lib/omni_api/resources/omni_api_connection.rb
|
136
136
|
- lib/omni_api/resources/payment_plan.rb
|
137
137
|
- lib/omni_api/resources/phone_call.rb
|
138
138
|
- lib/omni_api/resources/sms_message.rb
|
@@ -140,13 +140,16 @@ files:
|
|
140
140
|
- lib/omni_api/resources/user/client_association.rb
|
141
141
|
- lib/omni_api/resources/user/device.rb
|
142
142
|
- lib/omni_api/resources/user/user_resource.rb
|
143
|
+
- lib/omni_api/resources/user_authorization_error_handler.rb
|
143
144
|
- lib/omni_api/resources/user_authorized_resource.rb
|
144
145
|
- lib/omni_api/version.rb
|
145
146
|
- spec/OmniApi/factories/user_factory_spec.rb
|
146
147
|
- spec/OmniApi/resources/client_spec.rb
|
148
|
+
- spec/OmniApi/resources/connection_spec.rb
|
147
149
|
- spec/OmniApi/resources/oauth2/token_spec.rb
|
148
150
|
- spec/OmniApi/resources/sms_message_spec.rb
|
149
151
|
- spec/OmniApi/resources/user/user_resource_spec.rb
|
152
|
+
- spec/OmniApi/resources/user_authorization_error_handler_spec.rb
|
150
153
|
- spec/OmniApi/resources/user_authorized_resource_spec.rb
|
151
154
|
- spec/OmniApi/resources/user_spec.rb
|
152
155
|
- spec/OmniApi_spec.rb
|
@@ -178,9 +181,11 @@ summary: A gem used to make using OmniApi easier
|
|
178
181
|
test_files:
|
179
182
|
- spec/OmniApi/factories/user_factory_spec.rb
|
180
183
|
- spec/OmniApi/resources/client_spec.rb
|
184
|
+
- spec/OmniApi/resources/connection_spec.rb
|
181
185
|
- spec/OmniApi/resources/oauth2/token_spec.rb
|
182
186
|
- spec/OmniApi/resources/sms_message_spec.rb
|
183
187
|
- spec/OmniApi/resources/user/user_resource_spec.rb
|
188
|
+
- spec/OmniApi/resources/user_authorization_error_handler_spec.rb
|
184
189
|
- spec/OmniApi/resources/user_authorized_resource_spec.rb
|
185
190
|
- spec/OmniApi/resources/user_spec.rb
|
186
191
|
- spec/OmniApi_spec.rb
|