class Braintree::ResourceCollection

Attributes

ids[R]

Public Class Methods

new(response, &block) click to toggle source
# File lib/braintree/resource_collection.rb, line 7
def initialize(response, &block)
  @ids = Util.extract_attribute_as_array(response[:search_results], :ids)
  @page_size = response[:search_results][:page_size]
  @paging_block = block
end

Public Instance Methods

each(&block) click to toggle source

Yields each item

# File lib/braintree/resource_collection.rb, line 14
def each(&block)
  @ids.each_slice(@page_size) do |page_of_ids|
    resources = @paging_block.call(page_of_ids)
    resources.each(&block)
  end
end
empty?() click to toggle source
# File lib/braintree/resource_collection.rb, line 21
def empty?
  @ids.empty?
end
first(amount = 1) click to toggle source

Returns the first or the first N items in the collection or nil if the collection is empty

# File lib/braintree/resource_collection.rb, line 26
def first(amount = 1)
  return nil if @ids.empty?
  return @paging_block.call([@ids.first]).first if amount == 1

  @ids.first(amount).each_slice(@page_size).flat_map do |page_of_ids|
    @paging_block.call(page_of_ids)
  end
end
maximum_size() click to toggle source

Only the maximum size of a resource collection can be determined since the data on the server can change while fetching blocks of results for iteration. For example, customers can be deleted while iterating, so the number of results iterated over may be less than the maximum_size. In general, this method should be avoided.

# File lib/braintree/resource_collection.rb, line 38
def maximum_size
  @ids.size
end