Created
October 29, 2010 03:15
-
-
Save craigs/652827 to your computer and use it in GitHub Desktop.
Australia Post's DRC rate calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Interface to Australia Post's DRC rate calculator | |
# | |
# Merchant’s web site must display statements re DRC | |
# It is a condition of use of the DRC that certain statements are prominently displayed on the Merchant’s web site. | |
# These statements can be found at web site http://www.edeliver.com.au/Templates/ifs/IFS_DRC_Terms.htm in the section headed "Your representations". | |
class AusPost | |
AUSPOST_DRC_URL = "http://drc.edeliver.com.au/ratecalc.asp" | |
# domestic services | |
SERVICE_TYPE_STANDARD = "STANDARD" # Regular Parcels | |
SERVICE_TYPE_EXPRESS = "EXPRESS" # Express Parcels | |
SERVICE_TYPE_EXP_PLT = "EXP_PLT" # Express Parcels Platinum | |
# international services | |
SERVICE_TYPE_AIR = "AIR" # Air Mail | |
SERVICE_TYPE_SEA = "SEA" # Sea Mail | |
SERVICE_TYPE_ECI_D = "ECI_D" # Express Courier International Document | |
SERVICE_TYPE_ECI_M = "ECI_M" # Express Courier International Merchandise | |
SERVICE_TYPE_EPI = "EPI" # Express Post International | |
SERVICES_DOMESTIC = { | |
SERVICE_TYPE_STANDARD => "Regular Parcels", | |
SERVICE_TYPE_EXPRESS => "Express Parcels", | |
SERVICE_TYPE_EXP_PLT => "Express Platinum Parcels" | |
} | |
SERVICES_INTERNATIONAL = { | |
SERVICE_TYPE_AIR => "International Air Parcels", | |
SERVICE_TYPE_SEA => "International Sea Parcels", | |
SERVICE_TYPE_ECI_D => "Express Courier International Document", | |
SERVICE_TYPE_ECI_M => "Express Courier International Merchandise", | |
SERVICE_TYPE_EPI => "Express Post International" | |
} | |
attr_accessor :pickup_postcode, :destination_postcode, :country, :service_type, :weight, :length, :width, :height, :quantity | |
attr_accessor :charge, :err_msg, :days | |
def test_call | |
self.pickup_postcode = 3083 | |
self.destination_postcode = 3000 | |
self.country = "AU" | |
self.service_type = AusPost::SERVICE_TYPE_EXPRESS | |
self.weight = 500 | |
self.length = 250 | |
self.width = 250 | |
self.height = 250 | |
self.quantity = 1 | |
calculate_freight | |
end | |
def calculate_freight | |
@err_msg = nil | |
@charge = nil | |
@days = nil | |
response = split_params( | |
RestClient.post(AUSPOST_DRC_URL, | |
:pickup_postcode => pickup_postcode, | |
:destination_postcode => destination_postcode, | |
:country => country || 'AU', | |
:service_type => service_type || AusPost::SERVICE_TYPE_STANDARD, | |
:weight => weight, | |
:length => length, :width => width, :height => height, | |
:quantity => quantity || 1)) | |
@charge = response[:charge].to_f | |
@err_msg = response[:err_msg] | |
@err_msg = nil if response[:err_msg] == "OK" | |
@days = response[:days] | |
response | |
end | |
def is_error? | |
!@err_msg.nil? | |
end | |
def split_params(parameters) | |
return_values = {} | |
parameters.split("\r\n").each do |p| | |
param = p.split("=") | |
return_values[param[0].to_sym] = param[1].chomp | |
end | |
return_values | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment