Sha256: 53c7fc409d6c0d2324147292a64d7fd6b85409a972a82fcbcddc5cdcd1370102

Contents?: true

Size: 862 Bytes

Versions: 3

Compression:

Stored size: 862 Bytes

Contents

class CursorResponseCollection < ResponseCollection
  attr_accessor :collected_objects

  def initialize(options = {})
    @collected_objects = [options[:seed_page]].flatten
    @per_page = options[:per_page].to_i
    @load_more_call = options[:load_more_call]
    @next_cursor = options[:next_cursor]
  end

  def each
    until @next_cursor.blank?
      objects_hsh = load_more_call.call(@next_cursor)
      objects = objects_hsh[:objects]
      @next_cursor = objects_hsh[:next_cursor]
      objects.each do |obj|
        yield obj
      end
      @collected_objects += objects
    end
  end

  def last
    to_a
    @collected_objects.last
  end

  def loaded_results
    @collected_objects.flatten
  end

  def total
    to_a
    loaded_results.size
  end

  alias_method :size, :total
  alias_method :length, :total

  def empty?
    to_a.empty?
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bright-2.1.1 lib/bright/cursor_response_collection.rb
bright-2.1 lib/bright/cursor_response_collection.rb
bright-2.0 lib/bright/cursor_response_collection.rb