Class: PbAPI::PaginationHelper
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- PbAPI::PaginationHelper
- 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, account, end_date)
params_hash = form_query(start_date, account, 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
-
#data ⇒ Array
readonly
Array of transformed models.
-
#next_page_exists ⇒ Boolean
readonly
Indicates if the next page exists.
-
#next_page_id ⇒ Any?
readonly
The identifier for the next page.
Class Method Summary collapse
-
.from_response(response_body:, key:, type:) ⇒ PaginationHelper
Transforms the HTTP response and returns a new instance of PaginationHelper.
-
.paginate(params_hash:, key:, type:) {|params| ... } ⇒ Enumerator
Loads paginated data and yields individual items.
Instance Attribute Details
#data ⇒ Array (readonly)
Returns Array of transformed models.
21 |
# File 'lib/pb_api/pagination_helper.rb', line 21 attribute :data, Types::Array.of(Types::Any) |
#next_page_exists ⇒ Boolean (readonly)
Returns Indicates if the next page exists.
24 |
# File 'lib/pb_api/pagination_helper.rb', line 24 attribute :next_page_exists, Types::Bool |
#next_page_id ⇒ Any? (readonly)
Returns 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.
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.
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 |