rdstation-ruby-client 1.2.1 → 2.0.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.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/rdsm-ruby-client-issue-template.md +49 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +59 -0
- data/README.md +154 -45
- data/lib/rdstation-ruby-client.rb +1 -0
- data/lib/rdstation/api_response.rb +3 -2
- data/lib/rdstation/authorization_header.rb +21 -0
- data/lib/rdstation/client.rb +11 -78
- data/lib/rdstation/contacts.rb +7 -11
- data/lib/rdstation/error.rb +22 -15
- data/lib/rdstation/error_handler.rb +27 -26
- data/lib/rdstation/error_handler/bad_request.rb +30 -0
- data/lib/rdstation/error_handler/unauthorized.rb +17 -9
- data/lib/rdstation/events.rb +4 -11
- data/lib/rdstation/fields.rb +4 -9
- data/lib/rdstation/version.rb +1 -1
- data/lib/rdstation/webhooks.rb +8 -12
- data/rdstation-ruby-client.gemspec +2 -0
- data/spec/lib/rdstation-ruby-client_spec.rb +1 -1
- data/spec/lib/rdstation/authorization_header_spec.rb +24 -0
- data/spec/lib/rdstation/client_spec.rb +37 -0
- data/spec/lib/rdstation/contacts_spec.rb +34 -41
- data/spec/lib/rdstation/error_handler/unauthorized_spec.rb +0 -29
- data/spec/lib/rdstation/error_handler_spec.rb +142 -29
- data/spec/lib/rdstation/events_spec.rb +15 -9
- data/spec/lib/rdstation/fields_spec.rb +5 -3
- data/spec/lib/rdstation/webhooks_spec.rb +16 -13
- metadata +12 -9
- data/lib/rdstation/error_handler/default.rb +0 -15
- data/lib/rdstation/error_handler/resource_not_found.rb +0 -24
- data/spec/lib/rdstation/error_handler/default_spec.rb +0 -14
- data/spec/lib/rdstation/error_handler/resource_not_found_spec.rb +0 -54
@@ -11,31 +11,38 @@ RSpec.describe RDStation::Contacts do
|
|
11
11
|
let(:endpoint_with_valid_email) { "https://api.rd.services/platform/contacts/email:#{valid_email}" }
|
12
12
|
let(:endpoint_with_invalid_email) { "https://api.rd.services/platform/contacts/email:#{invalid_email}" }
|
13
13
|
|
14
|
-
let(:
|
15
|
-
let(:
|
16
|
-
let(:
|
14
|
+
let(:valid_access_token) { 'valid_access_token' }
|
15
|
+
let(:invalid_access_token) { 'invalid_access_token' }
|
16
|
+
let(:expired_access_token) { 'expired_access_token' }
|
17
|
+
|
18
|
+
let(:contact_with_valid_token) do
|
19
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: valid_access_token))
|
20
|
+
end
|
21
|
+
let(:contact_with_expired_token) do
|
22
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: expired_access_token))
|
23
|
+
end
|
24
|
+
let(:contact_with_invalid_token) do
|
25
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: invalid_access_token))
|
26
|
+
end
|
17
27
|
|
18
|
-
let(:contact_with_valid_token) { described_class.new(valid_auth_token) }
|
19
|
-
let(:contact_with_expired_token) { described_class.new(expired_auth_token) }
|
20
|
-
let(:contact_with_invalid_token) { described_class.new(invalid_auth_token) }
|
21
28
|
|
22
29
|
let(:valid_headers) do
|
23
30
|
{
|
24
|
-
'Authorization' => "Bearer #{
|
31
|
+
'Authorization' => "Bearer #{valid_access_token}",
|
25
32
|
'Content-Type' => 'application/json'
|
26
33
|
}
|
27
34
|
end
|
28
35
|
|
29
36
|
let(:invalid_token_headers) do
|
30
37
|
{
|
31
|
-
'Authorization' => "Bearer #{
|
38
|
+
'Authorization' => "Bearer #{invalid_access_token}",
|
32
39
|
'Content-Type' => 'application/json'
|
33
40
|
}
|
34
41
|
end
|
35
42
|
|
36
43
|
let(:expired_token_headers) do
|
37
44
|
{
|
38
|
-
'Authorization' => "Bearer #{
|
45
|
+
'Authorization' => "Bearer #{expired_access_token}",
|
39
46
|
'Content-Type' => 'application/json'
|
40
47
|
}
|
41
48
|
end
|
@@ -130,7 +137,7 @@ RSpec.describe RDStation::Contacts do
|
|
130
137
|
it 'raises a not found error' do
|
131
138
|
expect do
|
132
139
|
contact_with_valid_token.by_uuid(invalid_uuid)
|
133
|
-
end.to raise_error(RDStation::Error::
|
140
|
+
end.to raise_error(RDStation::Error::NotFound)
|
134
141
|
end
|
135
142
|
end
|
136
143
|
end
|
@@ -193,7 +200,7 @@ RSpec.describe RDStation::Contacts do
|
|
193
200
|
it 'raises a not found error' do
|
194
201
|
expect do
|
195
202
|
contact_with_valid_token.by_email(invalid_email)
|
196
|
-
end.to raise_error(RDStation::Error::
|
203
|
+
end.to raise_error(RDStation::Error::NotFound)
|
197
204
|
end
|
198
205
|
end
|
199
206
|
end
|
@@ -228,11 +235,11 @@ RSpec.describe RDStation::Contacts do
|
|
228
235
|
end
|
229
236
|
|
230
237
|
describe '#update' do
|
231
|
-
context 'with a valid
|
232
|
-
let(:
|
238
|
+
context 'with a valid access_token' do
|
239
|
+
let(:valid_access_token) { 'valid_access_token' }
|
233
240
|
let(:headers) do
|
234
241
|
{
|
235
|
-
'Authorization' => "Bearer #{
|
242
|
+
'Authorization' => "Bearer #{valid_access_token}",
|
236
243
|
'Content-Type' => 'application/json'
|
237
244
|
}
|
238
245
|
end
|
@@ -264,16 +271,16 @@ RSpec.describe RDStation::Contacts do
|
|
264
271
|
it 'raises a not found error' do
|
265
272
|
expect do
|
266
273
|
contact_with_valid_token.update(invalid_uuid, {})
|
267
|
-
end.to raise_error(RDStation::Error::
|
274
|
+
end.to raise_error(RDStation::Error::NotFound)
|
268
275
|
end
|
269
276
|
end
|
270
277
|
end
|
271
278
|
|
272
279
|
context 'with an invalid auth token' do
|
273
|
-
let(:
|
280
|
+
let(:invalid_access_token) { 'invalid_access_token' }
|
274
281
|
let(:headers) do
|
275
282
|
{
|
276
|
-
'Authorization' => "Bearer #{
|
283
|
+
'Authorization' => "Bearer #{invalid_access_token}",
|
277
284
|
'Content-Type' => 'application/json'
|
278
285
|
}
|
279
286
|
end
|
@@ -292,10 +299,10 @@ RSpec.describe RDStation::Contacts do
|
|
292
299
|
end
|
293
300
|
|
294
301
|
context 'with an expired auth token' do
|
295
|
-
let(:
|
302
|
+
let(:expired_access_token) { 'expired_access_token' }
|
296
303
|
let(:headers) do
|
297
304
|
{
|
298
|
-
'Authorization' => "Bearer #{
|
305
|
+
'Authorization' => "Bearer #{expired_access_token}",
|
299
306
|
'Content-Type' => 'application/json'
|
300
307
|
}
|
301
308
|
end
|
@@ -315,12 +322,12 @@ RSpec.describe RDStation::Contacts do
|
|
315
322
|
end
|
316
323
|
|
317
324
|
describe '#upsert' do
|
318
|
-
context 'with a valid
|
319
|
-
let(:
|
325
|
+
context 'with a valid access_token' do
|
326
|
+
let(:valid_access_token) { 'valid_access_token' }
|
320
327
|
|
321
328
|
let(:headers) do
|
322
329
|
{
|
323
|
-
'Authorization' => "Bearer #{
|
330
|
+
'Authorization' => "Bearer #{valid_access_token}",
|
324
331
|
'Content-Type' => 'application/json'
|
325
332
|
}
|
326
333
|
end
|
@@ -352,7 +359,7 @@ RSpec.describe RDStation::Contacts do
|
|
352
359
|
it 'raises a not found error' do
|
353
360
|
expect do
|
354
361
|
contact_with_valid_token.upsert('email', invalid_email, {})
|
355
|
-
end.to raise_error(RDStation::Error::
|
362
|
+
end.to raise_error(RDStation::Error::NotFound)
|
356
363
|
end
|
357
364
|
end
|
358
365
|
|
@@ -371,27 +378,13 @@ RSpec.describe RDStation::Contacts do
|
|
371
378
|
end.to raise_error(RDStation::Error::ConflictingField)
|
372
379
|
end
|
373
380
|
end
|
374
|
-
|
375
|
-
context 'when an unrecognized error occurs' do
|
376
|
-
before do
|
377
|
-
stub_request(:patch, endpoint_with_valid_email)
|
378
|
-
.with(headers: headers)
|
379
|
-
.to_return(unrecognized_error)
|
380
|
-
end
|
381
|
-
|
382
|
-
it 'raises an default error' do
|
383
|
-
expect do
|
384
|
-
contact_with_valid_token.upsert('email', valid_email, {})
|
385
|
-
end.to raise_error(RDStation::Error::Default)
|
386
|
-
end
|
387
|
-
end
|
388
381
|
end
|
389
382
|
|
390
383
|
context 'with an invalid auth token' do
|
391
|
-
let(:
|
384
|
+
let(:invalid_access_token) { 'invalid_access_token' }
|
392
385
|
let(:headers) do
|
393
386
|
{
|
394
|
-
'Authorization' => "Bearer #{
|
387
|
+
'Authorization' => "Bearer #{invalid_access_token}",
|
395
388
|
'Content-Type' => 'application/json'
|
396
389
|
}
|
397
390
|
end
|
@@ -410,10 +403,10 @@ RSpec.describe RDStation::Contacts do
|
|
410
403
|
end
|
411
404
|
|
412
405
|
context 'with an expired auth token' do
|
413
|
-
let(:
|
406
|
+
let(:expired_access_token) { 'expired_access_token' }
|
414
407
|
let(:headers) do
|
415
408
|
{
|
416
|
-
'Authorization' => "Bearer #{
|
409
|
+
'Authorization' => "Bearer #{expired_access_token}",
|
417
410
|
'Content-Type' => 'application/json'
|
418
411
|
}
|
419
412
|
end
|
@@ -21,34 +21,5 @@ RSpec.describe RDStation::ErrorHandler::Unauthorized do
|
|
21
21
|
end.to raise_error(RDStation::Error::Unauthorized, 'Error Message')
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
25
|
-
context 'when none of the errors are unauthorized errors' do
|
26
|
-
let(:errors) do
|
27
|
-
[
|
28
|
-
{
|
29
|
-
'error_message' => 'Error Message',
|
30
|
-
'error_type' => 'RANDOM_ERROR_TYPE'
|
31
|
-
},
|
32
|
-
{
|
33
|
-
'error_message' => 'Another Error Message',
|
34
|
-
'error_type' => 'ANOTHER_RANDOM_ERROR_TYPE'
|
35
|
-
}
|
36
|
-
]
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'does not raise an Unauthorized error' do
|
40
|
-
result = unauthorized_error.raise_error
|
41
|
-
expect(result).to be_nil
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'when there are no errors' do
|
46
|
-
let(:errors) { [] }
|
47
|
-
|
48
|
-
it 'does not raise an Unauthorized error' do
|
49
|
-
result = unauthorized_error.raise_error
|
50
|
-
expect(result).to be_nil
|
51
|
-
end
|
52
|
-
end
|
53
24
|
end
|
54
25
|
end
|
@@ -4,45 +4,158 @@ RSpec.describe RDStation::ErrorHandler do
|
|
4
4
|
describe '#raise_errors' do
|
5
5
|
subject(:error_handler) { described_class.new(error_response) }
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
let(:array_of_errors) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
'error_type' => 'ERROR_TYPE',
|
11
|
+
'error_message' => 'Error Message',
|
12
|
+
'headers' => { 'error' => 'header' },
|
13
|
+
'http_status' => http_status,
|
14
|
+
'body' => {
|
12
15
|
'errors' => {
|
13
|
-
'error_type' => '
|
16
|
+
'error_type' => 'ERROR_TYPE',
|
14
17
|
'error_message' => 'Error Message'
|
15
18
|
}
|
16
|
-
}
|
17
|
-
|
19
|
+
}
|
20
|
+
}
|
21
|
+
]
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:error_response) do
|
25
|
+
OpenStruct.new(
|
26
|
+
code: http_status,
|
27
|
+
headers: { 'error' => 'header' },
|
28
|
+
body: {
|
29
|
+
'errors' => {
|
30
|
+
'error_type' => 'ERROR_TYPE',
|
31
|
+
'error_message' => 'Error Message'
|
32
|
+
}
|
33
|
+
}.to_json
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with an error 400' do
|
38
|
+
let(:http_status) { 400 }
|
39
|
+
|
40
|
+
let(:bad_request_handler) { instance_double(RDStation::ErrorHandler::BadRequest, raise_error: 'raised error') }
|
41
|
+
|
42
|
+
before do
|
43
|
+
allow(RDStation::ErrorHandler::BadRequest).to receive(:new).with(array_of_errors).and_return(bad_request_handler)
|
18
44
|
end
|
19
45
|
|
20
|
-
it '
|
21
|
-
|
46
|
+
it 'calls the bad request error handler' do
|
47
|
+
error_handler.raise_error
|
48
|
+
expect(bad_request_handler).to have_received(:raise_error)
|
22
49
|
end
|
23
50
|
end
|
24
51
|
|
25
|
-
context '
|
26
|
-
let(:
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
)
|
52
|
+
context 'with an error 401' do
|
53
|
+
let(:http_status) { 401 }
|
54
|
+
|
55
|
+
let(:unauthorized_handler) { instance_double(RDStation::ErrorHandler::Unauthorized, raise_error: 'raised error') }
|
56
|
+
|
57
|
+
before do
|
58
|
+
allow(RDStation::ErrorHandler::Unauthorized).to receive(:new).with(array_of_errors).and_return(unauthorized_handler)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'calls the unauthorized error handler' do
|
62
|
+
error_handler.raise_error
|
63
|
+
expect(unauthorized_handler).to have_received(:raise_error)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with an error 403' do
|
68
|
+
let(:http_status) { 403 }
|
69
|
+
|
70
|
+
it 'raises a forbidden error' do
|
71
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::Forbidden, 'Error Message')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'with an error 404' do
|
76
|
+
let(:http_status) { 404 }
|
77
|
+
|
78
|
+
it 'raises a not found error' do
|
79
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::NotFound, 'Error Message')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'with an error 405' do
|
84
|
+
let(:http_status) { 405 }
|
85
|
+
|
86
|
+
it 'raises a method not allowed error' do
|
87
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::MethodNotAllowed, 'Error Message')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'with an error 406' do
|
92
|
+
let(:http_status) { 406 }
|
93
|
+
|
94
|
+
it 'raises a not acceptable error' do
|
95
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::NotAcceptable, 'Error Message')
|
37
96
|
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'with an error 409' do
|
100
|
+
let(:http_status) { 409 }
|
101
|
+
|
102
|
+
it 'raises a conflict error' do
|
103
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::Conflict, 'Error Message')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'with an error 415' do
|
108
|
+
let(:http_status) { 415 }
|
109
|
+
|
110
|
+
it 'raises an unsupported media type error' do
|
111
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::UnsupportedMediaType, 'Error Message')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'with an error 422' do
|
116
|
+
let(:http_status) { 422 }
|
117
|
+
|
118
|
+
it 'raises an unprocessable entity error' do
|
119
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::UnprocessableEntity, 'Error Message')
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'with an error 500' do
|
124
|
+
let(:http_status) { 500 }
|
125
|
+
|
126
|
+
it 'raises an internal server error' do
|
127
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::InternalServerError, 'Error Message')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'with an error 501' do
|
132
|
+
let(:http_status) { 501 }
|
133
|
+
|
134
|
+
it 'raises a not implemented error' do
|
135
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::NotImplemented, 'Error Message')
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'with an error 502' do
|
140
|
+
let(:http_status) { 502 }
|
141
|
+
|
142
|
+
it 'raises a bad gateway error' do
|
143
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::BadGateway, 'Error Message')
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'with an error 503' do
|
148
|
+
let(:http_status) { 503 }
|
149
|
+
|
150
|
+
it 'raises a service unavailable error' do
|
151
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::ServiceUnavailable, 'Error Message')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
context 'with 5xx error' do
|
155
|
+
let(:http_status) { 505 }
|
38
156
|
|
39
|
-
it 'raises
|
40
|
-
expect { error_handler.
|
41
|
-
expect(error.details).to be
|
42
|
-
expect(error.headers).to be
|
43
|
-
expect(error.body).to be
|
44
|
-
expect(error.http_status).to be
|
45
|
-
end
|
157
|
+
it 'raises a server error' do
|
158
|
+
expect { error_handler.raise_error }.to raise_error(RDStation::Error::ServerError, 'Error Message')
|
46
159
|
end
|
47
160
|
end
|
48
161
|
end
|
@@ -1,13 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe RDStation::Events do
|
4
|
-
let(:
|
5
|
-
let(:
|
6
|
-
let(:
|
4
|
+
let(:valid_access_token) { 'valid_access_token' }
|
5
|
+
let(:invalid_access_token) { 'invalid_access_token' }
|
6
|
+
let(:expired_access_token) { 'expired_access_token' }
|
7
7
|
|
8
|
-
let(:event_with_valid_token)
|
9
|
-
|
10
|
-
|
8
|
+
let(:event_with_valid_token) do
|
9
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: valid_access_token))
|
10
|
+
end
|
11
|
+
let(:event_with_expired_token) do
|
12
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: expired_access_token))
|
13
|
+
end
|
14
|
+
let(:event_with_invalid_token) do
|
15
|
+
described_class.new(authorization_header: RDStation::AuthorizationHeader.new(access_token: invalid_access_token))
|
16
|
+
end
|
11
17
|
|
12
18
|
let(:events_endpoint) { 'https://api.rd.services/platform/events' }
|
13
19
|
|
@@ -45,21 +51,21 @@ RSpec.describe RDStation::Events do
|
|
45
51
|
|
46
52
|
let(:valid_headers) do
|
47
53
|
{
|
48
|
-
'Authorization' => "Bearer #{
|
54
|
+
'Authorization' => "Bearer #{valid_access_token}",
|
49
55
|
'Content-Type' => 'application/json'
|
50
56
|
}
|
51
57
|
end
|
52
58
|
|
53
59
|
let(:invalid_token_headers) do
|
54
60
|
{
|
55
|
-
'Authorization' => "Bearer #{
|
61
|
+
'Authorization' => "Bearer #{invalid_access_token}",
|
56
62
|
'Content-Type' => 'application/json'
|
57
63
|
}
|
58
64
|
end
|
59
65
|
|
60
66
|
let(:expired_token_headers) do
|
61
67
|
{
|
62
|
-
'Authorization' => "Bearer #{
|
68
|
+
'Authorization' => "Bearer #{expired_access_token}",
|
63
69
|
'Content-Type' => 'application/json'
|
64
70
|
}
|
65
71
|
end
|