my_api_client 0.18.0 → 0.22.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.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +50 -120
  3. data/.gem_comet.yml +13 -2
  4. data/.github/dependabot.yml +32 -0
  5. data/.rubocop.yml +1 -1
  6. data/.rubocop_todo.yml +1 -15
  7. data/CHANGELOG.md +73 -86
  8. data/Gemfile +2 -0
  9. data/Gemfile.lock +75 -57
  10. data/README.jp.md +79 -15
  11. data/README.md +392 -8
  12. data/gemfiles/rails_5.0.gemfile +1 -0
  13. data/gemfiles/rails_5.1.gemfile +1 -0
  14. data/gemfiles/rails_5.2.gemfile +1 -0
  15. data/gemfiles/rails_6.0.gemfile +1 -0
  16. data/gemfiles/rails_6.1.gemfile +15 -0
  17. data/gemfiles/rails_7.0.gemfile +15 -0
  18. data/lib/generators/rails/templates/api_client.rb.erb +3 -5
  19. data/lib/generators/rspec/templates/api_client_spec.rb.erb +0 -8
  20. data/lib/my_api_client/base.rb +2 -9
  21. data/lib/my_api_client/integrations/bugsnag.rb +1 -1
  22. data/lib/my_api_client/request.rb +1 -1
  23. data/lib/my_api_client/rspec/matchers/be_handled_as_an_error.rb +3 -3
  24. data/lib/my_api_client/rspec/matchers/request_to.rb +1 -1
  25. data/lib/my_api_client/rspec/stub.rb +40 -15
  26. data/lib/my_api_client/version.rb +1 -1
  27. data/my_api/.ruby-version +1 -1
  28. data/my_api/Gemfile +3 -0
  29. data/my_api/Gemfile.lock +108 -99
  30. data/my_api/app/controllers/pagination_controller.rb +2 -2
  31. data/my_api/public/index.html +2 -2
  32. data/my_api/spec/spec_helper.rb +1 -1
  33. data/my_api_client.gemspec +6 -3
  34. data/rails_app/rails_5.2/Gemfile.lock +73 -64
  35. data/rails_app/rails_5.2/config/environments/production.rb +1 -1
  36. data/rails_app/rails_6.0/Gemfile.lock +95 -79
  37. data/rails_app/rails_6.0/config/environments/production.rb +1 -1
  38. data/rails_app/rails_6.1/.gitattributes +8 -0
  39. data/rails_app/rails_6.1/.gitignore +28 -0
  40. data/rails_app/rails_6.1/.rspec +3 -0
  41. data/rails_app/rails_6.1/Gemfile +17 -0
  42. data/rails_app/rails_6.1/Gemfile.lock +203 -0
  43. data/rails_app/rails_6.1/README.md +24 -0
  44. data/rails_app/rails_6.1/Rakefile +8 -0
  45. data/rails_app/rails_6.1/app/controllers/application_controller.rb +4 -0
  46. data/rails_app/rails_6.1/app/controllers/concerns/.keep +0 -0
  47. data/rails_app/rails_6.1/app/javascript/.keep +0 -0
  48. data/rails_app/rails_6.1/app/models/application_record.rb +5 -0
  49. data/rails_app/rails_6.1/app/models/concerns/.keep +0 -0
  50. data/rails_app/rails_6.1/bin/bundle +122 -0
  51. data/rails_app/rails_6.1/bin/rails +6 -0
  52. data/rails_app/rails_6.1/bin/rake +6 -0
  53. data/rails_app/rails_6.1/bin/setup +35 -0
  54. data/rails_app/rails_6.1/config/application.rb +42 -0
  55. data/rails_app/rails_6.1/config/boot.rb +5 -0
  56. data/rails_app/rails_6.1/config/credentials.yml.enc +1 -0
  57. data/rails_app/rails_6.1/config/database.yml +25 -0
  58. data/rails_app/rails_6.1/config/environment.rb +7 -0
  59. data/rails_app/rails_6.1/config/environments/development.rb +59 -0
  60. data/rails_app/rails_6.1/config/environments/production.rb +97 -0
  61. data/rails_app/rails_6.1/config/environments/test.rb +51 -0
  62. data/rails_app/rails_6.1/config/initializers/application_controller_renderer.rb +9 -0
  63. data/rails_app/rails_6.1/config/initializers/backtrace_silencers.rb +10 -0
  64. data/rails_app/rails_6.1/config/initializers/cors.rb +17 -0
  65. data/rails_app/rails_6.1/config/initializers/filter_parameter_logging.rb +8 -0
  66. data/rails_app/rails_6.1/config/initializers/inflections.rb +17 -0
  67. data/rails_app/rails_6.1/config/initializers/mime_types.rb +5 -0
  68. data/rails_app/rails_6.1/config/initializers/wrap_parameters.rb +16 -0
  69. data/rails_app/rails_6.1/config/locales/en.yml +33 -0
  70. data/rails_app/rails_6.1/config/routes.rb +5 -0
  71. data/rails_app/rails_6.1/config.ru +8 -0
  72. data/rails_app/rails_6.1/db/seeds.rb +8 -0
  73. data/rails_app/rails_6.1/lib/tasks/.keep +0 -0
  74. data/rails_app/rails_6.1/public/robots.txt +1 -0
  75. data/rails_app/rails_6.1/spec/rails_helper.rb +14 -0
  76. data/rails_app/rails_6.1/spec/spec_helper.rb +13 -0
  77. data/rails_app/rails_6.1/tmp/.keep +0 -0
  78. data/rails_app/rails_6.1/tmp/pids/.keep +0 -0
  79. data/rails_app/rails_6.1/vendor/.keep +0 -0
  80. data/rails_app/rails_7.0/Gemfile +13 -0
  81. data/rails_app/rails_7.0/Gemfile.lock +147 -0
  82. data/rails_app/rails_7.0/README.md +24 -0
  83. data/rails_app/rails_7.0/Rakefile +8 -0
  84. data/rails_app/rails_7.0/app/controllers/application_controller.rb +4 -0
  85. data/rails_app/rails_7.0/app/models/application_record.rb +5 -0
  86. data/rails_app/rails_7.0/bin/bundle +122 -0
  87. data/rails_app/rails_7.0/bin/rails +6 -0
  88. data/rails_app/rails_7.0/bin/rake +6 -0
  89. data/rails_app/rails_7.0/bin/setup +35 -0
  90. data/rails_app/rails_7.0/config/application.rb +41 -0
  91. data/rails_app/rails_7.0/config/boot.rb +5 -0
  92. data/rails_app/rails_7.0/config/credentials.yml.enc +1 -0
  93. data/rails_app/rails_7.0/config/database.yml +25 -0
  94. data/rails_app/rails_7.0/config/environment.rb +7 -0
  95. data/rails_app/rails_7.0/config/environments/development.rb +58 -0
  96. data/rails_app/rails_7.0/config/environments/production.rb +70 -0
  97. data/rails_app/rails_7.0/config/environments/test.rb +52 -0
  98. data/rails_app/rails_7.0/config/initializers/cors.rb +17 -0
  99. data/rails_app/rails_7.0/config/initializers/filter_parameter_logging.rb +8 -0
  100. data/rails_app/rails_7.0/config/initializers/inflections.rb +17 -0
  101. data/rails_app/rails_7.0/config/locales/en.yml +33 -0
  102. data/rails_app/rails_7.0/config/routes.rb +8 -0
  103. data/rails_app/rails_7.0/config.ru +8 -0
  104. data/rails_app/rails_7.0/db/seeds.rb +8 -0
  105. data/rails_app/rails_7.0/public/robots.txt +1 -0
  106. data/rails_app/rails_7.0/spec/rails_helper.rb +14 -0
  107. data/rails_app/rails_7.0/spec/spec_helper.rb +13 -0
  108. metadata +83 -11
  109. data/.dependabot/config.yml +0 -34
  110. data/.rubocop_challenge.yml +0 -5
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- my_api_client (0.18.0)
5
- activesupport (>= 5.0.0)
4
+ my_api_client (0.22.0)
5
+ activesupport (>= 5.2.0)
6
6
  faraday (>= 0.17.1)
