Class: CoreLibrary::LinkPagination
- Inherits:
-
PaginationStrategy
- Object
- PaginationStrategy
- CoreLibrary::LinkPagination
- Defined in:
- lib/apimatic-core/pagination/strategies/link_pagination.rb
Overview
Implements a pagination strategy that extracts the next page link from API responses using a JSON pointer.
This class updates the request builder with query parameters from the next page link and applies a metadata wrapper to the paged response.
Instance Attribute Summary
Attributes inherited from PaginationStrategy
Instance Method Summary collapse
-
#applicable?(response) ⇒ Boolean
Determines whether the link pagination strategy is applicable based on the given HTTP response.
-
#apply(paginated_data) ⇒ Object?
Updates the request builder with query parameters from the next page link extracted from the last API response.
-
#apply_metadata_wrapper(paged_response) ⇒ Object
Applies the metadata wrapper to the paged response, including the next page link.
-
#initialize(next_link_pointer, metadata_wrapper) ⇒ LinkPagination
constructor
Initializes a LinkPagination instance with the given next link pointer and metadata wrapper.
Constructor Details
#initialize(next_link_pointer, metadata_wrapper) ⇒ LinkPagination
Initializes a LinkPagination instance with the given next link pointer and metadata wrapper.
12 13 14 15 16 17 18 19 |
# File 'lib/apimatic-core/pagination/strategies/link_pagination.rb', line 12 def initialize(next_link_pointer, ) super() raise ArgumentError, 'Next link pointer for cursor based pagination cannot be nil' if next_link_pointer.nil? @next_link_pointer = next_link_pointer @next_link = nil end |
Instance Method Details
#applicable?(response) ⇒ Boolean
Determines whether the link pagination strategy is applicable based on the given HTTP response.
26 27 28 29 30 31 32 |
# File 'lib/apimatic-core/pagination/strategies/link_pagination.rb', line 26 def applicable?(response) return true if response.nil? @next_link = response.get_value_by_json_pointer(@next_link_pointer) !@next_link.nil? end |
#apply(paginated_data) ⇒ Object?
Updates the request builder with query parameters from the next page link extracted from the last API response.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/apimatic-core/pagination/strategies/link_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? @next_link = nil return request_builder end @next_link = last_response.get_value_by_json_pointer(@next_link_pointer) return nil if @next_link.nil? query_params = ApiHelper.get_query_parameters(@next_link) updated_query_params = DeepCloneUtils.deep_copy(request_builder.query_params) updated_query_params.merge!(query_params) request_builder.clone_with(query_params: updated_query_params) end |
#apply_metadata_wrapper(paged_response) ⇒ Object
Applies the metadata wrapper to the paged response, including the next page link.
63 64 65 |
# File 'lib/apimatic-core/pagination/strategies/link_pagination.rb', line 63 def (paged_response) @metadata_wrapper.call(paged_response, @next_link) end |