Class: CoreLibrary::PagePagination

Inherits:
PaginationStrategy show all
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

#metadata_wrapper

Instance Method Summary collapse

Constructor Details

#initialize(input, metadata_wrapper) ⇒ PagePagination

Initializes a PagePagination instance with the given input pointer and metadata wrapper.

Parameters:

  • input (String)

    JSON pointer indicating the pagination parameter in the request.

  • metadata_wrapper (Proc)

    A callable for wrapping pagination metadata.

Raises:

  • (ArgumentError)

    If input is nil.



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.

Parameters:

  • _response (HttpResponse, nil)

    The response from the previous API call.

Returns:

  • (Boolean)

    Always returns true, as this strategy does not depend on the response content.



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.

Parameters:

  • paginated_data (PaginatedData)

    An object containing the last response, request builder, and page size.

Returns:

  • (Object)

    The updated request builder configured for the next page request.



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.

Parameters:

  • paged_response (Object)

    The response object for the current page.

Returns:

  • (Object)

    The result of the metadata wrapper with the paged response and 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