Class: CoreLibrary::OffsetPagination
- Inherits:
-
PaginationStrategy
- Object
- PaginationStrategy
- CoreLibrary::OffsetPagination
- Defined in:
- lib/apimatic-core/pagination/strategies/offset_pagination.rb
Overview
Implements offset-based pagination strategy for API responses.
This class manages pagination by updating an offset parameter in the request builder, allowing sequential retrieval of paginated data. It extracts and updates the offset based on a configurable JSON pointer and applies a metadata wrapper to each page response.
Instance Attribute Summary
Attributes inherited from PaginationStrategy
Instance Method Summary collapse
-
#applicable?(_response) ⇒ Boolean
Determines whether the offset pagination strategy is applicable based on the given HTTP response.
-
#apply(paginated_data) ⇒ Object
Updates the request builder to fetch the next page of results using offset-based pagination.
-
#apply_metadata_wrapper(page_response) ⇒ Object
Applies the metadata wrapper to the given page response, passing the current offset.
-
#initialize(input, metadata_wrapper) ⇒ OffsetPagination
constructor
Initializes an OffsetPagination instance with the given input pointer and metadata wrapper.
Constructor Details
#initialize(input, metadata_wrapper) ⇒ OffsetPagination
Initializes an OffsetPagination instance with the given input pointer and metadata wrapper.
13 14 15 16 17 18 19 20 |
# File 'lib/apimatic-core/pagination/strategies/offset_pagination.rb', line 13 def initialize(input, ) super() raise ArgumentError, 'Input pointer for offset based pagination cannot be nil' if input.nil? @input = input @offset = 0 end |
Instance Method Details
#applicable?(_response) ⇒ Boolean
Determines whether the offset pagination strategy is applicable based on the given HTTP response.
27 28 29 |
# File 'lib/apimatic-core/pagination/strategies/offset_pagination.rb', line 27 def applicable?(_response) true end |
#apply(paginated_data) ⇒ Object
Updates the request builder to fetch the next page of results using offset-based pagination.
If this is the first page, initializes the offset from the request builder. Otherwise, increments the offset by the previous page size and updates the pagination parameter.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/apimatic-core/pagination/strategies/offset_pagination.rb', line 38 def apply(paginated_data) last_response = paginated_data.last_response request_builder = paginated_data.request_builder # If there is no response yet, this is the first page if last_response.nil? param_value = request_builder.get_parameter_value_by_json_pointer(@input) @offset = param_value.nil? ? 0 : Integer(param_value) return request_builder end @offset += paginated_data.page_size request_builder.get_updated_request_by_json_pointer(@input, @offset) end |
#apply_metadata_wrapper(page_response) ⇒ Object
Applies the metadata wrapper to the given page response, passing the current offset.
59 60 61 |
# File 'lib/apimatic-core/pagination/strategies/offset_pagination.rb', line 59 def (page_response) .call(page_response, @offset) end |