Skip to content

Instantly share code, notes, and snippets.

@jamonholmgren
Created October 24, 2012 16:21
Show Gist options
  • Save jamonholmgren/3947091 to your computer and use it in GitHub Desktop.
Save jamonholmgren/3947091 to your computer and use it in GitHub Desktop.
class UspsShipper
attr_accessor :order, :saddle
def usps
# removed usps api init code here...it works
end
def package
# Create the package
@package ||= Package.new(
saddle.package_weight_ounces,
[saddle.package_width, saddle.package_height],
units: :imperial
)
end
def origin
# Define the origin of package
@origin ||= Location.new(
country: "US",
state: "WA",
city: "Camas",
zip: "98607"
)
end
def destination
@destination ||= begin
if order.american?
# American orders
destination = Location.new(
country: "US",
state: order.shipping_state,
city: order.shipping_city,
zip: order.shipping_zip_code
)
else
# International orders
destination = Location.new(
country: order.shipping_country,
province: order.shipping_province,
city: order.shipping_city,
postal_code: order.shipping_zip_code
)
end
destination
end
end
def usps_shipping_rates
usps_rates = nil
usps = USPS.new(
login: "960ANSUR0115",
test: :false
)
# Query just USPS
begin
usps_response = usps.find_rates(origin, destination, package) if usps.valid_credentials?
rescue
usps_response = nil
end
if usps_response
usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
end
usps_rates
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment