Class: CoreLibrary::OffsetPagination

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

#metadata_wrapper

Instance Method Summary collapse

Constructor Details

#initialize(input, metadata_wrapper) ⇒ OffsetPagination

Initializes an OffsetPagination instance with the given input pointer and metadata wrapper.

Parameters:

  • input (String)

    JSON pointer indicating the pagination parameter to update.

  • metadata_wrapper (Proc)

    Callable for handling pagination metadata.

Raises:

  • (ArgumentError)

    If input is nil.



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.

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/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.

Parameters:

  • paginated_data (PaginatedData)

    Contains the last response, request builder, and page size.

Returns:

  • (Object)

    An updated request builder configured for the next page request.



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.

Parameters:

  • page_response (Object)

    The response object for the current page.

Returns:

  • (Object)

    The result of the metadata wrapper with the page response and offset.



59
60
61
# File 'lib/apimatic-core/pagination/strategies/offset_pagination.rb', line 59

def (page_response)
  .call(page_response, @offset)
end