Class: TesoteSdk::Pagination::OffsetEnumerator

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

Overview

Offset pagination: walks until has_more is false, advancing ‘offset` by `limit`. Yields each page hash.

Instance Method Summary collapse

Constructor Details

#initialize(start_query: {}, limit: 50, &fetch_page) ⇒ OffsetEnumerator

Returns a new instance of OffsetEnumerator.

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'lib/tesote_sdk/pagination.rb', line 65

def initialize(start_query: {}, limit: 50, &fetch_page)
  raise ArgumentError, 'block (fetch_page) is required' unless block_given?

  @start_query = start_query.dup
  @limit = limit
  @fetch_page = fetch_page
end

Instance Method Details

#eachObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tesote_sdk/pagination.rb', line 73

def each
  return enum_for(:each) unless block_given?

  offset = (@start_query[:offset] || @start_query['offset'] || 0).to_i
  loop do
    query = @start_query.merge(limit: @limit, offset: offset)
    page = @fetch_page.call(query)
    yield page

    break unless page_has_more?(page)

    offset += @limit
  end
end