Class: Braintree::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/braintree/resource_collection.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(response, &block) ⇒ ResourceCollection

:nodoc:



5
6
7
8
9
# File 'lib/braintree/resource_collection.rb', line 5

def initialize(response, &block) # :nodoc:
  @ids = Util.extract_attribute_as_array(response[:search_results], :ids)
  @page_size = response[:search_results][:page_size]
  @paging_block = block
end

Instance Method Details

#each(&block) ⇒ Object

Yields each item



12
13
14
15
16
17
# File 'lib/braintree/resource_collection.rb', line 12

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?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/braintree/resource_collection.rb', line 19

def empty?
  @ids.empty?
end

#firstObject

Returns the first item in the collection or nil if the collection is empty



24
25
26
# File 'lib/braintree/resource_collection.rb', line 24

def first
  @paging_block.call([@ids.first]).first
end

#maximum_sizeObject

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.



31
32
33
# File 'lib/braintree/resource_collection.rb', line 31

def maximum_size
  @ids.size
end