my_api_client 0.13.0 → 0.16.1

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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +102 -36
  3. data/.dependabot/config.yml +34 -0
  4. data/.envrc.skeleton +1 -0
  5. data/.rubocop.yml +8 -10
  6. data/.rubocop_challenge.yml +3 -0
  7. data/.rubocop_todo.yml +5 -10
  8. data/CHANGELOG.md +229 -0
  9. data/Gemfile.lock +48 -43
  10. data/README.jp.md +98 -23
  11. data/bin/console +4 -0
  12. data/example/api_clients/application_api_client.rb +13 -0
  13. data/example/api_clients/my_error_api_client.rb +34 -0
  14. data/example/api_clients/my_errors.rb +27 -0
  15. data/example/api_clients/my_pagination_api_client.rb +18 -0
  16. data/example/api_clients/my_rest_api_client.rb +48 -0
  17. data/example/api_clients/my_status_api_client.rb +22 -0
  18. data/lib/generators/rails/templates/application_api_client.rb.erb +0 -11
  19. data/lib/my_api_client.rb +10 -2
  20. data/lib/my_api_client/base.rb +2 -18
  21. data/lib/my_api_client/config.rb +0 -29
  22. data/lib/my_api_client/default_error_handlers.rb +64 -0
  23. data/lib/my_api_client/error_handling.rb +13 -23
  24. data/lib/my_api_client/error_handling/generator.rb +30 -10
  25. data/lib/my_api_client/error_handling/{process_retry_option.rb → retry_option_processor.rb} +1 -1
  26. data/lib/my_api_client/errors.rb +0 -53
  27. data/lib/my_api_client/errors/api_limit_error.rb +6 -0
  28. data/lib/my_api_client/errors/client_error.rb +93 -0
  29. data/lib/my_api_client/errors/network_error.rb +43 -0
  30. data/lib/my_api_client/errors/server_error.rb +42 -0
  31. data/lib/my_api_client/params/request.rb +7 -10
  32. data/lib/my_api_client/request.rb +48 -70
  33. data/lib/my_api_client/request/basic.rb +32 -0
  34. data/lib/my_api_client/request/executor.rb +89 -0
  35. data/lib/my_api_client/request/logger.rb +37 -0
  36. data/lib/my_api_client/request/pagination.rb +39 -0
  37. data/lib/my_api_client/rspec/matcher_helper.rb +2 -2
  38. data/lib/my_api_client/rspec/matchers/be_handled_as_an_error.rb +2 -0
  39. data/lib/my_api_client/rspec/matchers/request_to.rb +3 -4
  40. data/lib/my_api_client/version.rb +1 -1
  41. data/my_api/.envrc.skeleton +3 -0
  42. data/my_api/.gitignore +14 -0
  43. data/my_api/.jetskeep +1 -0
  44. data/my_api/.rspec +3 -0
  45. data/my_api/.ruby-version +1 -0
  46. data/my_api/Gemfile +23 -0
  47. data/my_api/Gemfile.lock +243 -0
  48. data/my_api/Procfile +7 -0
  49. data/my_api/README.md +54 -0
  50. data/my_api/Rakefile +4 -0
  51. data/my_api/app/controllers/application_controller.rb +5 -0
  52. data/my_api/app/controllers/error_controller.rb +21 -0
  53. data/my_api/app/controllers/pagination_controller.rb +58 -0
  54. data/my_api/app/controllers/rest_controller.rb +60 -0
  55. data/my_api/app/controllers/status_controller.rb +11 -0
  56. data/my_api/app/helpers/application_helper.rb +5 -0
  57. data/my_api/app/jobs/application_job.rb +7 -0
  58. data/my_api/app/models/application_item.rb +5 -0
  59. data/my_api/config.ru +7 -0
  60. data/my_api/config/application.rb +73 -0
  61. data/my_api/config/dynamodb.yml +22 -0
  62. data/my_api/config/environments/development.rb +9 -0
  63. data/my_api/config/environments/production.rb +11 -0
  64. data/my_api/config/environments/test.rb +9 -0
  65. data/my_api/config/routes.rb +17 -0
  66. data/my_api/db/.gitkeep +0 -0
  67. data/my_api/public/404.html +67 -0
  68. data/my_api/public/422.html +67 -0
  69. data/my_api/public/500.html +66 -0
  70. data/my_api/public/favicon.ico +0 -0
  71. data/my_api/public/index.html +91 -0
  72. data/my_api/spec/controllers/error_controller_spec.rb +43 -0
  73. data/my_api/spec/controllers/pagination_controller_spec.rb +73 -0
  74. data/my_api/spec/controllers/rest_controller_spec.rb +99 -0
  75. data/my_api/spec/controllers/status_controller_spec.rb +47 -0
  76. data/my_api/spec/fixtures/payloads/posts-index.json +51 -0
  77. data/my_api/spec/fixtures/payloads/posts-show.json +53 -0
  78. data/my_api/spec/spec_helper.rb +31 -0
  79. data/my_api_client.gemspec +1 -1
  80. metadata +62 -9
  81. data/lib/my_api_client/logger.rb +0 -36
  82. data/renovate.json +0 -5
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ module Request
5
+ # Description of Logger
6
+ class Logger
7
+ attr_reader :logger, :method, :uri
8
+
9
+ LOG_LEVEL = %i[debug info warn error fatal].freeze
10
+
11
+ # Description of #initialize
12
+ #
13
+ # @param logger [::Logger] describe_logger_here
14
+ # @param method [String] HTTP method
15
+ # @param uri [URI] Target URI
16
+ def initialize(logger, method, uri)
17
+ @logger = logger
18
+ @method = method.to_s.upcase
19
+ @uri = uri
20
+ end
21
+
22
+ LOG_LEVEL.each do |level|
23
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
24
+ def #{level}(message)
25
+ logger.#{level}(format(message))
26
+ end
27
+ METHOD
28
+ end
29
+
30
+ private
31
+
32
+ def format(message)
33
+ "API request `#{method} #{uri}`: \"#{message}\""
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ module Request
5
+ # Provides enumerable HTTP request method.
6
+ module Pagination
7
+ # Executes HTTP request with GET method, for pagination API. Expects the
8
+ # pagination API to provide pagination links as part of the content of the response.
9
+ #
10
+ # @param pathname [String]
11
+ # Pathname of the request target URL. It's joined with the defined by `endpoint`.
12
+ # @param paging [String]
13
+ # Specify the pagination link path included in the response body as JsonPath expression
14
+ # @param headers [Hash, nil]
15
+ # Request headers.
16
+ # @param query [Hash, nil]
17
+ # Query string.
18
+ # @param body [Hash, nil]
19
+ # Request body. You should not specify it when use GET method.
20
+ # @return [Enumerator::Lazy]
21
+ # Yields the pagination API response.
22
+ def pageable_get(pathname, paging:, headers: nil, query: nil)
23
+ Enumerator.new do |y|
24
+ response = call(:_request_with_relative_uri, :get, pathname, headers, query, nil)
25
+ loop do
26
+ y << response.data
27
+
28
+ next_uri = JsonPath.new(paging).first(response.body)
29
+ break if next_uri.blank?
30
+
31
+ response = call(:_request_with_absolute_uri, :get, next_uri, headers, nil)
32
+ end
33
+ end.lazy
34
+ end
35
+
36
+ alias pget pageable_get
37
+ end
38
+ end
39
+ end
@@ -4,8 +4,8 @@ module MyApiClient
4
4
  # Helper module for rspec custom matcher
