Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/master' into github_master
Browse files Browse the repository at this point in the history
  • Loading branch information
Holly Stotelmyer committed Jan 21, 2021
2 parents e40844f + 8a0479a commit 2b36813
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

##unreleased
* Allows `ResourceCollection#first` to receive one argument and return that argument number of items

## 3.1.0
* Add `acquirer_reference_number` to `Transaction`
* Add `billing_agreement_id` to `PayPalDetails`
Expand Down
11 changes: 8 additions & 3 deletions lib/braintree/resource_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ 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
# Returns the first or the first N items in the collection or nil if the collection is empty
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

# Only the maximum size of a resource collection can be determined since the data on the server can change while
Expand Down
29 changes: 29 additions & 0 deletions spec/unit/braintree/resource_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@
end
end

describe "#first" do
it 'returns nil with no results' do
values = %w(a b c d e)
collection = Braintree::ResourceCollection.new(:search_results => {:ids => [], :page_size => 2}) do |ids|
ids.map {|id| values[id] }
end

collection.first.should == nil
end

context 'with results' do
let(:collection) do
values = %w(a b c d e)

Braintree::ResourceCollection.new(:search_results => {:ids => [0,1,2,3,4], :page_size => 2}) do |ids|
ids.map {|id| values[id] }
end
end

it 'returns the first occourence' do
collection.first.should == 'a'
end

it 'returns the first N occourences' do
collection.first(4).should == ['a','b','c','d']
end
end
end

describe "#ids" do
it "returns a list of the resource collection ids" do
collection = Braintree::ResourceCollection.new(:search_results => {:ids => [0,1,2,3,4], :page_size => 2})
Expand Down

0 comments on commit 2b36813

Please sign in to comment.