-
Notifications
You must be signed in to change notification settings - Fork 12
/
web.rb
61 lines (50 loc) · 1.34 KB
/
web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'sinatra'
require 'easypost'
require 'dotenv'
Dotenv.load
EasyPost.api_key = ENV['EASYPOST_API_KEY']
get '/rates' do
content_type :json
toAddress = {
:street1 => params[:address],
:city => params[:city],
:state => params[:state],
:zip => params[:zip],
:country => params[:country]
}
fromAddress = {
:company => 'EasyPost',
:street1 => '164 Townsend Street',
:street2 => 'Unit 1',
:city => 'San Francisco',
:state => 'CA',
:zip => '94107',
:phone => '415-528-7555'
}
parcel = {
:length => 9,
:width => 6,
:height => 2,
:weight => 10
}
shipment = EasyPost::Shipment.create(
:to_address => toAddress,
:from_address => fromAddress,
:parcel => parcel
)
shipment.rates.sort_by do |rate|
rate.rate.to_i
end.each_with_index.map do |rate, i|
# Annoyingly, we don't have very good information here in testmode, so we'll just make up a value. cc @easypost :)
arrival_date = Time.now + (shipment.rates.count - i + 1) * (60*60*24)
formatted_arrival_date = arrival_date.strftime("Will arrive by %A, %b %-e")
{
:id => rate.id,
:carrier => rate.carrier,
:service => rate.service,
:amount => rate.rate,
:currency => rate.currency,
:formatted_arrival_date => formatted_arrival_date
}
end.to_json
end