Class: BaseCradle::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/basecradle/pagination.rb

Overview

The shared cursor-pagination engine. Every list endpoint paginates the same way: newest first, up to 50 per page, next_cursor in the response passed back as ?before= for the next (older) page, a null cursor meaning the end.

Lazy and Enumerable: the first page is fetched when iteration starts, and page N+1 only when iteration crosses the page boundary — so first/take/find stop early and cursors never appear in calling code.

Instance Method Summary collapse

Constructor Details

#initialize(client, path, envelope_key:, model:, params: nil) ⇒ Paginator

Returns a new instance of Paginator.



14
15
16
17
18
19
20
# File 'lib/basecradle/pagination.rb', line 14

def initialize(client, path, envelope_key:, model:, params: nil)
  @client = client
  @path = path
  @envelope_key = envelope_key
  @model = model
  @params = params || {}
end

Instance Method Details

#eachObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/basecradle/pagination.rb', line 22

def each
  return enum_for(:each) unless block_given?

  cursor = nil
  loop do
    page = @client.request("GET", @path, params: page_params(cursor))
    page.fetch(@envelope_key).each { |data| yield @model.new(data, client: @client) }
    cursor = page["next_cursor"]
    break if cursor.nil?
  end
end