rdstation-ruby-client 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -158,5 +158,19 @@ RSpec.describe RDStation::ErrorHandler do
158
158
  expect { error_handler.raise_error }.to raise_error(RDStation::Error::ServerError, 'Error Message')
159
159
  end
160
160
  end
161
+
162
+ context "when response body is not JSON-parseable" do
163
+ let(:error_response) do
164
+ OpenStruct.new(
165
+ code: 502,
166
+ headers: { 'error' => 'header' },
167
+ body: '<html><body>HTML error response</body></html>'
168
+ )
169
+ end
170
+
171
+ it 'raises the correct error' do
172
+ expect { error_handler.raise_error }.to raise_error(RDStation::Error::BadGateway, '<html><body>HTML error response</body></html>')
173
+ end
174
+ end
161
175
  end
162
176
  end
@@ -6,13 +6,13 @@ RSpec.describe RDStation::Events do
6
6
  let(:expired_access_token) { 'expired_access_token' }
7
7
 
8
8
  let(:event_with_valid_token) do
9
- described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: valid_access_token))
9
+ described_class.new(authorization: RDStation::Authorization.new(access_token: valid_access_token))
10
10
  end
11
11
  let(:event_with_expired_token) do
12
- described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: expired_access_token))
12
+ described_class.new(authorization: RDStation::Authorization.new(access_token: expired_access_token))
13
13
  end
14
14
  let(:event_with_invalid_token) do
15
- described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: invalid_access_token))
15
+ described_class.new(authorization: RDStation::Authorization.new(access_token: invalid_access_token))
16
16
  end
17
17
 
18
18
  let(:events_endpoint) { 'https://api.rd.services/platform/events' }
@@ -108,6 +108,11 @@ RSpec.describe RDStation::Events do
108
108
  }
109
109
  end
110
110
 
111
+ it 'calls retryable_request' do
112
+ expect(event_with_valid_token).to receive(:retryable_request)
113
+ event_with_valid_token.create({})
114
+ end
115
+
111
116
  context 'with a valid auth token' do
112
117
  before do
113
118
  stub_request(:post, events_endpoint)
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  RSpec.describe RDStation::Fields do
4
4
  let(:valid_access_token) { 'valid_access_token' }
5
5
  let(:rdstation_fields_with_valid_token) do
6
- described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: valid_access_token))
6
+ described_class.new(authorization: RDStation::Authorization.new(access_token: valid_access_token))
7
7
  end
8
8
 
9
9
  let(:valid_headers) do
@@ -38,6 +38,11 @@ RSpec.describe RDStation::Fields do
38
38
  }
39
39
  end
40
40
 
41
+ it 'calls retryable_request' do
42
+ expect(rdstation_fields_with_valid_token).to receive(:retryable_request)
43
+ rdstation_fields_with_valid_token.all
44
+ end
45
+
41
46
  context 'with a valid auth token' do
42
47
  before do