7
7
  jsonpath
8
8
  sawyer (>= 0.8.2)
@@ -10,38 +10,54 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activesupport (6.0.3.4)
13
+ activesupport (6.1.4.4)
14
14
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
- i18n (>= 0.7, < 2)
16
- minitest (~> 5.1)
17
- tzinfo (~> 1.1)
18
- zeitwerk (~> 2.2, >= 2.2.2)
19
- addressable (2.7.0)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
19
+ addressable (2.8.0)
20
20
  public_suffix (>= 2.0.2, < 5.0)
21
- ast (2.4.1)
22
- bugsnag (6.18.0)
21
+ ast (2.4.2)
22
+ bugsnag (6.24.1)
23
23
  concurrent-ruby (~> 1.0)
24
24
  byebug (11.1.3)
25
25
  coderay (1.1.3)
26
- concurrent-ruby (1.1.7)
27
- crack (0.4.4)
28
- diff-lcs (1.4.4)
29
- docile (1.3.2)
30
- faraday (1.1.0)
26
+ concurrent-ruby (1.1.9)
27
+ crack (0.4.5)
28
+ rexml
29
+ diff-lcs (1.5.0)
30
+ docile (1.4.0)
31
+ faraday (1.8.0)
32
+ faraday-em_http (~> 1.0)
33
+ faraday-em_synchrony (~> 1.0)
34
+ faraday-excon (~> 1.1)
35
+ faraday-httpclient (~> 1.0.1)
36
+ faraday-net_http (~> 1.0)
37
+ faraday-net_http_persistent (~> 1.1)
38
+ faraday-patron (~> 1.0)
39
+ faraday-rack (~> 1.0)
31
40
  multipart-post (>= 1.2, < 3)
