rdstation-ruby-client 2.0.0 → 2.4.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +116 -4
  3. data/README.md +114 -22
  4. data/lib/rdstation-ruby-client.rb +6 -1
  5. data/lib/rdstation.rb +19 -0
  6. data/lib/rdstation/api_response.rb +1 -2
  7. data/lib/rdstation/authentication.rb +32 -3
  8. data/lib/rdstation/{authorization_header.rb → authorization.rb} +11 -8
  9. data/lib/rdstation/builder/field.rb +70 -0
  10. data/lib/rdstation/client.rb +17 -7
  11. data/lib/rdstation/contacts.rb +22 -13
  12. data/lib/rdstation/error.rb +2 -0
  13. data/lib/rdstation/error/format.rb +29 -3
  14. data/lib/rdstation/error/formatter.rb +69 -8
  15. data/lib/rdstation/error_handler.rb +6 -1
  16. data/lib/rdstation/events.rb +7 -12
  17. data/lib/rdstation/fields.rb +35 -6
  18. data/lib/rdstation/retryable_request.rb +35 -0
  19. data/lib/rdstation/version.rb +1 -1
  20. data/lib/rdstation/webhooks.rb +25 -13
  21. data/rdstation-ruby-client.gemspec +2 -1
  22. data/spec/lib/rdstation/api_response_spec.rb +34 -0
  23. data/spec/lib/rdstation/authentication_spec.rb +164 -0
  24. data/spec/lib/rdstation/{authorization_header_spec.rb → authorization_spec.rb} +3 -3
  25. data/spec/lib/rdstation/builder/field_spec.rb +69 -0
  26. data/spec/lib/rdstation/client_spec.rb +6 -6
  27. data/spec/lib/rdstation/contacts_spec.rb +23 -3
  28. data/spec/lib/rdstation/error/format_spec.rb +63 -0
  29. data/spec/lib/rdstation/error/formatter_spec.rb +113 -0
  30. data/spec/lib/rdstation/error_handler_spec.rb +23 -0
  31. data/spec/lib/rdstation/events_spec.rb +8 -3
  32. data/spec/lib/rdstation/fields_spec.rb +6 -1
  33. data/spec/lib/rdstation/retryable_request_spec.rb +142 -0
  34. data/spec/lib/rdstation/webhooks_spec.rb +26 -1
  35. data/spec/lib/rdstation_spec.rb +18 -0
  36. metadata +36 -11
@@ -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.0.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo L F Casaretto
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2020-09-16 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,11 @@ 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
146
+ - lib/rdstation/builder/field.rb
131
147
  - lib/rdstation/client.rb
132
148
  - lib/rdstation/contacts.rb
133
149
  - lib/rdstation/error.rb
@@ -143,12 +159,15 @@ files:
143
159
  - lib/rdstation/error_handler/unauthorized.rb
144
160
  - lib/rdstation/events.rb
145
161
  - lib/rdstation/fields.rb
162
+ - lib/rdstation/retryable_request.rb
146
163
  - lib/rdstation/version.rb
147
164
  - lib/rdstation/webhooks.rb
148
165
  - rdstation-ruby-client.gemspec
149
166
  - spec/lib/rdstation-ruby-client_spec.rb
167
+ - spec/lib/rdstation/api_response_spec.rb
150
168
  - spec/lib/rdstation/authentication_spec.rb
151
- - spec/lib/rdstation/authorization_header_spec.rb
169
+ - spec/lib/rdstation/authorization_spec.rb
170
+ - spec/lib/rdstation/builder/field_spec.rb
152
171
  - spec/lib/rdstation/client_spec.rb
153
172
  - spec/lib/rdstation/contacts_spec.rb
154
173
  - spec/lib/rdstation/error/format_spec.rb
@@ -163,13 +182,15 @@ files:
163
182
  - spec/lib/rdstation/error_spec.rb
164
183
  - spec/lib/rdstation/events_spec.rb
165
184
  - spec/lib/rdstation/fields_spec.rb
185
+ - spec/lib/rdstation/retryable_request_spec.rb
166
186
  - spec/lib/rdstation/webhooks_spec.rb
187
+ - spec/lib/rdstation_spec.rb
167
188
  - spec/spec_helper.rb
168
189
  homepage: http://resultadosdigitais.com.br
169
190
  licenses:
170
191
  - MIT
171
192
  metadata: {}
172
- post_install_message:
193
+ post_install_message:
173
194
  rdoc_options: []
174
195
  require_paths:
175
196
  - lib
@@ -184,14 +205,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
205
  - !ruby/object:Gem::Version
185
206
  version: '0'
186
207
  requirements: []
187
- rubygems_version: 3.0.2
188
- signing_key:
208
+ rubygems_version: 3.0.8
209
+ signing_key:
189
210
  specification_version: 4
190
211
  summary: Ruby API wrapper for RD Station
191
212
  test_files:
192
213
  - spec/lib/rdstation-ruby-client_spec.rb
214
+ - spec/lib/rdstation/api_response_spec.rb
193
215
  - spec/lib/rdstation/authentication_spec.rb
194
- - spec/lib/rdstation/authorization_header_spec.rb
216
+ - spec/lib/rdstation/authorization_spec.rb
217
+ - spec/lib/rdstation/builder/field_spec.rb
195
218
  - spec/lib/rdstation/client_spec.rb
196
219
  - spec/lib/rdstation/contacts_spec.rb
197
220
  - spec/lib/rdstation/error/format_spec.rb
@@ -206,5 +229,7 @@ test_files:
206
229
  - spec/lib/rdstation/error_spec.rb
207
230
  - spec/lib/rdstation/events_spec.rb
208
231
  - spec/lib/rdstation/fields_spec.rb
232
+ - spec/lib/rdstation/retryable_request_spec.rb
209
233
  - spec/lib/rdstation/webhooks_spec.rb
234
+ - spec/lib/rdstation_spec.rb
210
235
  - spec/spec_helper.rb