Class: SignalWire::REST::PaginatedIterator
- Inherits:
-
Object
- Object
- SignalWire::REST::PaginatedIterator
- Includes:
- Enumerable
- Defined in:
- lib/signalwire/rest/pagination.rb
Overview
Iterates items across paginated API responses.
Mirrors the Python PaginatedIterator (signalwire.rest._pagination): the constructor records http/path/params/data_key without fetching; iteration walks pages by following the links.next cursor.
Usage:
iter = SignalWire::REST::PaginatedIterator.new(http, '/api/path',
params: {}, data_key: 'data')
iter.each { |item| ... }
The iterator is single-pass (matching Python’s __next__ semantics); use #to_a to collect every item across all pages.
Instance Attribute Summary collapse
-
#data_key ⇒ Object
readonly
Returns the value of attribute data_key.
-
#done ⇒ Object
readonly
Returns the value of attribute done.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(http, path, params = nil, data_key = 'data') ⇒ PaginatedIterator
constructor
A new instance of PaginatedIterator.
-
#next_item ⇒ Object
Equivalent of Python’s __next__.
Constructor Details
#initialize(http, path, params = nil, data_key = 'data') ⇒ PaginatedIterator
Returns a new instance of PaginatedIterator.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/signalwire/rest/pagination.rb', line 23 def initialize(http, path, params = nil, data_key = 'data') @http = http @path = path # Dup so callers can't mutate iterator state via the original Hash. @params = params ? params.dup : {} @data_key = data_key @items = [] @index = 0 @done = false end |
Instance Attribute Details
#data_key ⇒ Object (readonly)
Returns the value of attribute data_key.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def data_key @data_key end |
#done ⇒ Object (readonly)
Returns the value of attribute done.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def done @done end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def http @http end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def index @index end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def items @items end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/signalwire/rest/pagination.rb', line 21 def path @path end |
Instance Method Details
#each ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/signalwire/rest/pagination.rb', line 34 def each return enum_for(:each) unless block_given? loop do item = next_item break if item == :__stop__ yield item end end |
#next_item ⇒ Object
Equivalent of Python’s __next__. Returns the sentinel :__stop__ when exhausted (Ruby has no StopIteration error idiom for plain Enumerable), but the public surface is #each.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/signalwire/rest/pagination.rb', line 48 def next_item while @index >= @items.length return :__stop__ if @done fetch_next end item = @items[@index] @index += 1 item end |