Class: ReactorSDK::Paginator
- Inherits:
-
Object
- Object
- ReactorSDK::Paginator
- 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
-
#all(path, params: {}) {|Hash| ... } ⇒ Array<Hash>
Fetches every record from a paginated list endpoint.
-
#initialize(connection) ⇒ Paginator
constructor
A new instance of Paginator.
Constructor Details
#initialize(connection) ⇒ Paginator
Returns a new instance of Paginator.
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.
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 |