Skip to content

Commit

Permalink
Fix bug when no result is found (Dinda-com-br#19)
Browse files Browse the repository at this point in the history
* 🐛 Fix bug when no result is found for a merchant order id

* Update README.md
  • Loading branch information
andre-rodrigues authored and danielcoca committed Feb 6, 2017
1 parent 2e6ee99 commit b89ef2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,15 @@ credit_card.saved = true
```rb
sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
sale.customer.name
=> Maria
=> "Maria"
```

### Find sales for a store, aka: MerchantOrderId

```rb
sales = BraspagRest::Sale.find_by_order_id('REQUEST_ID', 'MERCHANT_ORDER_ID')
sales.map { |sale| sale.customer.name }
=> ["Maria", "Joana"]
```

### Cancel a sale
Expand Down
2 changes: 1 addition & 1 deletion lib/braspag-rest/sale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.find_by_order_id(request_id, order_id)
response = BraspagRest::Request.get_sales_for_merchant_order_id(request_id, order_id)
payments = response.parsed_body['Payments']

payments.map { |payment| BraspagRest::Sale.find(request_id, payment['PaymentId']) }
Array(payments).map { |payment| BraspagRest::Sale.find(request_id, payment['PaymentId']) }
end

def save
Expand Down
15 changes: 14 additions & 1 deletion spec/sale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,23 @@
subject
end

it 'return a list of BraspagRest::Sale' do
it 'returns a list of BraspagRest::Sale' do
is_expected.to be_an Array
expect(subject.all? { |payment| payment.is_a?(BraspagRest::Sale) }).to be_truthy
end

context 'when no result is returned' do
before do
allow(BraspagRest::Request).to receive(:get_sales_for_merchant_order_id).and_return(
double(parsed_body: { 'Payments' => nil })
)
end

it 'returns an empty list' do
is_expected.to be_an Array
is_expected.to be_empty
end
end
end

describe '#reload' do
Expand Down

0 comments on commit b89ef2c

Please sign in to comment.