my_api_client 0.14.0 → 0.15.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: f0b010bd5bb4ef7d0b3a99c826fefc67bedf5e9f51bcfd7779dc618c2b9a4f58
4
- data.tar.gz: 4806eb56f7556728fc848c7e54408a127393a921a29e248a9d32ba950b4f3ef3
3
+ metadata.gz: 3b40beeb8a70a00452e59208a5c7e2d5dee2c8155d2e23b5cd950e1a7e6f3eb1
4
+ data.tar.gz: 15c927bb0954c12225a73955b23b7ccc71b784a2cb63832f7914a757f2ac2ed2
5
5
  SHA512:
6
- metadata.gz: 6bc2efb59de265f84c727ce67e9717fff724251136ffe7159a0a61151d335bb476386649e23ef5bb8a9982e387bebd53c58064817517fdac855bd9ac3fc4d6e8
7
- data.tar.gz: f0db303e86287d71099d0c0e29024663013cf4c44dffac1ac1d9c2a18113b34a4d5a1e9a6d3a4824833d1bb413a875deb0a45bb2f2becb25d4119d1028a1d10c
6
+ metadata.gz: c57cfcf7db9a8b9d45ee0c603402c910d2ea59bc0f3b406ba2c91388e9b0bab0997fe948dea5944fbc06499ea259c83579a154158808cce7433ea8d0beb02236
7
+ data.tar.gz: acc4846d877c8aace073858a102c72cc13245d6b13c94542b90d5da43798d04a6317533797b3e92a7e7710f532936ae551a0fa9cb1dcc8151bf58c68f5f06e24
@@ -2,9 +2,30 @@ version: 1
2
2
  update_configs:
3
3
  - package_manager: "ruby:bundler"
4
4
  directory: "/"
5
- update_schedule: "daily"
5
+ update_schedule: "weekly"
6
6
  default_reviewers:
7
7
  - "ryz310"
8
+ default_assignees:
9
+ - "ryz310"
10
+ default_labels:
11
+ - "dependabot"
12
+ ignored_updates:
13
+ - match:
14
+ dependency_name: "rubocop*"
15
+ automerged_updates:
16
+ - match:
17
+ dependency_type: "development"
18
+ update_type: "all"
19
+ - match:
20
+ dependency_type: "production"
21
+ update_type: "semver:patch"
22
+ - package_manager: "ruby:bundler"
23
+ directory: "/my_api"
24
+ update_schedule: "weekly"
25
+ default_reviewers:
26
+ - "ryz310"
27
+ default_assignees:
28
+ - "ryz310"
8
29
  default_labels:
9
30
  - "dependabot"
10
31
  automerged_updates:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,53 @@
1
1
  # Change log
2
2
 
