Skip to content

Instantly share code, notes, and snippets.

@jstreebin
Created January 30, 2014 00:04
Show Gist options
  • Save jstreebin/8699919 to your computer and use it in GitHub Desktop.
Save jstreebin/8699919 to your computer and use it in GitHub Desktop.
Batch script
require 'csv'
require 'easypost'
EasyPost.api_key = 'INSERT PROD KEY HERE'
from_address = EasyPost::Address.create(
:name => 'Dr. Steve Brule',
:street1 => '179 N Harbor Dr',
:city => 'Redondo Beach',
:state => 'CA',
:zip => '90277',
:country => 'US',
:email => 'dr_steve_brule@gmail.com'
)
shipments = []
CSV.foreach('addresses.csv', :headers => true, :encoding => 'windows-1251:utf-8') do |row|
puts row[0]
to_address = EasyPost::Address.create(
:name => row[0],
:street1 => row[1],
:city => row[2],
:state => row[3],
:zip => row[4],
:country => row[5]
)
parcel = EasyPost::Parcel.create(
:predefined_package => 'Flat',
:weight => row[6].gsub('oz', '').to_f
)
if row[5].upcase != 'UNITED STATES'
customs_info = EasyPost::CustomsInfo.create(
:eel_pfc => 'NOEEI 30.37(a)',
:customs_certify => true,
:customs_signer => 'Indivisbile Shirts',
:contents_type => 'merchandise',
:contents_explanation => '',
:restriction_type => 'none',
:restriction_comments => '',
:non_delivery_option => 'abandon',
:customs_items => [{
'description' => 'Sweet shirts',
'quantity' => 1,
'weight' => row[6].gsub('oz', '').to_f,
'value' => 10,
'hs_tariff_number' => 123456,
'origin_country' => 'US'
}]
)
shipments << {
:to_address => to_address,
:from_address => from_address,
:parcel => parcel,
:service => 'First',
:carrier => 'USPS',
:customs_info => customs_info
}
else
shipments << {
:to_address => to_address,
:from_address => from_address,
:service => 'First',
:carrier => 'USPS',
:parcel => parcel
}
end
end
#wait for a response, then do the following calls
batch = EasyPost::Batch.create({:shipments => shipments})
puts batch.id
batch.buy()
#you can also do batch.refresh to check status. These happen asychronously so give it time (minutes, not hours)
#after purchased
#batch = EasyPost::Batch.retrieve('batch_id_here')
#batch.label({:file_format => 'epl2'}) if you want a different format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment