rdstation-ruby-client 1.2.1 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/rdsm-ruby-client-issue-template.md +49 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +167 -0
  5. data/README.md +243 -43
  6. data/lib/rdstation-ruby-client.rb +6 -0
  7. data/lib/rdstation.rb +19 -0
  8. data/lib/rdstation/api_response.rb +3 -3
  9. data/lib/rdstation/authentication.rb +32 -3
  10. data/lib/rdstation/authorization.rb +24 -0
  11. data/lib/rdstation/builder/field.rb +70 -0
  12. data/lib/rdstation/client.rb +17 -74
  13. data/lib/rdstation/contacts.rb +21 -16
  14. data/lib/rdstation/error.rb +23 -15
  15. data/lib/rdstation/error/format.rb +21 -3
  16. data/lib/rdstation/error/formatter.rb +53 -7
  17. data/lib/rdstation/error_handler.rb +31 -26
  18. data/lib/rdstation/error_handler/bad_request.rb +30 -0
  19. data/lib/rdstation/error_handler/unauthorized.rb +17 -9
  20. data/lib/rdstation/events.rb +7 -19
  21. data/lib/rdstation/fields.rb +31 -7
  22. data/lib/rdstation/retryable_request.rb +35 -0
  23. data/lib/rdstation/version.rb +1 -1
  24. data/lib/rdstation/webhooks.rb +25 -17
  25. data/rdstation-ruby-client.gemspec +4 -1
  26. data/spec/lib/rdstation-ruby-client_spec.rb +1 -1
  27. data/spec/lib/rdstation/api_response_spec.rb +34 -0
  28. data/spec/lib/rdstation/authentication_spec.rb +164 -0
  29. data/spec/lib/rdstation/authorization_spec.rb +24 -0
  30. data/spec/lib/rdstation/builder/field_spec.rb +69 -0
  31. data/spec/lib/rdstation/client_spec.rb +37 -0
  32. data/spec/lib/rdstation/contacts_spec.rb +54 -41
  33. data/spec/lib/rdstation/error/format_spec.rb +46 -0
  34. data/spec/lib/rdstation/error/formatter_spec.rb +83 -0
  35. data/spec/lib/rdstation/error_handler/unauthorized_spec.rb +0 -29
  36. data/spec/lib/rdstation/error_handler_spec.rb +162 -26
  37. data/spec/lib/rdstation/events_spec.rb +20 -9
  38. data/spec/lib/rdstation/fields_spec.rb +10 -3
  39. data/spec/lib/rdstation/retryable_request_spec.rb +142 -0
  40. data/spec/lib/rdstation/webhooks_spec.rb +41 -13
  41. data/spec/lib/rdstation_spec.rb +18 -0
  42. metadata +41 -12
  43. data/lib/rdstation/error_handler/default.rb +0 -15
  44. data/lib/rdstation/error_handler/resource_not_found.rb +0 -24
  45. data/spec/lib/rdstation/error_handler/default_spec.rb +0 -14
  46. data/spec/lib/rdstation/error_handler/resource_not_found_spec.rb +0 -54
@@ -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
@@ -1,18 +1,21 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe RDStation::Webhooks do
4
- let(:webhooks_client) { described_class.new('auth_token') }
4
+ let(:webhooks_client) do
5
+ described_class.new(authorization: RDStation::Authorization.new(access_token: 'access_token'))
6
+ end
7
+
5
8
  let(:webhooks_endpoint) { 'https://api.rd.services/integrations/webhooks/' }
6
9
 
7
10
  let(:headers) do
8
11
  {
9
- 'Authorization' => 'Bearer auth_token',
12
+ 'Authorization' => 'Bearer access_token',
10
13
  'Content-Type' => 'application/json'
11
14
  }
12
15
  end
13
16
 
14
17
  let(:error_handler) do
15
- instance_double(RDStation::ErrorHandler, raise_errors: 'mock raised errors')
18
+ instance_double(RDStation::ErrorHandler, raise_error: 'mock raised errors')
16
19
  end
17
20
 
18
21
  before do
@@ -20,6 +23,11 @@ RSpec.describe RDStation::Webhooks do
20
23
  end
21
24
 
22
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
+
23
31
  context 'when the request is successful' do
24
32
  let(:webhooks) do