43
48
  stub_request(:get, fields_endpoint)
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ class DummyClass
4
+ include ::RDStation::RetryableRequest
5
+ end
6
+
7
+ RSpec.describe RDStation::RetryableRequest do
8
+ let(:subject) { DummyClass.new }
9
+ describe '.retryable_request' do
10
+ context 'when authorization has a valid refresh_token and config is provided' do
11
+ let (:access_token) { 'access_token' }
12
+ let (:new_access_token) { 'new_access_token' }
13
+ let (:refresh_token) { 'refresh_token' }
14
+ let (:auth) do
15
+ ::RDStation::Authorization.new(access_token: access_token,
16
+ refresh_token: refresh_token
17
+ )
18
+ end
19
+ context 'original request was successful' do
20
+ it 'yields control to the given block' do
21
+ expect do |block|
22
+ subject.retryable_request(auth, &block)
23
+ end.to yield_with_args(auth)
24
+ end
25
+ end
26
+
27
+ context 'original request raised a retryable exception' do
28
+ let (:auth_new_access_token) do
29
+ ::RDStation::Authorization.new(access_token: new_access_token,
30
+ refresh_token: refresh_token
31
+ )
32
+ end
33
+
34
+ let(:new_credentials) do
35
+ {
36
+ 'access_token' => new_access_token,
37
+ 'expires_in' => 86_400,
38
+ 'refresh_token' => refresh_token
39
+ }
40
+ end
41
+ let(:authentication_client) {instance_double(::RDStation::Authentication) }
42
+
43
+ before do
44
+ RDStation.configure do |config|
45
+ config.client_id = "123"
46
+ config.client_secret = "312"
47
+ config.on_access_token_refresh do
48
+ 'callback code'
49
+ end
50
+ end
51
+ allow(::RDStation::Authentication).to receive(:new)
52
+ .with(no_args)
53
+ .and_return(authentication_client)
54
+ allow(authentication_client).to receive(:update_access_token)
55
+ .with(auth.refresh_token).
56
+ and_return(new_credentials)
57
+ end
58
+
59
+ it 'refreshes the access_token and retries the request' do
60
+ dummy_request = double("dummy_request")
61
+ expect(dummy_request).to receive(:call).twice do |auth|
62
+ expired_token = ::RDStation::Error::ExpiredAccessToken.new({'error_message' => 'x'})
63
+ raise expired_token unless auth.access_token == new_access_token
64
+ end
65
+
66
+ expect(RDStation.configuration.access_token_refresh_callback)
67
+ .to receive(:call)
68
+ .once do |authorization|
69
+ expect(authorization.access_token).to eq new_access_token
70
+ end
71
+
72
+ expect do
73
+ subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
74
+ end.not_to raise_error
75
+ end
76
+
77
+ context 'and keeps raising retryable exception event after token refreshed' do
78
+ it 'retries only once' do
79
+ dummy_request = double("dummy_request")
80
+ expect(dummy_request).to receive(:call).twice do |_|
81
+ raise ::RDStation::Error::ExpiredAccessToken.new({'error_message' => 'x'})
82
+ end
83
+
84
+ expect do
85
+ subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
86
+ end.to raise_error ::RDStation::Error::ExpiredAccessToken
87
+ end
88
+ end
89
+
90
+ context 'and access token refresh callback is not set' do
91
+ before do
92
+ RDStation.configure do |config|
93
+ config.on_access_token_refresh(&nil)
94
+ end
95
+ end
96
+
97
+ it 'executes the refresh and retry without raising an error' do
98
+ dummy_request = double("dummy_request")
99
+ expect(dummy_request).to receive(:call).twice do |auth|
100
+ expired_token = ::RDStation::Error::ExpiredAccessToken.new({'error_message' => 'x'})
101
+ raise expired_token unless auth.access_token == new_access_token
102
+ end
103
+
104
+ expect do
105
+ subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
106
+ end.not_to raise_error
107
+ end
108
+ end
109
+ end
110
+
111
+ context 'original request raised a non retryable exception' do
112
+ it 'raises error' do
113
+ dummy_request = double("dummy_request")
114
+ expect(dummy_request).to receive(:call).once do |_|
115
+ raise RuntimeError.new("a non retryable error")
116
+ end
117
+
118
+ expect do
119
+ subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
120
+ end.to raise_error RuntimeError
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'all legacy scenarios' do
126
+ let (:access_token) { 'access_token' }
127
+ let (:auth) { ::RDStation::Authorization.new(access_token: access_token) }
128
+
129
+ it 'implement me' do
130
+ dummy_request = double("dummy_request")
131
+ expect(dummy_request).to receive(:call).once do |_|
132
+ raise ::RDStation::Error::ExpiredAccessToken.new({'error_message' => 'x'})
133
+ end
134
+
135
+ expect do
136
+ subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
137
+ end.to raise_error ::RDStation::Error::ExpiredAccessToken
138
+ end
139
+ end
140
+
141
+ end
142
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe RDStation::Webhooks do
4
4
  let(:webhooks_client) do
5
- described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: 'access_token'))
5
+ described_class.new(authorization: RDStation::Authorization.new(access_token: 'access_token'))
6
6
  end
7
7
 
8
8
  let(:webhooks_endpoint) { 'https://api.rd.services/integrations/webhooks/' }
@@ -23,6 +23,11 @@ RSpec.describe RDStation::Webhooks do
23
23
  end
24
24
 
25
25
  describe '#all' do
26
+ it 'calls retryable_request' do
27
+ expect(webhooks_client).to receive(:retryable_request)
28
+ webhooks_client.all
29
+ end
30
+
26
31
  context 'when the request is successful' do
27
32
  let(:webhooks) do
