Class: NgrokAPI::PagedIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/ngrokapi/paged_iterator.rb

Overview

Low level class which allows the user to iterate through the results of a list API call

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, page:, list_property:, danger: false) ⇒ PagedIterator

Returns a new instance of PagedIterator.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ngrokapi/paged_iterator.rb', line 10

def initialize(
  client:,
  page:,
  list_property:,
  danger: false
)
  @n = 0
  @client = client
  @list_property = list_property
  @page = page
  @danger = danger
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/ngrokapi/paged_iterator.rb', line 8

def client
  @client
end

#list_propertyObject (readonly)

Returns the value of attribute list_property.



8
9
10
# File 'lib/ngrokapi/paged_iterator.rb', line 8

def list_property
  @list_property
end

#nObject

Returns the value of attribute n.



7
8
9
# File 'lib/ngrokapi/paged_iterator.rb', line 7

def n
  @n
end

#pageObject

Returns the value of attribute page.



7
8
9
# File 'lib/ngrokapi/paged_iterator.rb', line 7

def page
  @page
end

Instance Method Details

#get_nextobject

Iterate through the result set, returning the next instance if we already have one, or make a new API call to next_page_uri to get more results and return the next one from that call.

Returns:

  • (object)

    Returns an instance of a class.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ngrokapi/paged_iterator.rb', line 28

def get_next
  item = @page.attrs[@list_property][@n]
  raise "None" if item.nil?
  self.n += 1
  item
rescue
  if @page.next_page_uri
    res = @client.list(danger: @danger, url: @page.next_page_uri)
    self.n = 0
    self.page = res
    get_next
  end
end