class Sumo::Collection
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.
Attributes
offset[R]
Public Class Methods
new(hash = {})
click to toggle source
Create a new collection.
# 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
Public Instance Methods
each(&block)
click to toggle source
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.
# 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
Private Instance Methods
get_new_status()
click to toggle source
# File lib/sumo/collection.rb, line 36 def get_new_status stat = { 'state' => '', @count_key => @offset } until (@offset < stat[@count_key]) || stat['state'].start_with?('DONE') stat = @get_status.call sleep 1 end stat end
has_next_page?()
click to toggle source
# File lib/sumo/collection.rb, line 74 def has_next_page? ['GATHERING RESULTS', 'NOT STARTED'].include?(state) end
has_results?()
click to toggle source
# File lib/sumo/collection.rb, line 61 def has_results? limit > 0 end
limit()
click to toggle source
# File lib/sumo/collection.rb, line 66 def limit @limit ||= begin natural_limit = total - offset (natural_limit <= 1000) ? natural_limit : 1000 end end
page()
click to toggle source
# File lib/sumo/collection.rb, line 56 def page @page ||= has_results? ? values(:offset => offset, :limit => limit) : [] end
remaining()
click to toggle source
# File lib/sumo/collection.rb, line 79 def remaining @remaining ||= Sumo::Collection.new( :offset => offset + limit, :get_values => @get_values, :get_status => @get_status, :count_key => @count_key ) end
state()
click to toggle source
# File lib/sumo/collection.rb, line 51 def state status['state'] end
status()
click to toggle source
# File lib/sumo/collection.rb, line 31 def status @status ||= get_new_status end
total()
click to toggle source
# File lib/sumo/collection.rb, line 46 def total status[@count_key] end
values(hash)
click to toggle source
# File lib/sumo/collection.rb, line 26 def values(hash) @get_values.call(hash) end