25
33
  {
@@ -63,9 +71,9 @@ RSpec.describe RDStation::Webhooks do
63
71
  .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
64
72
  end
65
73
 
66
- it 'calls raise_errors on error handler' do
74
+ it 'calls raise_error on error handler' do
67
75
  webhooks_client.all
68
- expect(error_handler).to have_received(:raise_errors)
76
+ expect(error_handler).to have_received(:raise_error)
69
77
  end
70
78
  end
71
79
  end
@@ -74,6 +82,11 @@ RSpec.describe RDStation::Webhooks do
74
82
  let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
75
83
  let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
76
84
 
85
+ it 'calls retryable_request' do
86
+ expect(webhooks_client).to receive(:retryable_request)
87
+ webhooks_client.by_uuid('uuid')
88
+ end
89
+
77
90
  context 'when the request is successful' do
78
91
  let(:webhook) do
79
92
  {
@@ -105,9 +118,9 @@ RSpec.describe RDStation::Webhooks do
105
118
  .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
106
119
  end
107
120
 
108
- it 'calls raise_errors on error handler' do
121
+ it 'calls raise_error on error handler' do
109
122
  webhooks_client.by_uuid(uuid)
110
- expect(error_handler).to have_received(:raise_errors)
123
+ expect(error_handler).to have_received(:raise_error)
111
124
  end
112
125
  end
113
126
  end
@@ -123,6 +136,11 @@ RSpec.describe RDStation::Webhooks do
123
136
  }
124
137
  end
125
138
 
139
+ it 'calls retryable_request' do
140
+ expect(webhooks_client).to receive(:retryable_request)
141
+ webhooks_client.create('payload')
142
+ end
143
+
126
144
  context 'when the request is successful' do
127
145
  let(:webhook) do
128
146
  {
@@ -154,9 +172,9 @@ RSpec.describe RDStation::Webhooks do
154
172
  .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
155
173
  end
156
174
 
157
- it 'calls raise_errors on error handler' do
175
+ it 'calls raise_error on error handler' do
158
176
  webhooks_client.create(payload)
159
- expect(error_handler).to have_received(:raise_errors)
177
+ expect(error_handler).to have_received(:raise_error)
160
178
  end
161
179
  end
162
180
  end
@@ -174,6 +192,11 @@ RSpec.describe RDStation::Webhooks do
174
192
  }
175
193
  end
176
194
 
195
+ it 'calls retryable_request' do
196
+ expect(webhooks_client).to receive(:retryable_request)
197
+ webhooks_client.update('uuid', 'payload')
198
+ end
199
+
177
200
  context 'when the request is successful' do
178
201
  let(:updated_webhook) do
179
202
  {
@@ -205,9 +228,9 @@ RSpec.describe RDStation::Webhooks do
205
228
  .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
206
229
  end
207
230
 
208
- it 'calls raise_errors on error handler' do
231
+ it 'calls raise_error on error handler' do
209
232
  webhooks_client.update(uuid, new_payload)
210
- expect(error_handler).to have_received(:raise_errors)
233
+ expect(error_handler).to have_received(:raise_error)
211
234
  end
212
235
  end
213
236
  end
@@ -216,6 +239,11 @@ RSpec.describe RDStation::Webhooks do
216
239
  let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
217
240
  let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
218
241
 
242
+ it 'calls retryable_request' do
243
+ expect(webhooks_client).to receive(:retryable_request)
244
+ webhooks_client.delete('uuid')
245
+ end
246
+
219
247
  context 'when the request is successful' do
220
248
  before do
221
249
  stub_request(:delete, webhooks_endpoint_by_uuid).with(headers: headers).to_return(status: 204)
@@ -234,9 +262,9 @@ RSpec.describe RDStation::Webhooks do
234
262
  .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
235
263
  end
236
264
 
237
- it 'calls raise_errors on error handler' do
265
+ it 'calls raise_error on error handler' do
238
266
  webhooks_client.delete(uuid)
239
- expect(error_handler).to have_received(:raise_errors)
267
+ expect(error_handler).to have_received(:raise_error)
240
268
  end
241
269
  end
242
270
  end
@@ -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: 1.2.1
4
+ version: 2.3.1
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-04-05 00:00:00.000000000 Z
11
+ date: 2020-08-11 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
@@ -116,51 +130,61 @@ extensions: []
116
130
  extra_rdoc_files: []
117
131
  files:
118
132
  - ".circleci/config.yml"
133
+ - ".github/ISSUE_TEMPLATE/rdsm-ruby-client-issue-template.md"
119
134
  - ".gitignore"
135
+ - ".rspec"
136
+ - CHANGELOG.md
120
137
  - Gemfile
121
138
  - LICENSE
122
139
  - README.md
123
140
  - Rakefile
124
141
  - lib/rdstation-ruby-client.rb
142
+ - lib/rdstation.rb
125
143
  - lib/rdstation/api_response.rb
126
144
  - lib/rdstation/authentication.rb
145
+ - lib/rdstation/authorization.rb
146
+ - lib/rdstation/builder/field.rb
127
147
  - lib/rdstation/client.rb
128
148
  - lib/rdstation/contacts.rb
129
149
  - lib/rdstation/error.rb
130
150
  - lib/rdstation/error/format.rb
131
151
  - lib/rdstation/error/formatter.rb
132
152
  - lib/rdstation/error_handler.rb
153
+ - lib/rdstation/error_handler/bad_request.rb
133
154
  - lib/rdstation/error_handler/conflicting_field.rb
134
- - lib/rdstation/error_handler/default.rb
135
155
  - lib/rdstation/error_handler/expired_access_token.rb
136
156
  - lib/rdstation/error_handler/expired_code_grant.rb
137
157
  - lib/rdstation/error_handler/invalid_credentials.rb
138
158
  - lib/rdstation/error_handler/invalid_event_type.rb
139
- - lib/rdstation/error_handler/resource_not_found.rb
140
159
  - lib/rdstation/error_handler/unauthorized.rb
141
160
  - lib/rdstation/events.rb
142
161
  - lib/rdstation/fields.rb
162
+ - lib/rdstation/retryable_request.rb
143
163
  - lib/rdstation/version.rb
144
164
  - lib/rdstation/webhooks.rb
145
165
  - rdstation-ruby-client.gemspec
146
166
  - spec/lib/rdstation-ruby-client_spec.rb
167
+ - spec/lib/rdstation/api_response_spec.rb
147
168
  - spec/lib/rdstation/authentication_spec.rb
169
+ - spec/lib/rdstation/authorization_spec.rb
170
+ - spec/lib/rdstation/builder/field_spec.rb
171
+ - spec/lib/rdstation/client_spec.rb
148
172
  - spec/lib/rdstation/contacts_spec.rb
149
173
  - spec/lib/rdstation/error/format_spec.rb
150
174
  - spec/lib/rdstation/error/formatter_spec.rb
151
175
  - spec/lib/rdstation/error_handler/conflicting_field_spec.rb
152
- - spec/lib/rdstation/error_handler/default_spec.rb
153
176
  - spec/lib/rdstation/error_handler/expired_access_token_spec.rb
154
177
  - spec/lib/rdstation/error_handler/expired_code_grant_spec.rb
155
178
  - spec/lib/rdstation/error_handler/invalid_credentials_spec.rb
156
179
  - spec/lib/rdstation/error_handler/invalid_event_type_spec.rb
157
- - spec/lib/rdstation/error_handler/resource_not_found_spec.rb
158
180
  - spec/lib/rdstation/error_handler/unauthorized_spec.rb
159
181
  - spec/lib/rdstation/error_handler_spec.rb
160
182
  - spec/lib/rdstation/error_spec.rb
161
183
  - spec/lib/rdstation/events_spec.rb
162
184
  - spec/lib/rdstation/fields_spec.rb
185
+ - spec/lib/rdstation/retryable_request_spec.rb
163
186
  - spec/lib/rdstation/webhooks_spec.rb
187
+ - spec/lib/rdstation_spec.rb
164
188
  - spec/spec_helper.rb
165
189
  homepage: http://resultadosdigitais.com.br
166
190
  licenses:
@@ -174,34 +198,39 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
198
  requirements:
175
199
  - - ">="
176
200
  - !ruby/object:Gem::Version
177
- version: '0'
201
+ version: 2.0.0
178
202
  required_rubygems_version: !ruby/object:Gem::Requirement
179
203
  requirements:
180
204
  - - ">="
181
205
  - !ruby/object:Gem::Version
182
206
  version: '0'
183
207
  requirements: []
184
- rubygems_version: 3.0.2
208
+ rubyforge_project:
209
+ rubygems_version: 2.7.6.2
185
210
  signing_key:
186
211
  specification_version: 4
187
212
  summary: Ruby API wrapper for RD Station
188
213
  test_files:
189
214
  - spec/lib/rdstation-ruby-client_spec.rb
215
+ - spec/lib/rdstation/api_response_spec.rb
190
216
  - spec/lib/rdstation/authentication_spec.rb
217
+ - spec/lib/rdstation/authorization_spec.rb
218
+ - spec/lib/rdstation/builder/field_spec.rb
219
+ - spec/lib/rdstation/client_spec.rb
191
220
  - spec/lib/rdstation/contacts_spec.rb
192
221
  - spec/lib/rdstation/error/format_spec.rb
193
222
  - spec/lib/rdstation/error/formatter_spec.rb
194
223
  - spec/lib/rdstation/error_handler/conflicting_field_spec.rb
195
- - spec/lib/rdstation/error_handler/default_spec.rb
196
224
  - spec/lib/rdstation/error_handler/expired_access_token_spec.rb
197
225
  - spec/lib/rdstation/error_handler/expired_code_grant_spec.rb
198
226
  - spec/lib/rdstation/error_handler/invalid_credentials_spec.rb
199
227
  - spec/lib/rdstation/error_handler/invalid_event_type_spec.rb
200
- - spec/lib/rdstation/error_handler/resource_not_found_spec.rb
201
228
  - spec/lib/rdstation/error_handler/unauthorized_spec.rb
202
229
  - spec/lib/rdstation/error_handler_spec.rb
203
230
  - spec/lib/rdstation/error_spec.rb
204
231
  - spec/lib/rdstation/events_spec.rb
205
232
  - spec/lib/rdstation/fields_spec.rb
233
+ - spec/lib/rdstation/retryable_request_spec.rb
206
234
  - spec/lib/rdstation/webhooks_spec.rb
235
+ - spec/lib/rdstation_spec.rb
207
236
  - spec/spec_helper.rb