32
- ruby2_keywords
41
+ ruby2_keywords (>= 0.0.4)
42
+ faraday-em_http (1.0.0)
43
+ faraday-em_synchrony (1.0.0)
44
+ faraday-excon (1.1.0)
45
+ faraday-httpclient (1.0.1)
46
+ faraday-net_http (1.0.1)
47
+ faraday-net_http_persistent (1.2.0)
48
+ faraday-patron (1.0.0)
49
+ faraday-rack (1.0.0)
33
50
  hashdiff (1.0.1)
34
- i18n (1.8.5)
51
+ i18n (1.8.11)
35
52
  concurrent-ruby (~> 1.0)
36
- json (2.3.1)
37
- jsonpath (1.0.6)
53
+ jsonpath (1.1.0)
38
54
  multi_json
39
55
  method_source (1.0.0)
40
- minitest (5.14.2)
56
+ minitest (5.15.0)
41
57
  multi_json (1.15.0)
42
58
  multipart-post (2.1.1)
43
- parallel (1.20.1)
44
- parser (2.7.2.0)
59
+ parallel (1.21.0)
60
+ parser (3.0.3.2)
45
61
  ast (~> 2.4.1)
46
62
  pry (0.13.1)
47
63
  coderay (~> 1.1)
@@ -51,66 +67,68 @@ GEM
51
67
  pry (~> 0.13.0)
52
68
  public_suffix (4.0.6)
53
69
  rainbow (3.0.0)
54
- rake (13.0.1)
55
- regexp_parser (2.0.0)
56
- rexml (3.2.4)
70
+ rake (13.0.6)
71
+ regexp_parser (2.2.0)
72
+ rexml (3.2.5)
57
73
  rspec (3.10.0)
58
74
  rspec-core (~> 3.10.0)
59
75
  rspec-expectations (~> 3.10.0)
60
76
  rspec-mocks (~> 3.10.0)
61
- rspec-core (3.10.0)
77
+ rspec-core (3.10.1)
62
78
  rspec-support (~> 3.10.0)
63
- rspec-expectations (3.10.0)
79
+ rspec-expectations (3.10.1)
64
80
  diff-lcs (>= 1.2.0, < 2.0)
65
81
  rspec-support (~> 3.10.0)
66
- rspec-mocks (3.10.0)
82
+ rspec-mocks (3.10.2)
67
83
  diff-lcs (>= 1.2.0, < 2.0)
68
84
  rspec-support (~> 3.10.0)
