my_api_client 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e5581dc65458d9130b465c89479fa8fc877c78f67d1cf701c225d64cbef9577
4
- data.tar.gz: 86cae4eade33446a4ef2a5e54f44600e72ee868add6e3cfa5690fe8f1336df01
3
+ metadata.gz: 5e287e051f0699595e499150dd4e4c303e00a82f951ffc86b13afde142a3720e
4
+ data.tar.gz: 7725d581e64d01683a636f56e86d217010d536f827023d691faca3b5cbbe2264
5
5
  SHA512:
6
- metadata.gz: fce2d580f7c2622b2c2b1acc506e585434c29946ac81b6311e255cb53ac8e98276f30b79171e87610076376711f024b08b86724dcc5413328ba7488c9b52e983
7
- data.tar.gz: 9475d69090e5748e511e7000df1db586da11ee5f62fc5f35fad30fce203a6ca2beabcafcf28e8eaf505e4a58d45cd4f771da86dea74bdcc70cee7e072267751e
6
+ metadata.gz: c37ce83c7a27302b8e890087251c67b955e8398b95d08a78b47447401cf0b3332b9bcd66c0e46e4e578a5a51e80f0a4a5fcea576c69fd8d29be8fa5c1c670720
7
+ data.tar.gz: 9a42a194c50def52f2aca6f9c54aac80dd7e4191114aa24fef543f81fab235417b3ec1f86ff2f960cd77cd1ff17188df5c073a4574df80fed9963375060058ce
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log
2
2
 
