Class: PbAPI::PaginationHelper

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/pb_api/pagination_helper.rb

Overview

PaginationHelper provides pagination functionality by yielding individual items from paginated HTTP responses. Example of usage:

class BalanceResource < PbAPI::Resource
def common(uri, key, start_date, , end_date)
  params_hash = form_query(start_date, , end_date, nil, 20)
  # Pass a block to handle the HTTP request using your resource's get_request method.
  PbAPI::PaginationHelper
    .load(uri: uri, params_hash: params_hash, key: "balances", type: PbAPI::Models::Balance) do |uri, params|
    get_request(uri, params: params)
  end
end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dataArray (readonly)

Returns Array of transformed models.

Returns:

  • (Array)

    Array of transformed models.



21
# File 'lib/pb_api/pagination_helper.rb', line 21

attribute :data, Types::Array.of(Types::Any)

#next_page_existsBoolean (readonly)

Returns Indicates if the next page exists.

Returns:

  • (Boolean)

    Indicates if the next page exists.



24
# File 'lib/pb_api/pagination_helper.rb', line 24

attribute :next_page_exists, Types::Bool

#next_page_idAny? (readonly)

Returns The identifier for the next page.

Returns:

  • (Any, nil)

    The identifier for the next page.



27
# File 'lib/pb_api/pagination_helper.rb', line 27

attribute :next_page_id, Types::Any.optional

Class Method Details

.from_response(response_body:, key:, type:) ⇒ PaginationHelper

Transforms the HTTP response and returns a new instance of PaginationHelper.

Parameters:

  • response_body (Hash)

    The parsed HTTP response body.

  • key (String, Symbol)

    The key that holds the array of items.

  • type (Class)

    A Dry::Struct model class used to encapsulate each item.

Returns:

  • (PaginationHelper)

    An instance containing the transformed data and pagination flags.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pb_api/pagination_helper.rb', line 60

def self.from_response(response_body:, key:, type:)
  body = response_body

  transformer = type.transformer
  transformed = transformer.call(body[key])
  # Create an array of Balance models from the transformed hashes
  new(
    data: transformed.map { |hash| type.new(hash) },
    next_page_exists: body["exist_next_page"],
    next_page_id: body["next_page_id"]
  )
end

.paginate(params_hash:, key:, type:) {|params| ... } ⇒ Enumerator

Loads paginated data and yields individual items.

See example of usage in the class description.

Parameters:

  • params_hash (Hash)

    Query parameters for the HTTP request.

  • key (String, Symbol)

    The key to extract data from the response body.

  • type (Class)

    A Dry::Struct model class used for transforming each item.

Yields:

  • (params)

    Must perform the HTTP request and return a response object.

Yield Returns:

  • (Array)

    The HTTP response with a 'body' method containing a hash.

Returns:

  • (Enumerator)

    An enumerator yielding individual items.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pb_api/pagination_helper.rb', line 39

def self.paginate(params_hash:, key:, type:)
  Enumerator.new do |yielder|
    last_page_id = 0
    loop do
      # The block is used to perform the HTTP request, decoupling the helper from the client.
      response = yield(params_hash)
      processed = from_response(response_body: response.body, key: key, type: type)
      processed.data.each { |item| yielder << item }
      break unless processed.next_page_exists && processed.next_page_id != last_page_id

      last_page_id = params_hash[:followId] = processed.next_page_id
    end
  end
end