Class: CoreLibrary::LinkPagination

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

#metadata_wrapper

Instance Method Summary collapse

Constructor Details

#initialize(next_link_pointer, metadata_wrapper) ⇒ LinkPagination

Initializes a LinkPagination instance with the given next link pointer and metadata wrapper.

Parameters:

  • next_link_pointer (String)

    JSON pointer to extract the next page link from the API response.

  • metadata_wrapper (Proc)

    A callable to wrap the paged response metadata.

Raises:

  • (ArgumentError)

    If next_link_pointer is nil.



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.

Parameters:

  • response (HttpResponse, nil)

    The response from the previous API call.

Returns:

  • (Boolean)

    true if this strategy is applicable based on the response; false otherwise.



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.

Parameters:

  • paginated_data (Object)

    An object containing the last API response and the current request builder.

Returns:

  • (Object, nil)

    A new request builder with updated query parameters, or nil if no next link is found.



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.

Parameters:

  • paged_response (Object)

    The API response object for the current page.

Returns:

  • (Object)

    The result of the metadata wrapper, typically containing the response and next 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