my_api_client 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.jp.md +24 -0
- data/lib/my_api_client/rspec/matchers/be_handled_as_an_error.rb +18 -1
- data/lib/my_api_client/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b184fa1b59269203a189c396d43f1b9f63e91c38c6402c22ed77ce30c60f78e6
|
4
|
+
data.tar.gz: 05befe929b0d318c2668dfeb3627602c9985e4da4baf8b5a234469d772da4d78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b788bb9d4ac50f1c0c31f6d24df04e164fe325173f4ece5a01effb2b5b696ea68c7630b56a4e0f6a1b6e001da2ce46b1e4a568e9110729f75c5b7a5d086a20c0
|
7
|
+
data.tar.gz: a8d5a2aa67ce54336961ce0a1a79b2461cf38cfa82546f6b9ddbe1f33e09c8bc002aa2cefe2f1f093321d9a6bbaad4003ea5e63c29b4e8858eeb882178cdf7cd
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.jp.md
CHANGED
@@ -406,6 +406,30 @@ it do
|
|
406
406
|
end
|
407
407
|
```
|
408
408
|
|
409
|
+
##### `retry_on` を定義している場合
|
410
|
+
|
411
|
+
以下のように `retry_on` を API Client に定義している場合:
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
class ExampleApiClient < MyApiClient::Base
|
415
|
+
endpoint 'https://example.com'
|
416
|
+
|
417
|
+
error_handling json: { '$.errors.code': 20 }, raise: MyApiClient::ApiLimitError
|
418
|
+
retry_on MyApiClient::ApiLimitError, wait: 30.seconds, attempts: 3
|
419
|
+
end
|
420
|
+
```
|
421
|
+
|
422
|
+
`after_retry` と `times` という Custom Matcher を利用することが出来ます。
|
423
|
+
|
424
|
+
```ruby
|
425
|
+
it do
|
426
|
+
expect { api_client.get_users(condition: 'condition') }
|
427
|
+
.to be_handled_as_an_error(MyApiClient::ApiLimitError)
|
428
|
+
.after_retry(3).times
|
429
|
+
.when_receive(status_code: 200, body: { errors: { code: 20 } }.to_json)
|
430
|
+
end
|
431
|
+
```
|
432
|
+
|
409
433
|
### Stubbing
|
410
434
|
|
411
435
|
以下のような `ApiClient` を定義しているとします。
|
@@ -7,6 +7,7 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
|
|
7
7
|
|
8
8
|
match do |api_request|
|
9
9
|
init
|
10
|
+
set_validation_for_retry_count
|
10
11
|
handle_error(api_request).is_a? expected_error_class
|
11
12
|
end
|
12
13
|
|
@@ -15,8 +16,16 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
|
|
15
16
|
handle_error(api_request).nil?
|
16
17
|
end
|
17
18
|
|
19
|
+
chain :after_retry, :retry_count
|
20
|
+
chain(:times) { nil }
|
18
21
|
chain :when_receive, :expected_response
|
19
22
|
|
23
|
+
description do
|
24
|
+
message = "be handled as #{expected_error_class}"
|
25
|
+
message += " after retry #{retry_count} times" unless retry_count.nil?
|
26
|
+
message
|
27
|
+
end
|
28
|
+
|
20
29
|
failure_message do |api_request|
|
21
30
|
actual_error = handle_error(api_request)
|
22
31
|
if actual_error.nil?
|
@@ -34,6 +43,8 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
|
|
34
43
|
"but it was handled as #{actual_error.class.name}"
|
35
44
|
end
|
36
45
|
|
46
|
+
attr_reader :sawyer
|
47
|
+
|
37
48
|
def init
|
38
49
|
disable_logging
|
39
50
|
response = dummy_response(
|
@@ -41,10 +52,16 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
|
|
41
52
|
headers: expected_response[:headers] || {},
|
42
53
|
body: expected_response[:body] || nil
|
43
54
|
)
|
44
|
-
sawyer = instance_double(Sawyer::Agent, call: response)
|
55
|
+
@sawyer = instance_double(Sawyer::Agent, call: response, sleep: nil)
|
45
56
|
allow(Sawyer::Agent).to receive(:new).and_return(sawyer)
|
46
57
|
end
|
47
58
|
|
59
|
+
def set_validation_for_retry_count
|
60
|
+
return if retry_count.nil?
|
61
|
+
|
62
|
+
expect(sawyer).to receive(:call).exactly(retry_count + 1).times
|
63
|
+
end
|
64
|
+
|
48
65
|
def handle_error(api_request)
|
49
66
|
api_request.call
|
50
67
|
rescue MyApiClient::Error => e
|