Class: CoreLibrary::PagePagination
- Inherits:
-
PaginationStrategy
- Object
- PaginationStrategy
- CoreLibrary::PagePagination
- Defined in:
- lib/apimatic-core/pagination/strategies/page_pagination.rb
Overview
Implements a page-based pagination strategy for API requests.
This class manages pagination by updating the request builder with the appropriate page number, using a JSON pointer to identify the pagination parameter. It also applies a metadata wrapper to each paged response, including the current page number.
Instance Attribute Summary
Attributes inherited from PaginationStrategy
Instance Method Summary collapse
-
#applicable?(_response) ⇒ Boolean
Determines whether the page 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 based on the current paginated data.
-
#apply_metadata_wrapper(paged_response) ⇒ Object
Applies the metadata wrapper to the paged response, including the current page number.
-
#initialize(input, metadata_wrapper) ⇒ PagePagination
constructor
Initializes a PagePagination instance with the given input pointer and metadata wrapper.
Constructor Details
#initialize(input, metadata_wrapper) ⇒ PagePagination
Initializes a PagePagination instance with the given input pointer and metadata wrapper.
13 14 15 16 17 18 19 20 |
# File 'lib/apimatic-core/pagination/strategies/page_pagination.rb', line 13 def initialize(input, ) super() raise ArgumentError, 'Input pointer for page based pagination cannot be nil' if input.nil? @input = input @page_number = 1 end |
Instance Method Details
#applicable?(_response) ⇒ Boolean
Determines whether the page pagination strategy is applicable based on the given HTTP response.
27 28 29 |
# File 'lib/apimatic-core/pagination/strategies/page_pagination.rb', line 27 def applicable?(_response) true end |
#apply(paginated_data) ⇒ Object
Updates the request builder to fetch the next page of results based on the current paginated data.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/apimatic-core/pagination/strategies/page_pagination.rb', line 35 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) @page_number = param_value.nil? ? 1 : Integer(param_value) return request_builder end @page_number += 1 if paginated_data.page_size.positive? request_builder.get_updated_request_by_json_pointer(@input, @page_number) end |
#apply_metadata_wrapper(paged_response) ⇒ Object
Applies the metadata wrapper to the paged response, including the current page number.
56 57 58 |
# File 'lib/apimatic-core/pagination/strategies/page_pagination.rb', line 56 def (paged_response) @metadata_wrapper.call(paged_response, @page_number) end |