Skip to content

Commit

Permalink
feat: adds smartrate functionality (#117)
Browse files Browse the repository at this point in the history
* feat: adds smartrate functionality

* better smartrate test assertions

* fix: do not use refresh_from on get_smartrates

* fix: strip result key from smartrate response

* digs for smartrate result key

* fix: add empty array fallback to smartrate
  • Loading branch information
Justintime50 authored Jun 10, 2021
1 parent 2d622fd commit e9a19c4
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/easypost/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def get_rates(params={})
return self
end

def get_smartrates
response = EasyPost.make_request(:get, url + '/smartrate', @api_key)

return response.fetch('result', [])
end

def buy(params={})
if params.instance_of?(EasyPost::Rate)
temp = params.clone
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions spec/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,25 @@
expect(shipment.id).not_to eq(tracking_code)
end
end

describe '#smartrate' do
it 'retrieves the smartrates for a shipment' do
shipment = EasyPost::Shipment.create(
:to_address => ADDRESS[:missouri],
:from_address => ADDRESS[:california],
:parcel => PARCEL[:dimensions]
)
expect(shipment.rates).not_to be nil

smartrates = shipment.get_smartrates
expect(shipment.rates[0]['id']).to eq(smartrates[0]['id'])
expect(smartrates[0]['time_in_transit']['percentile_50']).to eq(1)
expect(smartrates[0]['time_in_transit']['percentile_75']).to eq(2)
expect(smartrates[0]['time_in_transit']['percentile_85']).to eq(2)
expect(smartrates[0]['time_in_transit']['percentile_90']).to eq(3)
expect(smartrates[0]['time_in_transit']['percentile_95']).to eq(3)
expect(smartrates[0]['time_in_transit']['percentile_97']).to eq(4)
expect(smartrates[0]['time_in_transit']['percentile_99']).to eq(5)
end
end
end

0 comments on commit e9a19c4

Please sign in to comment.