Class: UkrsibAPI::PaginationHelper
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- UkrsibAPI::PaginationHelper
- Defined in:
- lib/ukrsib_api/pagination_helper.rb
Overview
PaginationHelper provides pagination functionality by yielding individual items from paginated HTTP responses. Example of usage:
class BalanceResource < UkrsibAPI::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.
UkrsibAPI::PaginationHelper
.load(uri: uri, params_hash: params_hash, key: "balances", type: UkrsibAPI::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)
21 |
# File 'lib/ukrsib_api/pagination_helper.rb', line 21 attribute :data, Models::Types::Array.of(Models::Types::Any) |
#next_page_exists ⇒ Boolean (readonly)
24 |
# File 'lib/ukrsib_api/pagination_helper.rb', line 24 attribute :next_page_exists, Models::Types::Bool |
#next_page_id ⇒ Any? (readonly)
27 |
# File 'lib/ukrsib_api/pagination_helper.rb', line 27 attribute :next_page_start, Models::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 |
# File 'lib/ukrsib_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]) new( data: transformed.map { |hash| type.new(hash) }, next_page_exists: body["total"].to_i > (body["firstResult"].to_i + body["maxResult"].to_i), # TODO: possible bug, >= if it is all zero based next_page_start: body["firstResult"].to_i + body["maxResult"].to_i ) 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/ukrsib_api/pagination_helper.rb', line 39 def self.paginate(params_hash:, key:, type:) params_hash[:maxResult] ||= 100 params_hash[:firstResult] ||= 0 Enumerator.new do |yielder| loop do 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 params_hash[:firstResult] = params_hash[:firstResult] + params_hash[:maxResult] end end end |