my_api_client 0.8.0 → 0.9.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: 1e548093a65be2816e7e86939b5570ce245af19b2422180cba0d9cff7f2f15ed
4
- data.tar.gz: 0cbafd89253c574bc85cba334a6e90349b51af033aec2b8bcc5076cef22824d7
3
+ metadata.gz: 38d80eab152ca28a8dbd5456ed21cbd85c123a8ae5a25183a7a843a97a7a0f9e
4
+ data.tar.gz: ecf27feccc823ba7c45950b71ee5a8f7459bda3992ab81209c03eca957604e6f
5
5
  SHA512:
6
- metadata.gz: b370c044f12d7d9862bbe3cb1d038a0f7a949acbf2390a442880d59db33f5f5d42ff73e8f205f4696ed2713b3102b86b6cc2a473e218ee2269ea385c6d80c890
7
- data.tar.gz: e32efde0fd72efc444dbac9f5435cf9682fc5c18e4d0a8dc09571de9c4d29b8a36b375cee239a39cb05f4af65f2075cd245e6f985d01d11e4166ed0cc46b936f
6
+ metadata.gz: e23a635d8a9342a536202d29dffe54d9bc2973e7e555f0d340a59862aad977bffd2501dad8af159c874cf56ca48b3ae0c6fe258d12584b81e96114d426bd9aa0
7
+ data.tar.gz: '067183e46ad3421bb6d48c22c89201e2eaa9e490993264afb0a0500f136a0ef2abee5826b39f777902eb11c1c5bb61c93c66291f5c5d18480fd7bdc3289f3a36'
data/.rubocop_todo.yml CHANGED
@@ -1,7 +1,17 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-07-16 23:31:21 +0000 using RuboCop version 0.73.0.
3
+ # on 2019-07-23 23:30:32 +0000 using RuboCop version 0.73.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 18
10
+ RSpec/LeakyConstantDeclaration:
11
+ Exclude:
12
+ - 'spec/lib/my_api_client/base_spec.rb'
13
+ - 'spec/lib/my_api_client/config_spec.rb'
14
+ - 'spec/lib/my_api_client/error_handling_spec.rb'
15
+ - 'spec/lib/my_api_client/exceptions_spec.rb'
16
+ - 'spec/lib/my_api_client/request_spec.rb'
17
+ - 'spec/lib/my_api_client/rspec/stub_spec.rb'
data/CHANGELOG.md CHANGED
@@ -1,50 +1,60 @@
1
1
  # Change log
2
2
 
