Class: SignalWire::REST::PaginatedIterator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_keyObject (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

#doneObject (readonly)

Returns the value of attribute done.



21
22
23
# File 'lib/signalwire/rest/pagination.rb', line 21

def done
  @done
end

#httpObject (readonly)

Returns the value of attribute http.



21
22
23
# File 'lib/signalwire/rest/pagination.rb', line 21

def http
  @http
end

#indexObject (readonly)

Returns the value of attribute index.



21
22
23
# File 'lib/signalwire/rest/pagination.rb', line 21

def index
  @index
end

#itemsObject (readonly)

Returns the value of attribute items.



21
22
23
# File 'lib/signalwire/rest/pagination.rb', line 21

def items
  @items
end

#paramsObject (readonly)

Returns the value of attribute params.



21
22
23
# File 'lib/signalwire/rest/pagination.rb', line 21

def params
  @params
end

#pathObject (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

#eachObject



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_itemObject

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