3
+ ## v0.15.0 (Mar 21, 2020)
4
+
5
+ ### Feature
6
+
7
+ * [#220](https://github.com/ryz310/my_api_client/pull/220) Pageable HTTP request ([@ryz310](https://github.com/ryz310))
8
+ * Add `#pageable_get` method (alias: `#pget`)
9
+ * For example:
10
+ * API client definition
11
+ ```ruby
12
+ class MyPaginationApiClient < ApplicationApiClient
13
+ endpoint 'https://example.com/v1'
14
+
15
+ # GET pagination?page=1
16
+ def pagination
17
+ pageable_get 'pagination', paging: '$.links.next', headers: headers, query: { page: 1 }
18
+ end
19
+
20
+ private
21
+
22
+ def headers
23
+ { 'Content-Type': 'application/json;charset=UTF-8' }
24
+ end
25
+ end
26
+ ```
27
+ * The pagination API response
28
+ ```json
29
+ {
30
+ "links": {
31
+ "next": "https://example.com/pagination?page=3",
32
+ "previous": "https://example.com/pagination?page=1",
33
+ },
34
+ "page": 2
35
+ }
36
+ ```
37
+ * Usage
38
+ ```ruby
39
+ api_clinet = MyPaginationApiClient.new
40
+ api_clinet.pagination.each do |response|
41
+ # Do something.
42
+ end
43
+
44
+ p = api_clinet.pagination
45
+ p.next # => 1st page result
46
+ p.next # => 2nd page result
47
+ p.next # => 3rd page result
48
+ ```
49
+ * [#223](https://github.com/ryz310/my_api_client/pull/223) Use Enumerator::Lazy instead of Enumerator ([@ryz310](https://github.com/ryz310))
50
+
3
51
  ## v0.14.0 (Mar 14, 2020)
4
52
 
5
53
  ### Feature
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- my_api_client (0.14.0)
4
+ my_api_client (0.15.0)
5
5
  activesupport (>= 4.2.0)
6
6
  faraday (>= 0.17.1)
7
7
  jsonpath
@@ -10,7 +10,7 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activesupport (5.2.4.1)
13
+ activesupport (5.2.4.2)
14
14
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
15
  i18n (>= 0.7, < 2)
16
16
  minitest (~> 5.1)
data/README.jp.md CHANGED
@@ -83,6 +83,68 @@ api_clinet.get_users #=> #<Sawyer::Response>
83
83
 
84
84
  続いて、 `#get_users` や `#post_user` を定義します。メソッド名には API のタイトルを付けると良いと思います。メソッド内部で `#get` や `#post` を呼び出していますが、これがリクエスト時の HTTP Method になります。他にも `#patch` `#put` `#delete` が利用可能です。
85
85
 
86
+ ### Pagination
87
+
88
+ API の中にはレスポンスに結果の続きを取得するための URL を含んでいるものがあります。
89
+
90
+ MyApiClient では、このような API を enumerable に扱うための `#pageable_get` というメソッドを用意しています。以下に例を示します。
91
+
92
+ ```ruby
93
+ class MyPaginationApiClient < ApplicationApiClient
94
+ endpoint 'https://example.com/v1'
95
+
96
+ # GET pagination?page=1
97
+ def pagination
98
+ pageable_get 'pagination', paging: '$.links.next', headers: headers, query: { page: 1 }
99
+ end
100
+
101
+ private
102
+
103
+ def headers
104
+ { 'Content-Type': 'application/json;charset=UTF-8' }
105
+ end
106
+ end
107
+ ```
108
+
109
+ 上記の例の場合、最初に `GET https://example.com/v1/pagination?page=1` に対してリクエストが実行され、続けてレスポンス JSON の `$.link.next` に含まれる URL に対して enumerable にリクエストを実行します。
110
+
111
+ 例えば以下のようなレスポンスであれば、`$.link.next` は `"https://example.com/pagination?page=3"` を示します。
112
+
113
+ ```json
114
+ {
115
+ "links": {
116
+ "next": "https://example.com/pagination?page=3",
117
+ "previous": "https://example.com/pagination?page=1",
118
+ },
119
+ "page": 2
120
+ }
121
+ ```
122
+
123
+ そして `#pageable_get` は [Enumerator::Lazy](https://docs.ruby-lang.org/ja/latest/class/Enumerator=3a=3aLazy.html) を返すので、 `#each` や `#next` を実行することで次の結果を取得できます。
124
+
125
+ ```ruby
126
+ api_clinet = MyPaginationApiClient.new
127
+ api_clinet.pagination.each do |response|
128
+ # Do something.
129
+ end
130
+
131
+ p = api_clinet.pagination
132
+ p.next # => 1st page result
133
+ p.next # => 2nd page result
134
+ p.next # => 3rd page result
135
+ ```
136
+
137
+ なお、`#each` はレスポンスに含まれる `paging` の値が nil になるまで繰り返されるのでご注意ください。例えば `#take` と組み合わせることでページネーションの上限を設定できます。
138
+
139
+ `#pageable_get` の alias として `#pget` も利用可能です。
140
+
141
+ ```ruby
142
+ # GET pagination?page=1
143
+ def pagination
144
+ pget 'pagination', paging: '$.links.next', headers: headers, query: { page: 1 }
145
+ end
146
+ ```
147
+
86
148
  ### Error handling
87
149
 
88
150
  上記のコードにエラーハンドリングを追加してみます。
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'application_api_client'
4
+
5
+ # An usage example of the `my_api_client`.
6
+ # See also: my_api/app/controllers/pagination_controller.rb
7
+ class MyPaginationApiClient < ApplicationApiClient
8
+ # GET pagination?page=1
9
+ def pagination
10
+ pget 'pagination', paging: '$.links.next', headers: headers, query: { page: 1 }
11
+ end
12
+
13
+ private
14
+
15
+ def headers
16
+ { 'Content-Type': 'application/json;charset=UTF-8' }
17
+ end
18
+ end
@@ -29,8 +29,9 @@ module MyApiClient
29
29
  # @option status_code [String, Range, Integer, Regexp]
30
30
  # Verifies response HTTP status code and raises error if matched
31
31
  # @option json [Hash, Symbol]
32
- # Verifies response body as JSON and raises error if matched.
33
- # If specified `:forbid_nil`, it forbid `nil` on response_body.
32
+ # Specify the validation target value path included in the response body
33
+ # as JsonPath expression.
34
+ # If specified `:forbid_nil`, it forbid `nil` at the response body.
34
35
  # @option with [Symbol]
35
36
  # Calls specified method when error detected
36
37
  # @option raise [MyApiClient::Error]
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ module Request
5
+ # Provides basic HTTP request method.
6
+ module Basic
7
+ HTTP_METHODS = %i[get post patch delete].freeze
8
+
9
+ HTTP_METHODS.each do |http_method|
10
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
11
+ # Executes HTTP request with #{http_method.upcase} method
12
+ #
13
+ # @param pathname [String]
14
+ # Pathname of the request target URL.
15
+ # It's joined with the defined by `endpoint`.
16
+ # @param headers [Hash, nil]
17
+ # Request headers.
18
+ # @param query [Hash, nil]
19
+ # Query string.
20
+ # @param body [Hash, nil]
21
+ # Request body. You should not specify it when use GET method.
22
+ # @return [Sawyer::Resource]
23
+ # Response body instance.
24
+ def #{http_method}(pathname, headers: nil, query: nil, body: nil)
25
+ response = call(:_request_with_relative_uri, :#{http_method}, pathname, headers, query, body)
26
+ response.data
27
+ end
28
+ METHOD
29
+ end
30
+
31
+ alias put patch
32
+ end
33
+ end
34
+ 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
@@ -1,38 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MyApiClient
4
- # Description of Request
4
+ # Provides HTTP request method.
5
5
  module Request
6
- HTTP_METHODS = %i[get post patch delete].freeze
7
-
8
- HTTP_METHODS.each do |http_method|
9
- class_eval <<~METHOD, __FILE__, __LINE__ + 1
10
- # Executes HTTP request with #{http_method.upcase} method
11
- #
12
- # @param pathname [String]
13
- # Pathname of the request target URL.
14
- # It's joined with the defined `endpoint`.
15
- # @param headers [Hash, nil]
16
- # Request headers.
17
- # @param query [Hash, nil]
18
- # Query string.
19
- # @param body [Hash, nil]
20
- # Request body. You should not specify it when use GET method.
21
- # @return [Sawyer::Resouce]
22
- # Response body instance.
23
- def #{http_method}(pathname, headers: nil, query: nil, body: nil)
24
- query_strings = query.present? ? '?' + query&.to_query : ''
25
- uri = URI.join(File.join(endpoint, pathname), query_strings)
26
- response = call(:_request, :#{http_method}, uri, headers, body)
27
- response.data
28
- end
29
- METHOD
30
- end
31
- alias put patch
6
+ include Basic
7
+ include Pagination
32
8
 
33
9
  private
34
10
 
35
- # Executes HTTP request.
11
+ # Executes HTTP request with relative URI.
12
+ #
13
+ # @param http_method [Symbol]
14
+ # HTTP method. e.g. `:get`, `:post`, `:patch` and `:delete`.
15
+ # @param pathname [String]
16
+ # Pathname of the request target URL.
17
+ # It's joined with the defined by `endpoint`.
18
+ # @param headers [Hash, nil]
19
+ # Request headers.
20
+ # @param query [Hash, nil]
21
+ # Query string.
22
+ # @param body [Hash, nil]
23
+ # Request body.
24
+ # @return [Sawyer::Response]
25
+ # Response instance.
26
+ def _request_with_relative_uri(http_method, pathname, headers, query, body)
27
+ query_strings = query.present? ? '?' + query&.to_query : ''
28
+ uri = URI.join(File.join(endpoint, pathname), query_strings)
29
+ _request_with_absolute_uri(http_method, uri, headers, body)
30
+ end
31
+
32
+ # Executes HTTP request with absolute URI.
36
33
  #
37
34
  # @param http_method [Symbol]
38
35
  # HTTP method. e.g. `:get`, `:post`, `:patch` and `:delete`.
@@ -44,13 +41,11 @@ module MyApiClient
44
41
  # Request body.
45
42
  # @return [Sawyer::Response]
46
43
  # Response instance.
47
- def _request(http_method, uri, headers, body)
48
- request_params = Params::Request.new(http_method, uri, headers, body)
49
- request_logger = Logger.new(logger, http_method, uri)
44
+ def _request_with_absolute_uri(http_method, uri, headers, body)
50
45
  Executor.call(
51
46
  instance: self,
52
- request_params: request_params,
53
- request_logger: request_logger,
47
+ request_params: Params::Request.new(http_method, uri, headers, body),
48
+ request_logger: Logger.new(logger, http_method, uri),
54
49
  faraday_options: faraday_options
55
50
  )
56
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MyApiClient
4
- VERSION = '0.14.0'
4
+ VERSION = '0.15.0'
5
5
  end
data/lib/my_api_client.rb CHANGED
@@ -16,6 +16,8 @@ require 'my_api_client/error_handling'
16
16
  require 'my_api_client/exceptions'
17
17
  require 'my_api_client/request/logger'
18
18
  require 'my_api_client/request/executor'
19
+ require 'my_api_client/request/basic'
20
+ require 'my_api_client/request/pagination'
19
21
  require 'my_api_client/errors'
20
22
  require 'my_api_client/params/params'
21
23
  require 'my_api_client/params/request'
data/my_api/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.0
1
+ 2.5.7
data/my_api/Gemfile.lock CHANGED
@@ -1,34 +1,34 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- actionmailer (6.0.2.1)
5
- actionpack (= 6.0.2.1)
6
- actionview (= 6.0.2.1)
7
- activejob (= 6.0.2.1)
4
+ actionmailer (6.0.2.2)
5
+ actionpack (= 6.0.2.2)
6
+ actionview (= 6.0.2.2)
7
+ activejob (= 6.0.2.2)
8
8
  mail (~> 2.5, >= 2.5.4)
9
9
  rails-dom-testing (~> 2.0)
10
- actionpack (6.0.2.1)
11
- actionview (= 6.0.2.1)
12
- activesupport (= 6.0.2.1)
10
+ actionpack (6.0.2.2)
11
+ actionview (= 6.0.2.2)
12
+ activesupport (= 6.0.2.2)
13
13
  rack (~> 2.0, >= 2.0.8)
14
14
  rack-test (>= 0.6.3)
15
15
  rails-dom-testing (~> 2.0)
16
16
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
- actionview (6.0.2.1)
18
- activesupport (= 6.0.2.1)
17
+ actionview (6.0.2.2)
18
+ activesupport (= 6.0.2.2)
19
19
  builder (~> 3.1)
20
20
  erubi (~> 1.4)
21
21
  rails-dom-testing (~> 2.0)
22
22
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
- activejob (6.0.2.1)
24
- activesupport (= 6.0.2.1)
23
+ activejob (6.0.2.2)
24
+ activesupport (= 6.0.2.2)
25
25
  globalid (>= 0.3.6)
26
- activemodel (6.0.2.1)
27
- activesupport (= 6.0.2.1)
28
- activerecord (6.0.2.1)
29
- activemodel (= 6.0.2.1)
30
- activesupport (= 6.0.2.1)
31
- activesupport (6.0.2.1)
26
+ activemodel (6.0.2.2)
27
+ activesupport (= 6.0.2.2)
28
+ activerecord (6.0.2.2)
29
+ activemodel (= 6.0.2.2)
30
+ activesupport (= 6.0.2.2)
31
+ activesupport (6.0.2.2)
32
32
  concurrent-ruby (~> 1.0, >= 1.0.2)
33
33
  i18n (>= 0.7, < 2)
34
34
  minitest (~> 5.1)
@@ -45,44 +45,44 @@ GEM
45
45
  rainbow
46
46
  thor
47
47
  zeitwerk
48
- aws-partitions (1.278.0)
49
- aws-sdk-apigateway (1.36.0)
48
+ aws-partitions (1.285.0)
49
+ aws-sdk-apigateway (1.37.0)
50
50
  aws-sdk-core (~> 3, >= 3.71.0)
51
51
  aws-sigv4 (~> 1.1)
52
- aws-sdk-cloudformation (1.30.0)
52
+ aws-sdk-cloudformation (1.31.0)
53
53
  aws-sdk-core (~> 3, >= 3.71.0)
54
54
  aws-sigv4 (~> 1.1)
55
- aws-sdk-cloudwatchlogs (1.28.0)
55
+ aws-sdk-cloudwatchlogs (1.29.0)
56
56
  aws-sdk-core (~> 3, >= 3.71.0)
57
57
  aws-sigv4 (~> 1.1)
58
- aws-sdk-core (3.90.1)
58
+ aws-sdk-core (3.91.1)
59
59
  aws-eventstream (~> 1.0, >= 1.0.2)
60
60
  aws-partitions (~> 1, >= 1.239.0)
61
61
  aws-sigv4 (~> 1.1)
62
62
  jmespath (~> 1.0)
63
- aws-sdk-dynamodb (1.43.0)
63
+ aws-sdk-dynamodb (1.45.0)
64
64
  aws-sdk-core (~> 3, >= 3.71.0)
65
65
  aws-sigv4 (~> 1.1)
66
- aws-sdk-kinesis (1.20.0)
66
+ aws-sdk-kinesis (1.21.0)
67
67
  aws-sdk-core (~> 3, >= 3.71.0)
68
68
  aws-sigv4 (~> 1.1)
69
- aws-sdk-kms (1.29.0)
69
+ aws-sdk-kms (1.30.0)
70
70
  aws-sdk-core (~> 3, >= 3.71.0)
71
71
  aws-sigv4 (~> 1.1)
72
- aws-sdk-lambda (1.36.0)
72
+ aws-sdk-lambda (1.37.0)
73
73
  aws-sdk-core (~> 3, >= 3.71.0)
74
74
  aws-sigv4 (~> 1.1)
75
- aws-sdk-s3 (1.60.2)
75
+ aws-sdk-s3 (1.61.1)
76
76
  aws-sdk-core (~> 3, >= 3.83.0)
77
77
  aws-sdk-kms (~> 1)
78
78
  aws-sigv4 (~> 1.1)
79
- aws-sdk-sns (1.21.0)
79
+ aws-sdk-sns (1.22.0)
80
80
  aws-sdk-core (~> 3, >= 3.71.0)
81
81
  aws-sigv4 (~> 1.1)
82
- aws-sdk-sqs (1.23.1)
82
+ aws-sdk-sqs (1.24.0)
83
83
  aws-sdk-core (~> 3, >= 3.71.0)
84
84
  aws-sigv4 (~> 1.1)
85
- aws-sdk-ssm (1.71.0)
85
+ aws-sdk-ssm (1.73.0)
86
86
  aws-sdk-core (~> 3, >= 3.71.0)
87
87
  aws-sigv4 (~> 1.1)
88
88
  aws-sigv4 (1.1.1)
@@ -118,7 +118,7 @@ GEM
118
118
  hashie (4.1.0)
119
119
  i18n (1.8.2)
120
120
  concurrent-ruby (~> 1.0)
121
- jets (2.3.13)
121
+ jets (2.3.14)
122
122
  actionmailer (~> 6.0.0)
123
123
  actionpack (~> 6.0.0)
124
124
  actionview (~> 6.0.0)
@@ -154,7 +154,7 @@ GEM
154
154
  text-table
155
155
  thor
156
156
  zeitwerk
157
- jets-gems (0.2.0)
157
+ jets-gems (0.2.2)
158
158
  gems
159
159
  jets-html-sanitizer (1.0.4)
160
160
  loofah (~> 2.2, >= 2.2.2)
@@ -169,13 +169,13 @@ GEM
169
169
  mail (2.7.1)
170
170
  mini_mime (>= 0.1.1)
171
171
  memoist (0.16.2)
172
- method_source (0.9.2)
172
+ method_source (1.0.0)
173
173
  mimemagic (0.3.4)
174
174
  mini_mime (1.0.2)
175
175
  mini_portile2 (2.4.0)
176
176
  minitest (5.14.0)
177
177
  nio4r (2.5.2)
178
- nokogiri (1.10.8)
178
+ nokogiri (1.10.9)
179
179
  mini_portile2 (~> 2.4.0)
180
180
  public_suffix (4.0.3)
181
181
  puma (4.3.3)
@@ -188,15 +188,15 @@ GEM
188
188
  nokogiri (>= 1.6)
189
189
  rails-html-sanitizer (1.3.0)
190
190
  loofah (~> 2.3)
191
- railties (6.0.2.1)
192
- actionpack (= 6.0.2.1)
193
- activesupport (= 6.0.2.1)
191
+ railties (6.0.2.2)
192
+ actionpack (= 6.0.2.2)
193
+ activesupport (= 6.0.2.2)
194
194
  method_source
195
195
  rake (>= 0.8.7)
196
196
  thor (>= 0.20.3, < 2.0)
197
197
  rainbow (3.0.0)
198
198
  rake (13.0.1)
199
- recursive-open-struct (1.1.0)
199
+ recursive-open-struct (1.1.1)
200
200
  regexp_parser (1.7.0)
201
201
  rspec (3.9.0)
202
202
  rspec-core (~> 3.9.0)
@@ -204,7 +204,7 @@ GEM
204
204
  rspec-mocks (~> 3.9.0)
205
205
  rspec-core (3.9.1)
206
206
  rspec-support (~> 3.9.1)
207
- rspec-expectations (3.9.0)
207
+ rspec-expectations (3.9.1)
208
208
  diff-lcs (>= 1.2.0, < 2.0)
209
209
  rspec-support (~> 3.9.0)
210
210
  rspec-mocks (3.9.1)
@@ -222,7 +222,7 @@ GEM
222
222
  thread_safe (~> 0.1)
223
223
  xpath (3.2.0)
224
224
  nokogiri (~> 1.8)
225
- zeitwerk (2.2.2)
225
+ zeitwerk (2.3.0)
226
226
 
227
227
  PLATFORMS
228
228
  ruby
data/my_api/README.md CHANGED
@@ -28,6 +28,12 @@ This API returns arbitrary error code as JSON.
28
28
 
29
29
  * `GET error/:code`
30
30
 
31
+ ### My Pagination API
32
+
33
+ This API returns a response including pagination links.
34
+
35
+ * `GET pagination?page=1`
36
+
31
37
  ## Deployment
32
38
 
33
39
  You need to prepare following environment variables:
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Pagination API
4
+ class PaginationController < ApplicationController
5
+ # GET pagination
6
+ def index
7
+ case params[:page]
8
+ when '1', nil
9
+ render status: :ok, json: first_page
10
+ when '2'
11
+ render status: :ok, json: second_page
12
+ when '3'
13
+ render status: :ok, json: third_page
14
+ else
15
+ render status: :not_found, json: ''
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def first_page
22
+ {
23
+ links: {
24
+ next: page_link(2),
25
+ },
26
+ page: 1,
27
+ }
28
+ end
29
+
30
+ def second_page
31
+ {
32
+ links: {
33
+ next: page_link(3),
34
+ previous: page_link(1),
35
+ },
36
+ page: 2,
37
+ }
38
+ end
39
+
40
+ def third_page
41
+ {
42
+ links: {
43
+ previous: page_link(2),
44
+ },
45
+ page: 3,
46
+ }
47
+ end
48
+
49
+ # NOTE: `#pagination_url` returns `http://xxxxx.execute-api.ap-northeast-1.amazonaws.com/pagination`
50
+ # but it should be `https://xxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/pagination`.
51
+ # So this is workaround.
52
+ def page_link(page)
53
+ query_strings = '?' + { page: page }.to_query
54
+ uri = File.join(ENV['JETS_HOST'], ENV['JETS_STAGE'], pagination_path)
55
+ uri.sub!('http://', 'https://')
56
+ URI.join(uri, query_strings)
57
+ end
58
+ end
@@ -7,6 +7,7 @@ Jets.application.routes.draw do
7
7
 
8
8
  get 'status/:status', to: 'status#show'
9
9
  get 'error/:code', to: 'error#show'
10
+ get 'pagination', to: 'pagination#index'
10
11
 
11
12
  # The jets/public#show controller can serve static utf8 content out of the public folder.
12
13
  # Note, as part of the deploy process Jets uploads files in the public folder to s3
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe PaginationController, type: :controller do
4
+ describe '#index' do
5
+ let(:first_page) do
6
+ {
7
+ links: {
8
+ next: 'https://example.com/test/pagination?page=2',
9
+ },
10
+ page: 1,
11
+ }.to_json
12
+ end
13
+
14
+ let(:second_page) do
15
+ {
16
+ links: {
17
+ next: 'https://example.com/test/pagination?page=3',
18
+ previous: 'https://example.com/test/pagination?page=1',
19
+ },
20
+ page: 2,
21
+ }.to_json
22
+ end
23
+
24
+ let(:third_page) do
25
+ {
26
+ links: {
27
+ previous: 'https://example.com/test/pagination?page=2',
28
+ },
29
+ page: 3,
30
+ }.to_json
31
+ end
32
+
33
+ context 'without page' do
34
+ it 'returns a 1st page contents including 2nd page link' do
35
+ get '/pagination'
36
+ expect(response.status).to eq 200
37
+ expect(response.body).to eq first_page
38
+ end
39
+ end
40
+
41
+ context 'with page = 1' do
42
+ it 'returns a 1st page contents including 2nd page link' do
43
+ get '/pagination', page: 1
44
+ expect(response.status).to eq 200
45
+ expect(response.body).to eq first_page
46
+ end
47
+ end
48
+
49
+ context 'with page = 2' do
50
+ it 'returns a 2nd page contents including 3rd page link' do
51
+ get '/pagination', page: 2
52
+ expect(response.status).to eq 200
53
+ expect(response.body).to eq second_page
54
+ end
55
+ end
56
+
57
+ context 'with page = 3' do
58
+ it 'returns a 3rd page contents not including next page link' do
59
+ get '/pagination', page: 3
60
+ expect(response.status).to eq 200
61
+ expect(response.body).to eq third_page
62
+ end
63
+ end
64
+
65
+ context 'with page = 4' do
66
+ it 'returns 404 NOT FOUND' do
67
+ get '/pagination', page: 4
68
+ expect(response.status).to eq 404
69
+ expect(response.body).to be_blank
70
+ end
71
+ end
72
+ end
73
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  ENV['JETS_TEST'] = '1'
4
4
  ENV['JETS_ENV'] ||= 'test'
5
+ ENV['JETS_STAGE'] ||= 'test'
5
6
  # Ensures aws api never called. Fixture home folder does not contain ~/.aws/credentails
6
7
  ENV['HOME'] = 'spec/fixtures/home'
7
8
 
@@ -24,3 +25,7 @@ end
24
25
  RSpec.configure do |c|
25
26
  c.include Helpers
26
27
  end
28
+
29
+ Jets.application.configure do
30
+ config.helpers.host = 'https://example.com'
31
+ 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.14.0
4
+ version: 0.15.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-03-14 00:00:00.000000000 Z
11
+ date: 2020-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -248,6 +248,7 @@ files:
248
248
  - example/api_clients/application_api_client.rb
249
249
  - example/api_clients/my_error_api_client.rb
250
250
  - example/api_clients/my_errors.rb
251
+ - example/api_clients/my_pagination_api_client.rb
251
252
  - example/api_clients/my_rest_api_client.rb
252
253
  - example/api_clients/my_status_api_client.rb
253
254
  - gemfiles/rails_4.2.gemfile
@@ -275,8 +276,10 @@ files:
275
276
  - lib/my_api_client/params/params.rb
276
277
  - lib/my_api_client/params/request.rb
277
278
  - lib/my_api_client/request.rb
279
+ - lib/my_api_client/request/basic.rb
278
280
  - lib/my_api_client/request/executor.rb
279
281
  - lib/my_api_client/request/logger.rb
282
+ - lib/my_api_client/request/pagination.rb
280
283
  - lib/my_api_client/rspec.rb
281
284
  - lib/my_api_client/rspec/matcher_helper.rb
282
285
  - lib/my_api_client/rspec/matchers/be_handled_as_an_error.rb
@@ -297,6 +300,7 @@ files:
297
300
  - my_api/Rakefile
298
301
  - my_api/app/controllers/application_controller.rb
299
302
  - my_api/app/controllers/error_controller.rb
303
+ - my_api/app/controllers/pagination_controller.rb
300
304
  - my_api/app/controllers/rest_controller.rb
301
305
  - my_api/app/controllers/status_controller.rb
302
306
  - my_api/app/helpers/application_helper.rb
@@ -316,13 +320,13 @@ files:
316
320
  - my_api/public/favicon.ico
317
321
  - my_api/public/index.html
318
322
  - my_api/spec/controllers/error_controller_spec.rb
323
+ - my_api/spec/controllers/pagination_controller_spec.rb
319
324
  - my_api/spec/controllers/rest_controller_spec.rb
320
325
  - my_api/spec/controllers/status_controller_spec.rb
321
326
  - my_api/spec/fixtures/payloads/posts-index.json
322
327
  - my_api/spec/fixtures/payloads/posts-show.json
323
328
  - my_api/spec/spec_helper.rb
324
329
  - my_api_client.gemspec
325
- - renovate.json
326
330
  homepage: https://github.com/ryz310/my_api_client
327
331
  licenses:
328
332
  - MIT
data/renovate.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "extends": [
3
- "config:base"
4
- ],
5
- "packageRules": [
6
- {
7
- "updateTypes": ["major"],
8
- "labels": ["renovate", "major"]
9
- },
10
- {
11
- "updateTypes": ["minor"],
12
- "labels": ["renovate", "minor", "auto merge"],
13
- "automerge": true
14
- },
15
- {
16
- "updateTypes": ["patch"],
17
- "labels": ["renovate", "patch", "auto merge"],
18
- "automerge": true
19
- }
20
- ]
21
- }