Class: Sumo::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Error
Defined in:
lib/sumo/collection.rb

Overview

This class is used to un-paginate results from the API. Specifically, this is currently used to page through records and messages returned by the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Collection

Create a new collection.



10
11
12
13
14
15
# File 'lib/sumo/collection.rb', line 10

def initialize(hash = {})
  @offset = hash[:offset] || 0
  @get_values = hash[:get_values]
  @get_status = hash[:get_status]
  @count_key = hash[:count_key]
end

Instance Attribute Details

#offsetObject (readonly)

Returns the value of attribute offset.



7
8
9
# File 'lib/sumo/collection.rb', line 7

def offset
  @offset
end

Instance Method Details

#each(&block) ⇒ Object

Iterate through each member of the collection, lazily making HTTP requests to get the next member. If no block is given, an ‘Enumerator` is returned.



19
20
21
22
23
24
# File 'lib/sumo/collection.rb', line 19

def each(&block)
  return enum_for(:each) if block.nil?
  page.each { |value| block.call(value) }
  remaining.each { |value| block.call(value) } if has_next_page?
  self
end