3
+ ## 0.9.0 (July 25, 2019)
4
+
5
+ ### New Features
6
+
7
+ * Forbid nil response ([#93](https://github.com/ryz310/my_api_client/pull/93))
8
+
9
+ ### Misc
10
+
11
+ * RSpec/DescribedClass-20190723233015 ([#92](https://github.com/ryz310/my_api_client/pull/92))
12
+
3
13
  ## 0.8.0 (July 23, 2019)
4
14
 
5
15
  ### New Features
6
16
 
7
- * Allow method calling on error handling (#89)
17
+ * Allow method calling on error handling ([#89](https://github.com/ryz310/my_api_client/pull/89))
8
18
 
9
19
  ### Breaking Changes
10
20
 
11
- * Require sawyer gem v0.8.2 over (#88)
21
+ * Require sawyer gem v0.8.2 over ([#88](https://github.com/ryz310/my_api_client/pull/88))
12
22
 
13
23
  ## 0.7.0 (July 17, 2019)
14
24
 
15
25
  ### Features
16
26
 
17
- * Add request duration to metadata (#80)
18
- * Support boolean on error handling (#81)
27
+ * Add request duration to metadata ([#80](https://github.com/ryz310/my_api_client/pull/80))
28
+ * Support boolean on error handling ([#81](https://github.com/ryz310/my_api_client/pull/81))
19
29
 
20
30
  ### Breaking Changes
21
31
 
22
- * Modify the generator to be simple (#82)
32
+ * Modify the generator to be simple ([#82](https://github.com/ryz310/my_api_client/pull/82))
23
33
 
24
34
  ### Misc
25
35
 
26
- * Re-generate .rubocop_todo.yml with RuboCop v0.73.0 (#79)
27
- * Introduce gem comet (#85)
36
+ * Re-generate .rubocop_todo.yml with RuboCop v0.73.0 ([#79](https://github.com/ryz310/my_api_client/pull/79))
37
+ * Introduce gem comet ([#85](https://github.com/ryz310/my_api_client/pull/85))
28
38
 
29
39
  ## 0.6.2 (July 03, 2019)
30
40
 
31
41
  ### Bug fixes
32
42
 
33
- * Fix logger setter on the template (#76)
43
+ * Fix logger setter on the template ([#76](https://github.com/ryz310/my_api_client/pull/76))
34
44
  * Fix #54
35
45
 
36
46
  ## 0.6.1 (July 03, 2019)
37
47
 
38
48
  ### Misc
39
49
 
40
- * Bump yard from `0.9.19` to `0.9.20` (#72)
50
+ * Bump yard from `0.9.19` to `0.9.20` ([#72](https://github.com/ryz310/my_api_client/pull/72))
41
51
  * Fix a security risk
42
52
 
43
53
  ## 0.6.0 (June 25, 2019)
44
54
 
45
55
  ### New Features
46
56
 
47
- * New stubbing helper (#65)
57
+ * New stubbing helper ([#65](https://github.com/ryz310/my_api_client/pull/65))
48
58
 
49
59
  ```rb
50
60
  stub_api_client_all(
@@ -74,85 +84,85 @@ response.id # => 1
74
84
 
75
85
  ### Bug Fixes
76
86
 
77
- * Initialize sawyer agent before logger initialization (#60)
78
- * Fixes: The URL included in the logger is incomplete (#53)
79
- * Fix parsing error if given text/html response (#61)
87
+ * Initialize sawyer agent before logger initialization ([#60](https://github.com/ryz310/my_api_client/pull/60))
88
+ * Fixes: The URL included in the logger is incomplete ([#53](https://github.com/ryz310/my_api_client/pull/53))
89
+ * Fix parsing error if given text/html response ([#61](https://github.com/ryz310/my_api_client/pull/61))
80
90
 
81
91
  ## 0.5.2 (June 23, 2019)
82
92
 
83
93
  ### Bug Fixes
84
94
 
85
- * Fix the result of the retry (#57)
86
- * Issue: Return values are nil after retrying (#56)
95
+ * Fix the result of the retry ([#57](https://github.com/ryz310/my_api_client/pull/57))
96
+ * Issue: Return values are nil after retrying ([#56](https://github.com/ryz310/my_api_client/pull/56))
87
97
 
88
98
  ### Misc
89
99
 
90
- * Improvement test coverage (#55)
100
+ * Improvement test coverage ([#55](https://github.com/ryz310/my_api_client/pull/55))
91
101
 
92
102
  ## 0.5.1 (June 19, 2019)
93
103
 
94
104
  ### Bug Fixes
95
105
 
96
- * Fix unsupported data for the Bugsnag breadcrumbs (#50)
106
+ * Fix unsupported data for the Bugsnag breadcrumbs ([#50](https://github.com/ryz310/my_api_client/pull/50))
97
107
 
98
108
  ## 0.5.0 (June 16, 2019)
99
109
 
100
110
  ### New Features
101
111
 
102
- * Support bugsnag breadcrumb (#41)
112
+ * Support bugsnag breadcrumb ([#41](https://github.com/ryz310/my_api_client/pull/41))
103
113
 
104
114
  ### Misc
105
115
 
106
- * Use CircleCI Orbs (#43)
116
+ * Use CircleCI Orbs ([#43](https://github.com/ryz310/my_api_client/pull/43))
107
117
 
108
118
  ## 0.4.0 (June 03, 2019)
109
119
 
110
120
  ### Feature
111
121
 
112
- * Improvement for endpoint (#35)
122
+ * Improvement for endpoint ([#35](https://github.com/ryz310/my_api_client/pull/35))
113
123
 
114
124
  ### Bug Fix
115
125
 
116
- * Add requirements for `$ bin/console` (#31)
126
+ * Add requirements for `$ bin/console` ([#31](https://github.com/ryz310/my_api_client/pull/31))
117
127
 
118
128
  ### Misc
119
129
 
120
- * Update RuboCop v0.70.0 -> v0.71.0 (#34)
130
+ * Update RuboCop v0.70.0 -> v0.71.0 ([#34](https://github.com/ryz310/my_api_client/pull/34))
121
131
 
122
132
  ## 0.3.0 (May 29, 2019)
123
133
 
124
134
  ### New Features
125
135
 
126
- * Provide test helper for RSpec (#28)
136
+ * Provide test helper for RSpec ([#28](https://github.com/ryz310/my_api_client/pull/28))
127
137
 
128
138
  ## 0.2.0 (May 29, 2019)
129
139
 
130
140
  ### New Features
131
141
 
132
- * Support Bugsnag metadata (#22)
142
+ * Support Bugsnag metadata ([#22](https://github.com/ryz310/my_api_client/pull/22))
133
143
 
134
144
  ### Misc
135
145
 
136
- * Improve test coverage (#24)
137
- * Fix problem on the release job (#25)
146
+ * Improve test coverage ([#24](https://github.com/ryz310/my_api_client/pull/24))
147
+ * Fix problem on the release job ([#25](https://github.com/ryz310/my_api_client/pull/25))
138
148
 
139
149
  ## 0.1.4 (May 28, 2019)
140
150
 
141
151
  ### Bugfix
142
152
 
143
- * Support activesupport before v5.2.0 (#17)
153
+ * Support activesupport before v5.2.0 ([#17](https://github.com/ryz310/my_api_client/pull/17))
144
154
 
145
155
  ## 0.1.3 (May 27, 2019)
146
156
 
147
- * Fix wrong variable name (#13)
157
+ * Fix wrong variable name ([#13](https://github.com/ryz310/my_api_client/pull/13))
148
158
 
149
159
  ## 0.1.2 (May 27, 2019)
150
160
 
151
- * Fix wrong method name (#10)
161
+ * Fix wrong method name ([#10](https://github.com/ryz310/my_api_client/pull/10))
152
162
 
153
163
  ## 0.1.1 (May 27, 2019)
154
164
 
155
- * Fix typo (#6)
165
+ * Fix typo ([#6](https://github.com/ryz310/my_api_client/pull/6))
156
166
 
157
167
  ## 0.1.0 (May 27, 2019)
158
168
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- my_api_client (0.8.0)
4
+ my_api_client (0.9.0)
5
5
  activesupport (>= 4.2.0)
6
6
  jsonpath
7
7
  sawyer (>= 0.8.2)
@@ -76,7 +76,7 @@ GEM
76
76
  unicode-display_width (>= 1.4.0, < 1.7)
77
77
  rubocop-performance (1.4.0)
78
78
  rubocop (>= 0.71.0)
79
- rubocop-rspec (1.33.0)
79
+ rubocop-rspec (1.34.0)
80
80
  rubocop (>= 0.60.0)
81
81
  ruby-progressbar (1.10.1)
82
82
  safe_yaml (1.0.5)
data/README.jp.md CHANGED
@@ -187,6 +187,14 @@ error_handling json: { '$.errors.code': :negative? }
187
187
  }
188
188
  ```
189
189
 
190
+ #### forbid_nil
191
+
192
+ ```ruby
193
+ error_handling status_code: 200, forbid_nil: true
194
+ ```
195
+
196
+ 一部のサービスではサーバーから何らかの Response Body が返ってくる事を期待しているにも関わらず、空の結果が結果が返ってくるというケースがあるようです。こちらも実験的な機能ですが、そういったケースを検出するために `forbid_nil` オプションを用意しました。通常の場合、Response Body が空の場合はエラー判定をしませんが、このオプションに `true` を指定するとエラーとして検知する様になります。正常応答が空となる API も存在するので、誤検知にご注意下さい。
197
+
190
198
  #### MyApiClient::Params::Params
191
199
 
192
200
  WIP
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ module ErrorHandling
5
+ # Generates an error handler proc (or symbol)
6
+ class Generator
7
+ private_class_method :new
8
+
9
+ # @param options [Hash]
10
+ # Options for this generator
11
+ # @option response [Sawyer::Response]
12
+ # The target of verifying
13
+ # @option status_code [String, Range, Integer, Regexp]
14
+ # Verifies response HTTP status code and raises error if matched
15
+ # @option json [Hash]
16
+ # Verifies response body as JSON and raises error if matched
17
+ # @option forbid_nil [Boolean]
18
+ # Verifies response_body and raises error if it is `nil`
19
+ # @option with [Symbol]
20
+ # Calls specified method when error detected
21
+ # @option raise [MyApiClient::Error]
22
+ # Raises specified error when error detected. default: MyApiClient::Error
23
+ # @option block [Proc]
24
+ # Executes the block when error detected
25
+ # @return [Proc]
26
+ # Returns value as `Proc` if given `raise` or `block` option
27
+ # @return [Symbol]
28
+ # Returns value as `Symbol` if given `with` option
29
+ def self.call(**options)
30
+ new(options).send(:call)
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :_response, :_status_code, :_json, :_forbid_nil, :_with, :_raise, :_block
36
+
37
+ def initialize(**options)
38
+ options.each { |k, v| instance_variable_set("@_#{k}", v) }
39
+ end
40
+
41
+ def call
42
+ return unless match?(_status_code, _response.status)
43
+ return unless match_all?(_json, _response.body)
44
+
45
+ if _block
46
+ ->(params, logger) { _block.call(params, logger) }
47
+ elsif _with
48
+ _with
49
+ else
50
+ ->(params, _logger) { raise _raise, params }
51
+ end
52
+ end
53
+
54
+ # rubocop:disable Metrics/CyclomaticComplexity
55
+ def match?(operator, target)
56
+ case operator
57
+ when nil
58
+ true
59
+ when String, Integer, TrueClass, FalseClass
60
+ operator == target
61
+ when Range
62
+ operator.include?(target)
63
+ when Regexp
64
+ operator =~ target.to_s
65
+ when Symbol
66
+ target.respond_to?(operator) && target.public_send(operator)
67
+ else
68
+ raise "Unexpected operator type was given: #{operator.inspect}"
69
+ end
70
+ end
71
+ # rubocop:enable Metrics/CyclomaticComplexity
72
+
73
+ def match_all?(json, response_body)
74
+ return true if json.nil?
75
+ return true if response_body.nil? && _forbid_nil
76
+ return false if response_body.blank?
77
+
78
+ json.all? do |path, operator|
79
+ target = JsonPath.new(path.to_s).first(response_body)
80
+ match?(operator, target)
81
+ end
82
+ rescue MultiJson::ParseError
83
+ false
84
+ end
85
+ end
86
+ end
87
+ end
@@ -7,6 +7,7 @@ module MyApiClient
7
7
  # You need to define `class_attribute: error_handler, default: []` for the
8
8
  # included class.
9
9
  # @example
10
+ # error_handling status_code: 200, forbid_nil: true
10
11
  # error_handling status_code: 400..499, raise: MyApiClient::ClientError
11
12
  # error_handling status_code: 500..599 do |params, logger|
12
13
  # logger.warn 'Server error occurred.'
@@ -16,65 +17,36 @@ module MyApiClient
16
17
  # error_handling json: { '$.errors.code': 10..19 }, with: :my_error_handling
17
18
  # error_handling json: { '$.errors.code': 20 }, raise: MyApiClient::ApiLimitError
18
19
  # error_handling json: { '$.errors.message': /Sorry/ }, raise: MyApiClient::ServerError
20
+ # error_handling json: { '$.errors.code': :negative? }
19
21
  module ErrorHandling
20
22
  extend ActiveSupport::Concern
21
23
 
22
24
  class_methods do
23
- # Description of .error_handling
25
+ # Definition of an error handling
24
26
  #
25
- # @param status_code [String, Range, Integer, Regexp] default: nil
26
- # @param json [Hash] default: nil
27
- # @param with [Symbol] default: nil
28
- # @param raise [MyApiClient::Error] default: MyApiClient::Error
29
- # @param block [Proc] describe_block_here
30
- def error_handling(status_code: nil, json: nil, with: nil, raise: MyApiClient::Error)
27
+ # @param options [Hash]
28
+ # Options for this generator
29
+ # @option status_code [String, Range, Integer, Regexp]
30
+ # Verifies response HTTP status code and raises error if matched
31
+ # @option json [Hash]
32
+ # Verifies response body as JSON and raises error if matched
33
+ # @option forbid_nil [Boolean]
34
+ # Verifies response_body and raises error if it is `nil`. default: false.
35
+ # @option with [Symbol]
36
+ # Calls specified method when error detected
37
+ # @option raise [MyApiClient::Error]
38
+ # Raises specified error when error detected. default: MyApiClient::Error
39
+ # @yield [MyApiClient::Params::Params, MyApiClient::Logger]
40
+ # Executes the block when error detected
41
+ def error_handling(**options, &block)
42
+ options[:forbid_nil] ||= false
43
+ options[:raise] ||= MyApiClient::Error
44
+ options[:block] = block if block_given?
45
+
31
46
  temp = error_handlers.dup
32
- temp << lambda { |response|
33
- if match?(status_code, response.status) && match_all?(json, response.body)
34
- if block_given?
35
- ->(params, logger) { yield params, logger }
36
- elsif with
37
- with
38
- else
39
- ->(params, _logger) { raise raise, params }
40
- end
41
- end
42
- }
47
+ temp << ->(response) { Generator.call(options.merge(response: response)) }
43
48
  self.error_handlers = temp
44
49
  end
45
-
46
- private
47
-
48
- # rubocop:disable Metrics/CyclomaticComplexity
49
- def match?(operator, target)
50
- case operator
51
- when nil
52
- true
53
- when String, Integer, TrueClass, FalseClass
54
- operator == target
55
- when Range
56
- operator.include?(target)
57
- when Regexp
58
- operator =~ target.to_s
59
- when Symbol
60
- target.respond_to?(operator) && target.public_send(operator)
61
- else
62
- raise "Unexpected operator type was given: #{operator.inspect}"
63
- end
64
- end
65
- # rubocop:enable Metrics/CyclomaticComplexity
66
-
67
- def match_all?(json, response_body)
68
- return true if json.nil?
69
- return false if response_body.blank?
70
-
71
- json.all? do |path, operator|
72
- target = JsonPath.new(path.to_s).first(response_body)
73
- match?(operator, target)
74
- end
75
- rescue MultiJson::ParseError
76
- false
77
- end
78
50
  end
79
51
 
80
52
  # The error handlers defined later takes precedence
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MyApiClient
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/my_api_client.rb CHANGED
@@ -9,6 +9,7 @@ require 'active_support/core_ext'
9
9
  require 'sawyer'
10
10
  require 'my_api_client/version'
11
11
  require 'my_api_client/config'
12
+ require 'my_api_client/error_handling/generator'
12
13
  require 'my_api_client/error_handling'
13
14
  require 'my_api_client/exceptions'
14
15
  require 'my_api_client/logger'
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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryz310
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-23 00:00:00.000000000 Z
11
+ date: 2019-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -246,6 +246,7 @@ files:
246
246
  - lib/my_api_client/base.rb
247
247
  - lib/my_api_client/config.rb
248
248
  - lib/my_api_client/error_handling.rb
249
+ - lib/my_api_client/error_handling/generator.rb
249
250
  - lib/my_api_client/errors.rb
250
251
  - lib/my_api_client/exceptions.rb
251
252
  - lib/my_api_client/integrations/bugsnag.rb