69
- rspec-support (3.10.0)
85
+ rspec-support (3.10.3)
70
86
  rspec_junit_formatter (0.4.1)
71
87
  rspec-core (>= 2, < 4, != 2.12.0)
72
- rubocop (1.5.1)
88
+ rubocop (1.24.0)
73
89
  parallel (~> 1.10)
74
- parser (>= 2.7.1.5)
90
+ parser (>= 3.0.0.0)
75
91
  rainbow (>= 2.2.2, < 4.0)
76
- regexp_parser (>= 2.0)
92
+ regexp_parser (>= 1.8, < 3.0)
77
93
  rexml
78
- rubocop-ast (>= 1.2.0)
94
+ rubocop-ast (>= 1.15.0, < 2.0)
79
95
  ruby-progressbar (~> 1.7)
80
- unicode-display_width (>= 1.4.0, < 2.0)
81
- rubocop-ast (1.3.0)
82
- parser (>= 2.7.1.5)
83
- rubocop-performance (1.9.1)
84
- rubocop (>= 0.90.0, < 2.0)
96
+ unicode-display_width (>= 1.4.0, < 3.0)
97
+ rubocop-ast (1.15.0)
98
+ parser (>= 3.0.1.1)
99
+ rubocop-performance (1.13.0)
100
+ rubocop (>= 1.7.0, < 2.0)
85
101
  rubocop-ast (>= 0.4.0)
86
- rubocop-rspec (2.0.1)
87
- rubocop (~> 1.0)
88
- rubocop-ast (>= 1.1.0)
89
- ruby-progressbar (1.10.1)
90
- ruby2_keywords (0.0.2)
102
+ rubocop-rspec (2.6.0)
103
+ rubocop (~> 1.19)
104
+ ruby-progressbar (1.11.0)
105
+ ruby2_keywords (0.0.5)
91
106
  sawyer (0.8.2)
92
107
  addressable (>= 2.3.5)
93
108
  faraday (> 0.8, < 2.0)
94
- simplecov (0.17.1)
109
+ simplecov (0.21.2)
95
110
  docile (~> 1.1)
96
- json (>= 1.8, < 3)
97
- simplecov-html (~> 0.10.0)
98
- simplecov-html (0.10.2)
99
- thread_safe (0.3.6)
100
- tzinfo (1.2.8)
101
- thread_safe (~> 0.1)
102
- unicode-display_width (1.7.0)
103
- webmock (3.10.0)
104
- addressable (>= 2.3.6)
111
+ simplecov-html (~> 0.11)
112
+ simplecov_json_formatter (~> 0.1)
113
+ simplecov-html (0.12.3)
114
+ simplecov_json_formatter (0.1.3)
115
+ tzinfo (2.0.4)
116
+ concurrent-ruby (~> 1.0)
117
+ unicode-display_width (2.1.0)
118
+ webmock (3.14.0)
119
+ addressable (>= 2.8.0)
105
120
  crack (>= 0.3.2)
106
121
  hashdiff (>= 0.4.0, < 2.0.0)
107
- yard (0.9.25)
108
- zeitwerk (2.4.2)
122
+ webrick (1.7.0)
123
+ yard (0.9.27)
124
+ webrick (~> 1.7.0)
125
+ zeitwerk (2.5.1)
109
126
 
110
127
  PLATFORMS
111
128
  ruby
112
129
 
113
130
  DEPENDENCIES
131
+ activesupport (~> 6.1)
114
132
  bugsnag (>= 6.11.0)
115
133
  bundler (>= 2.0)
116
134
  my_api_client!
@@ -121,9 +139,9 @@ DEPENDENCIES
121
139
  rubocop
122
140
  rubocop-performance
123
141
  rubocop-rspec
124
- simplecov (= 0.17.1)
142
+ simplecov (= 0.21.2)
125
143
  webmock
126
144
  yard
127
145
 
128
146
  BUNDLED WITH
129
- 2.1.4
147
+ 2.2.22
data/README.jp.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # MyApiClient
4
4
 
