Class: Agendrix::Nethris::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/agendrix/nethris/collection_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, entity_class, options = {}) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



6
7
8
9
10
# File 'lib/agendrix/nethris/collection_proxy.rb', line 6

def initialize(client, entity_class, options = {})
  @client = client
  @entity_class = entity_class
  @options = options
end

Instance Method Details

#[](target_index) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/agendrix/nethris/collection_proxy.rb', line 35

def [](target_index)
  self.each_with_index do |item, index|
    return item if index == target_index
  end

  nil
end

#each(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agendrix/nethris/collection_proxy.rb', line 12

def each(&block)
  has_next_page = false
  current_page = 0

  loop do
    current_page += 1
    body = fetch(page: current_page)

    current_page = body["CurrentPage"].to_i
    ipp = body["RecordsPerPage"].to_i
    total_count = body["TotalCount"].to_i
    has_next_page = current_page * ipp < total_count

    body["data"].each do |entity_hash|
      yield(@entity_class.new(@client, entity_hash))
    end

    break if !has_next_page
  end

  self
end