28
33
  {
@@ -77,6 +82,11 @@ RSpec.describe RDStation::Webhooks do
77
82
  let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
78
83
  let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
79
84
 
85
+ it 'calls retryable_request' do
86
+ expect(webhooks_client).to receive(:retryable_request)
87
+ webhooks_client.by_uuid('uuid')
88
+ end
89
+
80
90
  context 'when the request is successful' do
81
91
  let(:webhook) do
82
92
  {
@@ -126,6 +136,11 @@ RSpec.describe RDStation::Webhooks do
126
136
  }
127
137
  end
128
138
 
139
+ it 'calls retryable_request' do
140
+ expect(webhooks_client).to receive(:retryable_request)
141
+ webhooks_client.create('payload')
142
+ end
143
+
129
144
  context 'when the request is successful' do
130
145
  let(:webhook) do
131
146
  {
@@ -177,6 +192,11 @@ RSpec.describe RDStation::Webhooks do
177
192
  }
178
193
  end
179
194
 
195
+ it 'calls retryable_request' do
196
+ expect(webhooks_client).to receive(:retryable_request)
197
+ webhooks_client.update('uuid', 'payload')
198
+ end
199
+
180
200
  context 'when the request is successful' do
181
201
  let(:updated_webhook) do
182
202
  {
@@ -219,6 +239,11 @@ RSpec.describe RDStation::Webhooks do
219
239
  let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
220
240
  let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
221
241
 
242
+ it 'calls retryable_request' do
243
+ expect(webhooks_client).to receive(:retryable_request)
244
+ webhooks_client.delete('uuid')
245
+ end
246
+
222
247
  context 'when the request is successful' do
223
248
  before do
224
249
  stub_request(:delete, webhooks_endpoint_by_uuid).with(headers: headers).to_return(status: 204)
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RDStation do
4
+ describe '.configure' do
5
+ let(:client_id) { 'client_id' }
6
+ let(:client_secret) { 'client_secret' }
7
+
8
+ it 'sets the configuration' do
9
+ RDStation.configure do |config|
10
+ config.client_id = client_id
11
+ config.client_secret = client_secret
12
+ end
13
+
14
+ expect(RDStation.configuration.client_id).to eq client_id
15
+ expect(RDStation.configuration.client_secret).to eq client_secret
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdstation-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo L F Casaretto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-15 00:00:00.000000000 Z
11
+ date: 2019-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: httparty
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -125,9 +139,10 @@ files:
125
139
  - README.md
126
140
  - Rakefile
127
141
  - lib/rdstation-ruby-client.rb
142
+ - lib/rdstation.rb
128
143
  - lib/rdstation/api_response.rb
129
144
  - lib/rdstation/authentication.rb
130
- - lib/rdstation/authorization_header.rb
145
+ - lib/rdstation/authorization.rb
131
146
  - lib/rdstation/client.rb
132
147
  - lib/rdstation/contacts.rb
133
148
  - lib/rdstation/error.rb
@@ -143,12 +158,14 @@ files:
143
158
  - lib/rdstation/error_handler/unauthorized.rb
144
159
  - lib/rdstation/events.rb
145
160
  - lib/rdstation/fields.rb
161
+ - lib/rdstation/retryable_request.rb
146
162
  - lib/rdstation/version.rb
147
163
  - lib/rdstation/webhooks.rb
148
164
  - rdstation-ruby-client.gemspec
149
165
  - spec/lib/rdstation-ruby-client_spec.rb
166
+ - spec/lib/rdstation/api_response_spec.rb
150
167
  - spec/lib/rdstation/authentication_spec.rb
151
- - spec/lib/rdstation/authorization_header_spec.rb
168
+ - spec/lib/rdstation/authorization_spec.rb
152
169
  - spec/lib/rdstation/client_spec.rb
153
170
  - spec/lib/rdstation/contacts_spec.rb
154
171
  - spec/lib/rdstation/error/format_spec.rb
@@ -163,7 +180,9 @@ files:
163
180
  - spec/lib/rdstation/error_spec.rb
164
181
  - spec/lib/rdstation/events_spec.rb
165
182
  - spec/lib/rdstation/fields_spec.rb
183
+ - spec/lib/rdstation/retryable_request_spec.rb
166
184
  - spec/lib/rdstation/webhooks_spec.rb
185
+ - spec/lib/rdstation_spec.rb
167
186
  - spec/spec_helper.rb
168
187
  homepage: http://resultadosdigitais.com.br
169
188
  licenses:
@@ -184,15 +203,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
203
  - !ruby/object:Gem::Version
185
204
  version: '0'
186
205
  requirements: []
187
- rubyforge_project:
188
- rubygems_version: 2.6.8
206
+ rubygems_version: 3.0.6
189
207
  signing_key:
190
208
  specification_version: 4
191
209
  summary: Ruby API wrapper for RD Station
192
210
  test_files:
193
211
  - spec/lib/rdstation-ruby-client_spec.rb
212
+ - spec/lib/rdstation/api_response_spec.rb
194
213
  - spec/lib/rdstation/authentication_spec.rb
195
- - spec/lib/rdstation/authorization_header_spec.rb
214
+ - spec/lib/rdstation/authorization_spec.rb
196
215
  - spec/lib/rdstation/client_spec.rb
197
216
  - spec/lib/rdstation/contacts_spec.rb
198
217
  - spec/lib/rdstation/error/format_spec.rb
@@ -207,5 +226,7 @@ test_files:
207
226
  - spec/lib/rdstation/error_spec.rb
208
227
  - spec/lib/rdstation/events_spec.rb
209
228
  - spec/lib/rdstation/fields_spec.rb
229
+ - spec/lib/rdstation/retryable_request_spec.rb
210
230
  - spec/lib/rdstation/webhooks_spec.rb
231
+ - spec/lib/rdstation_spec.rb
211
232
  - spec/spec_helper.rb