3
+ ## v0.13.0 (Jan 21, 2020)
4
+
5
+ ### Feature
6
+
7
+ * [#180](https://github.com/ryz310/my_api_client/pull/180) Stub response on raising error ([@ryz310](https://github.com/ryz310))
8
+
3
9
  ## v0.12.0 (Jan 19, 2020)
4
10
 
5
11
  ### Feature
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- my_api_client (0.12.0)
4
+ my_api_client (0.13.0)
5
5
  activesupport (>= 4.2.0)
6
6
  faraday (>= 0.17.1)
7
7
  jsonpath
@@ -20,7 +20,7 @@ GEM
20
20
  ast (2.4.0)
21
21
  bugsnag (6.12.2)
22
22
  concurrent-ruby (~> 1.0)
23
- byebug (11.0.1)
23
+ byebug (11.1.0)
24
24
  coderay (1.1.2)
25
25
  concurrent-ruby (1.1.5)
26
26
  crack (0.4.3)
data/README.jp.md CHANGED
@@ -201,7 +201,21 @@ WIP
201
201
 
202
202
  #### MyApiClient::Error
203
203
 
204
- WIP
204
+ API リクエストのレスポンスが `error_handling` で定義した matcher に合致した場合、 `raise` で指定した例外処理が発生します。この例外クラスは `MyApiClient::Error` を継承している必要があります。
205
+
206
+ この例外クラスには `#params` というメソッドが存在し、リクエストやレスポンスのパラメータを参照することが出来ます。
207
+
208
+ ```ruby
209
+ begin
210
+ api_client.request
211
+ rescue MyApiClient::Error => e
212
+ e.params.inspect
213
+ # => {
214
+ # :request=>"#<MyApiClient::Params::Request#inspect>",
215
+ # :response=>"#<Sawyer::Response#inspect>",
216
+ # }
217
+ end
218
+ ```
205
219
 
206
220
  #### Bugsnag breadcrumbs
207
221
 
@@ -534,6 +548,25 @@ stub_api_client_all(ExampleApiClient, request: { raise: MyApiClient::Error })
534
548
  expect { execute_api_request }.to raise_error(MyApiClient::Error)
535
549
  ```
536
550
 
551
+ なお、発生した例外に含まれるレスポンスパラメータもスタブ化したい場合は、 `response` オプションと同時に指定することが可能です。
552
+
553
+ ```ruby
554
+ stub_api_client_all(
555
+ ExampleApiClient,
556
+ request: {
557
+ raise: MyApiClient::Error,
558
+ response: { message: 'error' }
559
+ }
560
+ )
561
+
562
+ begin
563
+ ExampleApiClient.new.request(user_id: 1)
564
+ rescue MyApiClient::Error => e
565
+ response_body = e.params.response.data.to_h
566
+ expect(response_body).to eq(message: 'error')
567
+ end
568
+ ```
569
+
537
570
  ## Contributing
538
571
 
539
572
  不具合の報告や Pull Request を歓迎しています。OSS という事で自分はなるべく頑張って英語を使うようにしていますが、日本語での報告でも大丈夫です :+1:
@@ -3,6 +3,11 @@
3
3
  module MyApiClient
4
4
  # Test helper module for RSpec
5
5
  module Stub
6
+ ERROR_MESSAGE =
7
+ 'If you use the `raise` option as an error instance, the `response` option ' \
8
+ 'is ignored. If you want to use both options, you need to specify the ' \
9
+ '`raise` option as an error class.'
10
+
6
11
  # Stubs all instance of arbitrary MyApiClient class.
7
12
  # And returns a stubbed arbitrary MyApiClient instance.
8
13
  #
@@ -16,7 +21,11 @@ module MyApiClient
16
21
  # get_user: { response: { id: 1 } }, # Returns an arbitrary response.
17
22
  # post_users: { id: 1 }, # You can ommit `response` keyword.
18
23
  # patch_user: ->(params) { { id: params[:id] } }, # Returns calculated result as response.
19
- # delete_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
24
+ # put_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
25
+ # delete_user: {
26
+ # raise: MyApiClient::ClientError,
27
+ # response: { errors: [{ code: 10 }] }, # You can stub response with exception.
28
+ # }
20
29
  # )
21
30
  # response = ExampleApiClient.new.get_user(id: 123)
22
31
  # response.id # => 1
@@ -40,7 +49,11 @@ module MyApiClient
40
49
  # get_user: { response: { id: 1 } }, # Returns an arbitrary response.
41
50
  # post_users: { id: 1 }, # You can ommit `response` keyword.
42
51
  # patch_user: ->(params) { { id: params[:id] } }, # Returns calculated result as response.
43
- # delete_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
52
+ # put_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
53
+ # delete_user: {
54
+ # raise: MyApiClient::ClientError,
55
+ # response: { errors: [{ code: 10 }] }, # You can stub response with exception.
56
+ # }
44
57
  # )
45
58
  # response = api_client.get_user(id: 123)
46
59
  # response.id # => 1
@@ -58,17 +71,18 @@ module MyApiClient
58
71
  def stubbing(instance, action, options)
59
72
  case options
60
73
  when Proc
61
- allow(instance).to receive(action) { |*request| stub_as_sawyer(options.call(*request)) }
74
+ allow(instance).to receive(action) { |*request| stub_as_resource(options.call(*request)) }
62
75
  when Hash
63
76
  if options[:raise].present?
64
- allow(instance).to receive(action).and_raise(process_raise_option(options[:raise]))
77
+ exception = process_raise_option(options[:raise], options[:response])
78
+ allow(instance).to receive(action).and_raise(exception)
65
79
  elsif options[:response]
66
- allow(instance).to receive(action).and_return(stub_as_sawyer(options[:response]))
80
+ allow(instance).to receive(action).and_return(stub_as_resource(options[:response]))
67
81
  else
68
- allow(instance).to receive(action).and_return(stub_as_sawyer(options))
82
+ allow(instance).to receive(action).and_return(stub_as_resource(options))
69
83
  end
70
84
  else
71
- allow(instance).to receive(action).and_return(stub_as_sawyer(options))
85
+ allow(instance).to receive(action).and_return(stub_as_resource(options))
72
86
  end
73
87
  end
74
88
  # rubocop:enable Metrics/AbcSize
@@ -81,26 +95,38 @@ module MyApiClient
81
95
  # @param exception [Clsas, MyApiClient::Error] Processing target.
82
96
  # @return [MyApiClient::Error] Processed exception.
83
97
  # @raise [RuntimeError] Unsupported error class was set.
84
- def process_raise_option(exception)
98
+ def process_raise_option(exception, response = {})
85
99
  case exception
86
100
  when Class
87
- params = instance_double(MyApiClient::Params::Params, metadata: {})
101
+ params = MyApiClient::Params::Params.new(nil, stub_as_response(response))
88
102
  if exception == MyApiClient::NetworkError
89
103
  exception.new(params, Net::OpenTimeout.new)
90
104
  else
91
105
  exception.new(params)
92
106
  end
93
107
  when MyApiClient::Error
108
+ raise ERROR_MESSAGE if response.present?
109
+
94
110
  exception
95
111
  else
96
112
  raise "Unsupported error class was set: #{exception.inspect}"
97
113
  end
98
114
  end
99
115
 
100
- def stub_as_sawyer(params)
116
+ def stub_as_response(params)
117
+ instance_double(
118
+ Sawyer::Response,
119
+ status: 400,
120
+ headers: {},
121
+ data: stub_as_resource(params),
122
+ timing: 0.123
123
+ )
124
+ end
125
+
126
+ def stub_as_resource(params)
101
127
  case params
102
128
  when Hash then Sawyer::Resource.new(agent, params)
103
- when Array then params.map { |hash| stub_as_sawyer(hash) }
129
+ when Array then params.map { |hash| stub_as_resource(hash) }
104
130
  when nil then nil
105
131
  else params
106
132
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MyApiClient
4
- VERSION = '0.12.0'
4
+ VERSION = '0.13.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryz310
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-19 00:00:00.000000000 Z
11
+ date: 2020-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport