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.



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

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.



10
11
12
# File 'lib/ngrokapi/paged_iterator.rb', line 10

def client
  @client
end

#list_propertyObject (readonly)

Returns the value of attribute list_property.



10
11
12
# File 'lib/ngrokapi/paged_iterator.rb', line 10

def list_property
  @list_property
end

#nObject

Returns the value of attribute n.



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

def n
  @n
end

#pageObject

Returns the value of attribute page.



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

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.



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

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