5
5
  module MatcherHelper
6
6
  def disable_logging
7
- logger = instance_double(MyApiClient::Logger, info: nil, warn: nil)
8
- allow(MyApiClient::Logger).to receive(:new).and_return(logger)
7
+ logger = instance_double(MyApiClient::Request::Logger, info: nil, warn: nil)
8
+ allow(MyApiClient::Request::Logger).to receive(:new).and_return(logger)
9
9
  end
10
10
 
11
11
  def dummy_response(status: 200, headers: {}, body: nil)
@@ -45,6 +45,7 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
45
45
 
46
46
  attr_reader :sawyer
47
47
 
48
+ # rubocop:disable Metrics/AbcSize
48
49
  def init
49
50
  disable_logging
50
51
  response = dummy_response(
@@ -56,6 +57,7 @@ RSpec::Matchers.define :be_handled_as_an_error do |expected_error_class|
56
57
  allow(Sawyer::Agent).to receive(:new).and_return(sawyer)
57
58
  allow(MyApiClient::Sleeper).to receive(:call)
58
59
  end
60
+ # rubocop:enable Metrics/AbcSize
59
61
 
60
62
  def set_validation_for_retry_count
61
63
  return if retry_count.nil?
@@ -8,10 +8,9 @@ RSpec::Matchers.define :request_to do |expected_method, expected_url|
8
8
  match do |api_request|
9
9
  disable_logging
10
10
  @expected = {
11
- request_line: request_line(expected_method, expected_url),
11
+ request_line: request_line(expected_method, expected_url, expected_options[:query]),
12
12
  body: expected_options[:body],
13
13
  headers: expected_options[:headers],
14
- query: expected_options[:query],
15
14
  }.compact
16
15
  @actual = {}
17
16
  sawyer = instance_double(Sawyer::Agent)
@@ -24,7 +23,6 @@ RSpec::Matchers.define :request_to do |expected_method, expected_url|
24
23
  request_line: request_line(method, @actual_schema_and_hostname + pathname),
25
24
  body: body,
26
25
  headers: options[:headers],
27
- query: options[:query],
28
26
  }.compact