5
- MyApiClient は API リクエストクラスを作成するための汎用的な機能を提供します。Sawyer や Faraday をベースにエラーハンドリングの機能を強化した構造になっています。
5
+ MyApiClient は API リクエストクラスを作成するための汎用的な機能を提供します。[Sawyer](https://github.com/lostisland/sawyer)[Faraday](https://github.com/lostisland/faraday) をベースにエラーハンドリングの機能を強化した構造になっています。
6
6
 
7
7
  ただし、 Sawyer はダミーデータの作成が難しかったり、他の gem で競合することがよくあるので、将来的には依存しないように変更していくかもしれません。
8
8
 
@@ -12,12 +12,12 @@ MyApiClient は API リクエストクラスを作成するための汎用的な
12
12
 
13
13
  ## Supported Versions
14
14
 
15
- * Ruby 2.5, 2.6, 2.7
16
- * Rails 5.0, 5.1, 5.2, 6.0
15
+ * Ruby 2.6, 2.7, 3.0
16
+ * Rails 5.2, 6.0, 6.1, 7.0
17
17
 
18
18
  ## Installation
19
19
 
20
- この gem は macOS と Linux で作動します。まずは `my_api_client` を Gemfile に追加します:
20
+ `my_api_client` を Gemfile に追加して下さい:
21
21
 
22
22
  ```ruby
23
23
  gem 'my_api_client'
@@ -130,13 +130,13 @@ api_clinet.pagination.each do |response|
130
130
  # Do something.
131
131
  end
132
132
 
133
- p = api_clinet.pagination
134
- p.next # => 1st page result
135
- p.next # => 2nd page result
136
- p.next # => 3rd page result
133
+ result = api_clinet.pagination
134
+ result.next # => 1st page result
135
+ result.next # => 2nd page result
136
+ result.next # => 3rd page result
137
137
  ```
138
138
 
139
- なお、`#each` はレスポンスに含まれる `paging` の値が nil になるまで繰り返されるのでご注意ください。例えば `#take` と組み合わせることでページネーションの上限を設定できます。
139
+ なお、`#each` はレスポンスに含まれる `paging` の値が `nil` になるまで繰り返されるのでご注意ください。例えば `#take` と組み合わせることでページネーションの上限を設定できます。
140
140
 
141
141
  `#pageable_get` の alias として `#pget` も利用可能です。
142
142
 
@@ -206,12 +206,12 @@ end
206
206
  API request `GET https://example.com/path/to/resouce`: "Server error occurred."
207
207
  ```
208
208
 
209
+ `json` には `Hash` の Key に [JSONPath](https://goessner.net/articles/JsonPath/) を指定して、レスポンス JSON から任意の値を取得し、 Value とマッチするかどうかでエラーハンドリングできます。Value には `String` `Integer` `Range` `Regexp` が指定可能です。上記の場合であれば、以下のような JSON にマッチします。
210
+
209
211
  ```ruby
210
212
  error_handling json: { '$.errors.code': 10..19 }, with: :my_error_handling
211
213
  ```
212
214
 
213
- `json` には `Hash` の Key に [JSONPath](https://goessner.net/articles/JsonPath/) を指定して、レスポンス JSON から任意の値を取得し、 Value とマッチするかどうかでエラーハンドリングできます。Value には `String` `Integer` `Range` `Regexp` が指定可能です。上記の場合であれば、以下のような JSON にマッチします。
214
-
215
215
  ```json
216
216
  {
217
217
  "erros": {
@@ -551,6 +551,8 @@ end
551
551
 
552
552
  ### Stubbing
553
553
 
554
+ #### `response` option
555
+
554
556
  以下のような `ApiClient` を定義しているとします。
555
557
 
556
558
  ```ruby
@@ -587,6 +589,7 @@ response = ExampleApiClient.new.request(user_id: 1)
587
589
  response.id # => 12345
588
590
  ```
589
591
 
592
+ #### Proc
590
593
 
591
594
  リクスエストパラメータを使ったレスポンスを返すようにスタブ化したい場合は、 `Proc` を利用することで実現できます。
592
595
 
@@ -600,7 +603,9 @@ response = ExampleApiClient.new.request(user_id: 1)
600
603
  response.id # => 1
601
604
  ```
602
605
 
603
- `receive` `have_received` を使ったテストを書きたい場合は、 `stub_api_client_all` `stub_api_client` の戻り値を利用すると良いでしょう。
606
+ #### Return value of `#stub_api_client_all` and `#stub_api_client`
607
+
608
+ `#stub_api_client_all` や `#stub_api_client` の戻り値はスタブ化した API Client のスタブオブジェクトです。`receive` や `have_received` を使ったテストを書きたい場合は、これらの値を利用すると良いでしょう。
604
609
 
605
610
  ```ruby
606
611
  def execute_api_request
@@ -612,7 +617,9 @@ execute_api_request
612
617
  expect(api_client).to have_received(:request).with(user_id: 1)
613
618
  ```
614
619
 
615
- また、例外が発生する場合のテストを書きたい場合は、 `raise` オプションを利用することができます。
620
+ #### `raise` option
621
+
622
+ 例外が発生する場合のテストを書きたい場合は、 `raise` オプションを利用することができます。
616
623
 
617
624
  ```ruby
618
625
  def execute_api_request
@@ -623,14 +630,15 @@ stub_api_client_all(ExampleApiClient, request: { raise: MyApiClient::Error })
623
630
  expect { execute_api_request }.to raise_error(MyApiClient::Error)
624
631
  ```
625
632
 
626
- なお、発生した例外に含まれるレスポンスパラメータもスタブ化したい場合は、 `response` オプションと同時に指定することが可能です。
633
+ なお、発生した例外に含まれるレスポンスパラメータやステータスコードもスタブ化したい場合は、 `response` オプションと同時に指定することが可能です。
627
634
 
628
635
  ```ruby
629
636
  stub_api_client_all(
630
637
  ExampleApiClient,
631
638
  request: {
632
639
  raise: MyApiClient::Error,
633
- response: { message: 'error' }
640
+ response: { message: 'error' },
641
+ status_code: 429
634
642
  }
635
643
  )
636
644
 
@@ -639,9 +647,65 @@ begin
639
647
  rescue MyApiClient::Error => e
640
648
  response_body = e.params.response.data.to_h
641
649
  expect(response_body).to eq(message: 'error')
650
+ status_code = e.params.response.status
651
+ expect(status_code).to eq(429)
652
+ end
653
+ ```
654
+
655
+ #### `pageable` option
656
+
657
+ `#pageable_get` (`#pget`) を使った実装用に `pageable` というオプションが利用できます。
658
+ `pageable` に設定する値は `Enumerable` である必要があります。
659
+
660
+ ```ruby
661
+ stub_api_client_all(
662
+ MyPaginationApiClient,
663
+ pagination: {
664
+ pageable: [
665
+ { page: 1 },
666
+ { page: 2 },
667
+ { page: 3 },
668
+ ],
669
+ }
670
+ )
671
+
672
+ MyPaginationApiClient.new.pagination.each do |response|
673
+ response.page #=> 1, 2, 3
642
674
  end
643
675
  ```
644
676
 
677
+ なお、 `Enumerable` の各値にはここまで紹介した `response`, `raise`, `Proc` など全てのオプションが利用可能です。
678
+
679
+ ```ruby
680
+ stub_api_client_all(
681
+ MyPaginationApiClient,
682
+ pagination: {
683
+ pageable: [
684
+ { response: { page: 1 } },
685
+ { page: 2 },
686
+ ->(params) { { page: 3, user_id: params[:user_id] } },
687
+ { raise: MyApiClient::ClientError::IamTeapot },
688
+ ],
689
+ }
690
+ )
691
+ ```
692
+
693
+ また、 `Enumerator` を使えば無限に続くページネーションを定義することもできます。
694
+
695
+
696
+ ```ruby
697
+ stub_api_client_all(
698
+ MyPaginationApiClient,
699
+ pagination: {
700
+ pageable: Enumerator.new do |y|
701
+ loop.with_index(1) do |_, i|
702
+ y << { page: i }
703
+ end
704
+ end,
705
+ }
706
+ )
707
+ ```
708
+
645
709
  ## Deployment
646
710
 
647
711
  この gem のリリースには [gem_comet](https://github.com/ryz310/gem_comet) を利用しています。