Sha256: d4dc25d90d636dea9a819613e4eed6c0906118f7cb4f718f2409605a392422cd

Contents?: true

Size: 1.08 KB

Versions: 49

Compression:

Stored size: 1.08 KB

Contents

module Braintree
  class ResourceCollection # :nodoc:
    include Enumerable

    attr_reader :ids

    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

    # Yields each item
    def each(&block)
      @ids.each_slice(@page_size) do |page_of_ids|
        resources = @paging_block.call(page_of_ids)
        resources.each(&block)
      end
    end

    def empty?
      @ids.empty?
    end

    # Returns the first item in the collection or nil if the collection is empty
    def first
      @paging_block.call([@ids.first]).first
    end

    # 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.
    def maximum_size
      @ids.size
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
braintree-3.1.0 lib/braintree/resource_collection.rb
braintree-3.0.1 lib/braintree/resource_collection.rb
braintree-2.104.1 lib/braintree/resource_collection.rb
braintree-2.104.0 lib/braintree/resource_collection.rb
braintree-2.103.0 lib/braintree/resource_collection.rb
braintree-2.102.0 lib/braintree/resource_collection.rb
braintree-2.101.0 lib/braintree/resource_collection.rb
braintree-2.100.0 lib/braintree/resource_collection.rb
braintree-2.99.0 lib/braintree/resource_collection.rb
braintree-2.98.0 lib/braintree/resource_collection.rb
braintree-2.97.0 lib/braintree/resource_collection.rb
braintree-2.96.0 lib/braintree/resource_collection.rb
braintree-2.95.0 lib/braintree/resource_collection.rb
braintree-2.94.0 lib/braintree/resource_collection.rb
braintree-2.93.0 lib/braintree/resource_collection.rb
braintree-2.92.0 lib/braintree/resource_collection.rb
braintree-2.91.0 lib/braintree/resource_collection.rb
braintree-2.90.0 lib/braintree/resource_collection.rb
braintree-2.89.0 lib/braintree/resource_collection.rb
braintree-2.88.0 lib/braintree/resource_collection.rb