29
27
  end.and_return(dummy_response)
30
28
  safe_execution(api_request)
@@ -51,7 +49,8 @@ RSpec::Matchers.define :request_to do |expected_method, expected_url|
51
49
  nil
52
50
  end
53
51
 
54
- def request_line(method, url)
52
+ def request_line(method, url, query = nil)
53
+ url += '?' + query.to_query if query.present?
55
54
  "#{method.upcase} #{url}"
56
55
  end
57
56
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MyApiClient
4
- VERSION = '0.13.0'
4
+ VERSION = '0.16.1'
5
5
  end
@@ -0,0 +1,3 @@
1
+ export AWS_REGION=ap-northeast-1
2
+ export AWS_ACCESS_KEY_ID={AWS Access Key ID}
3
+ export AWS_SECRET_ACCESS_KEY={AWS Secret Access Key}
@@ -0,0 +1,14 @@
1
+ *.gem
2
+ .bundle
3
+ .byebug_history
4
+ .DS_Store
5
+ .env
6
+ .env.*
7
+ .envrc
8
+ /node_modules
9
+ /public/packs
10
+ /public/packs-test
11
+ bundled
12
+ coverage
13
+ pkg
14
+ tmp
@@ -0,0 +1 @@
1
+ pack
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.5.7
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'jets'
6
+
7
+ gem 'dynomite'
8
+
9
+ # development and test groups are not bundled as part of the deployment
10
+ group :development, :test do
11
+ # Call 'byebug' anywhere in the code to stop execution and get a debugger console
12
+ gem 'byebug', platforms: %i[mri mingw x64_mingw]
13
+ gem 'puma'
14
+ gem 'rack'
15
+ gem 'shotgun'
16
+ end
17
+
18
+ group :test do
19
+ gem 'capybara'
20
+ gem 'launchy'
21
+ gem 'rspec'
22
+ gem 'rspec_junit_formatter'
23
+ end
@@ -0,0 +1,243 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (6.0.3.2)
5
+ actionpack (= 6.0.3.2)
6
+ actionview (= 6.0.3.2)
7
+ activejob (= 6.0.3.2)
8
+ mail (~> 2.5, >= 2.5.4)
9
+ rails-dom-testing (~> 2.0)
10
+ actionpack (6.0.3.2)
11
+ actionview (= 6.0.3.2)
12
+ activesupport (= 6.0.3.2)
13
+ rack (~> 2.0, >= 2.0.8)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.0.3.2)
18
+ activesupport (= 6.0.3.2)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activejob (6.0.3.2)
24
+ activesupport (= 6.0.3.2)
25
+ globalid (>= 0.3.6)
26
+ activemodel (6.0.3.2)
27
+ activesupport (= 6.0.3.2)
28
+ activerecord (6.0.3.2)
29
+ activemodel (= 6.0.3.2)
30
+ activesupport (= 6.0.3.2)
31
+ activesupport (6.0.3.2)
32
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
+ i18n (>= 0.7, < 2)
34
+ minitest (~> 5.1)
35
+ tzinfo (~> 1.1)
36
+ zeitwerk (~> 2.2, >= 2.2.2)
37
+ addressable (2.7.0)
38
+ public_suffix (>= 2.0.2, < 5.0)
39
+ aws-eventstream (1.1.0)
40
+ aws-mfa-secure (0.4.2)
41
+ activesupport
42
+ aws-sdk-core
43
+ aws_config
44
+ memoist
45
+ rainbow
46
+ thor
47
+ zeitwerk
48
+ aws-partitions (1.358.0)
49
+ aws-sdk-apigateway (1.49.0)
50
+ aws-sdk-core (~> 3, >= 3.99.0)
51
+ aws-sigv4 (~> 1.1)
52
+ aws-sdk-cloudformation (1.41.0)
53
+ aws-sdk-core (~> 3, >= 3.99.0)
54
+ aws-sigv4 (~> 1.1)
55
+ aws-sdk-cloudwatchlogs (1.34.0)
56
+ aws-sdk-core (~> 3, >= 3.99.0)
57
+ aws-sigv4 (~> 1.1)
58
+ aws-sdk-core (3.104.4)
59
+ aws-eventstream (~> 1, >= 1.0.2)
60
+ aws-partitions (~> 1, >= 1.239.0)
61
+ aws-sigv4 (~> 1.1)
62
+ jmespath (~> 1.0)
63
+ aws-sdk-dynamodb (1.51.0)
64
+ aws-sdk-core (~> 3, >= 3.99.0)
65
+ aws-sigv4 (~> 1.1)
66
+ aws-sdk-kinesis (1.27.0)
67
+ aws-sdk-core (~> 3, >= 3.99.0)
68
+ aws-sigv4 (~> 1.1)
69
+ aws-sdk-kms (1.36.0)
70
+ aws-sdk-core (~> 3, >= 3.99.0)
71
+ aws-sigv4 (~> 1.1)
72
+ aws-sdk-lambda (1.48.0)
73
+ aws-sdk-core (~> 3, >= 3.99.0)
74
+ aws-sigv4 (~> 1.1)
75
+ aws-sdk-s3 (1.78.0)
76
+ aws-sdk-core (~> 3, >= 3.104.3)
77
+ aws-sdk-kms (~> 1)
78
+ aws-sigv4 (~> 1.1)
79
+ aws-sdk-sns (1.29.0)
80
+ aws-sdk-core (~> 3, >= 3.99.0)
81
+ aws-sigv4 (~> 1.1)
82
+ aws-sdk-sqs (1.30.0)
83
+ aws-sdk-core (~> 3, >= 3.99.0)
84
+ aws-sigv4 (~> 1.1)
85
+ aws-sdk-ssm (1.86.0)
86
+ aws-sdk-core (~> 3, >= 3.99.0)
87
+ aws-sigv4 (~> 1.1)
88
+ aws-sigv4 (1.2.2)
89
+ aws-eventstream (~> 1, >= 1.0.2)
90
+ aws_config (0.1.0)
91
+ builder (3.2.4)
92
+ byebug (11.1.3)
93
+ capybara (3.33.0)
94
+ addressable
95
+ mini_mime (>= 0.1.3)
96
+ nokogiri (~> 1.8)
97
+ rack (>= 1.6.0)
98
+ rack-test (>= 0.6.3)
99
+ regexp_parser (~> 1.5)
100
+ xpath (~> 3.2)
101
+ cfn_camelizer (0.4.9)
102
+ activesupport
103
+ memoist
104
+ rainbow
105
+ cfnresponse (0.4.0)
106
+ concurrent-ruby (1.1.7)
107
+ crass (1.0.6)
108
+ diff-lcs (1.3)
109
+ dotenv (2.7.6)
110
+ dynomite (1.2.6)
111
+ activesupport
112
+ aws-sdk-dynamodb
113
+ rainbow
114
+ erubi (1.9.0)
115
+ gems (1.2.0)
116
+ globalid (0.4.2)
117
+ activesupport (>= 4.2.0)
118
+ hashie (4.1.0)
119
+ i18n (1.8.5)
120
+ concurrent-ruby (~> 1.0)
121
+ jets (2.3.17)
122
+ actionmailer (~> 6.0.0)
123
+ actionpack (~> 6.0.0)
124
+ actionview (~> 6.0.0)
125
+ activerecord (~> 6.0.0)
126
+ activesupport (~> 6.0.0)
127
+ aws-mfa-secure (~> 0.4.0)
128
+ aws-sdk-apigateway
129
+ aws-sdk-cloudformation
130
+ aws-sdk-cloudwatchlogs
131
+ aws-sdk-dynamodb
132
+ aws-sdk-kinesis
133
+ aws-sdk-lambda
134
+ aws-sdk-s3
135
+ aws-sdk-sns
136
+ aws-sdk-sqs
137
+ aws-sdk-ssm
138
+ cfn_camelizer (~> 0.4.6)
139
+ cfnresponse
140
+ dotenv
141
+ gems
142
+ hashie
143
+ jets-gems
144
+ jets-html-sanitizer
145
+ kramdown
146
+ memoist
147
+ mimemagic
148
+ rack
149
+ railties (~> 6.0.0)
150
+ rainbow
151
+ recursive-open-struct
152
+ shotgun
153
+ text-table
154
+ thor
155
+ zeitwerk
156
+ jets-gems (0.2.2)
157
+ gems
158
+ jets-html-sanitizer (1.0.4)
159
+ loofah (~> 2.2, >= 2.2.2)
160
+ jmespath (1.4.0)
161
+ kramdown (2.3.0)
162
+ rexml
163
+ launchy (2.5.0)
164
+ addressable (~> 2.7)
165
+ loofah (2.6.0)
166
+ crass (~> 1.0.2)
167
+ nokogiri (>= 1.5.9)
168
+ mail (2.7.1)
169
+ mini_mime (>= 0.1.1)
170
+ memoist (0.16.2)
171
+ method_source (1.0.0)
172
+ mimemagic (0.3.5)
173
+ mini_mime (1.0.2)
174
+ mini_portile2 (2.4.0)
175
+ minitest (5.14.1)
176
+ nio4r (2.5.2)
177
+ nokogiri (1.10.10)
178
+ mini_portile2 (~> 2.4.0)
179
+ public_suffix (4.0.5)
180
+ puma (4.3.5)
181
+ nio4r (~> 2.0)
182
+ rack (2.2.3)
183
+ rack-test (1.1.0)
184
+ rack (>= 1.0, < 3)
185
+ rails-dom-testing (2.0.3)
186
+ activesupport (>= 4.2.0)
187
+ nokogiri (>= 1.6)
188
+ rails-html-sanitizer (1.3.0)
189
+ loofah (~> 2.3)
190
+ railties (6.0.3.2)
191
+ actionpack (= 6.0.3.2)
192
+ activesupport (= 6.0.3.2)
193
+ method_source
194
+ rake (>= 0.8.7)
195
+ thor (>= 0.20.3, < 2.0)
196
+ rainbow (3.0.0)
197
+ rake (13.0.1)
198
+ recursive-open-struct (1.1.2)
199
+ regexp_parser (1.7.1)
200
+ rexml (3.2.4)
201
+ rspec (3.9.0)
202
+ rspec-core (~> 3.9.0)
203
+ rspec-expectations (~> 3.9.0)
204
+ rspec-mocks (~> 3.9.0)
205
+ rspec-core (3.9.1)
206
+ rspec-support (~> 3.9.1)
207
+ rspec-expectations (3.9.1)
208
+ diff-lcs (>= 1.2.0, < 2.0)
209
+ rspec-support (~> 3.9.0)
210
+ rspec-mocks (3.9.1)
211
+ diff-lcs (>= 1.2.0, < 2.0)
212
+ rspec-support (~> 3.9.0)
213
+ rspec-support (3.9.2)
214
+ rspec_junit_formatter (0.4.1)
215
+ rspec-core (>= 2, < 4, != 2.12.0)
216
+ shotgun (0.9.2)
217
+ rack (>= 1.0)
218
+ text-table (1.2.4)
219
+ thor (1.0.1)
220
+ thread_safe (0.3.6)
221
+ tzinfo (1.2.7)
222
+ thread_safe (~> 0.1)
223
+ xpath (3.2.0)
224
+ nokogiri (~> 1.8)
225
+ zeitwerk (2.4.0)
226
+
227
+ PLATFORMS
228
+ ruby
229
+
230
+ DEPENDENCIES
231
+ byebug
232
+ capybara
233
+ dynomite
234
+ jets
235
+ launchy
236
+ puma
237
+ rack
238
+ rspec
239
+ rspec_junit_formatter
240
+ shotgun
241
+
242
+ BUNDLED WITH
243
+ 2.1.4
@@ -0,0 +1,7 @@
1
+ local: dynamodb-local # port 8000
2
+ admin: env AWS_ACCESS_KEY_ID=$DYNAMODB_ADMIN_AWS_ACCESS_KEY_ID PORT=8001 dynamodb-admin # port 8001
3
+ # web: jets server # port 8888
4
+
5
+ # Using Procfile to just start local dynamodb services for now.
6
+ # To start jets server for now use:
7
+ # jets server