Class: ReactorSDK::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor_sdk/paginator.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =

Number of records to request per page. Adobe’s maximum is 100 — always request the maximum to minimise the number of API calls made.

100

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Paginator

Returns a new instance of Paginator.

Parameters:



36
37
38
# File 'lib/reactor_sdk/paginator.rb', line 36

def initialize(connection)
  @connection = connection
end

Instance Method Details

#all(path, params: {}) {|Hash| ... } ⇒ Array<Hash>

Fetches every record from a paginated list endpoint.

Follows the ‘links.next` cursor in each response until it is absent, then returns all collected records as a single flat array.

An optional block can be provided to process each record as it arrives rather than waiting for all pages to complete — useful for large datasets.

Parameters:

  • path (String)

    Relative API path (e.g. “/properties/PR123/rules”)

  • params (Hash) (defaults to: {})

    Additional query parameters to merge into the request

Yields:

  • (Hash)

    Each raw JSON:API record hash as it is fetched (optional)

Returns:

  • (Array<Hash>)

    All records across every page as a flat array

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reactor_sdk/paginator.rb', line 55

def all(path, params: {})
  records  = []
  next_url = build_initial_url(path, params)

  while next_url
    response = @connection.get(next_url)
    data     = Array(response&.fetch('data', []))

    data.each do |record|
      yield record if block_given?
      records << record
    end

    next_url = response&.dig('links', 'next')
  end

  records
end