Module: MyApiClient::Request::Pagination

Included in:
MyApiClient::Request
Defined in:
lib/my_api_client/request/pagination.rb

Overview

Provides enumerable HTTP request method.

Instance Method Summary collapse

Instance Method Details

#pageable_get(pathname, paging:, headers: nil, query: nil) ⇒ Enumerator::Lazy Also known as: pget

Executes HTTP request with GET method, for pagination API. Expects the pagination API to provide pagination links as part of the content of the response.

Parameters:

  • pathname (String)

    Pathname of the request target URL. It’s joined with the defined by ‘endpoint`.

  • paging (String, Proc)

    Specify the pagination link path included in the response body as JsonPath expression

  • headers (Hash, nil) (defaults to: nil)

    Request headers.

  • query (Hash, nil) (defaults to: nil)

    Query string.

  • body (Hash, nil)

    Request body. You should not specify it when use GET method.

Returns:

  • (Enumerator::Lazy)

    Yields the pagination API response.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/my_api_client/request/pagination.rb', line 22

def pageable_get(pathname, paging:, headers: nil, query: nil)
  Enumerator.new do |y|
    response = call(:_request_with_relative_uri, :get, pathname, headers, query, nil)
    loop do
      y << response.data

      next_uri = get_next_url(paging, response)
      break if next_uri.blank?

      response = call(:_request_with_absolute_uri, :get, next_uri, headers, nil)
    end
  end.lazy
end