Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Created December 13, 2008 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmacaulay/35532 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/35532 to your computer and use it in GitHub Desktop.
include ActiveMerchant::Shipping
usps = USPS.new(:login => '123JAMES4567')
origin = Location.new(:country => 'US', :zip => '90210')
destination = Location.new(:country => 'CA', :postal_code => 'K2P 0K3')
grams = 100
centimetres = [20,10,10]
packages = [Package.new(grams, centimetres)]
response = usps.find_rates(origin, destination, packages)
rates = response.rates.sort_by(&:price)
cheapest = rates.map {|r| [ r.service_name,
r.price.to_f / 100, # rates returned in cents
r.currency]}.first
# => ["USPS First-Class Mail International", 1.62, "USD"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment