forked from gaffneyc/usps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added USPS International Shipping Rate (IntlRateV2) functionality
USPS::Request::InternationalShippingRatesLookup can be used with USPS::Request::Package::InternationalPackage to calculate international shipping rates. Because of the profileration of shipping-rate-related classes, the files have been reorganized. Squashed commit of the following: commit a1cd232 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Tue Sep 18 11:54:35 2012 -0400 fixed bug related to required properties in Packages commit 565f3d3 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Tue Sep 18 11:01:10 2012 -0400 updated 'missing package' error messages commit fac4887 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Tue Sep 18 10:23:34 2012 -0400 added specs for response commit f3b92d8 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Mon Sep 17 10:15:23 2012 -0400 tests for international package request and lookup commit a9e7769 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Thu Sep 13 15:34:39 2012 -0400 allow package to be initialized in block, not just with option hash commit 3901ddd Author: Nicholas Yianilos <npy@reenhanced.com> Date: Thu Sep 13 15:29:22 2012 -0400 always include dimension elements, even if they're blank commit 163e3d1 Author: Nicholas Yianilos <npy@reenhanced.com> Date: Wed Sep 12 17:00:01 2012 -0400 Implemented RateV2 (Int'l) API. Restructured existing Shipping Rate (V2) functionality. Needs better testing
- Loading branch information
Nicholas Yianilos
committed
Sep 18, 2012
1 parent
95bdba0
commit 235111b
Showing
24 changed files
with
490 additions
and
62 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,15 +1,17 @@ | |||
module USPS::Request | module USPS::Request | ||
autoload :Base, 'usps/request/base' | autoload :Base, 'usps/request/base' | ||
autoload :ZipCodeLookup, 'usps/request/zip_code_lookup' | autoload :ZipCodeLookup, 'usps/request/zip_code_lookup' | ||
autoload :CityAndStateLookup, 'usps/request/city_and_state_lookup' | autoload :CityAndStateLookup, 'usps/request/city_and_state_lookup' | ||
autoload :AddressStandardization, 'usps/request/address_standardization' | autoload :AddressStandardization, 'usps/request/address_standardization' | ||
|
|
||
# Delivery and Signature confirmation. | # Delivery and Signature confirmation. | ||
# DeliveryConfirmationCertify and SignatureConfirmationCertify should be used for testing | # DeliveryConfirmationCertify and SignatureConfirmationCertify should be used for testing | ||
autoload :DeliveryConfirmation, 'usps/request/delivery_confirmation' | autoload :DeliveryConfirmation, 'usps/request/delivery_confirmation' | ||
autoload :DeliveryConfirmationCertify, 'usps/request/delivery_confirmation_certify' | autoload :DeliveryConfirmationCertify, 'usps/request/delivery_confirmation_certify' | ||
|
|
||
autoload :TrackingLookup, 'usps/request/tracking_lookup' | autoload :TrackingLookup, 'usps/request/tracking_lookup' | ||
autoload :TrackingFieldLookup, 'usps/request/tracking_field_lookup' | autoload :TrackingFieldLookup, 'usps/request/tracking_field_lookup' | ||
autoload :ShippingRatesLookup, 'usps/request/shipping_rates_lookup' | autoload :ShippingRatesLookup, 'usps/request/shipping_rates_lookup' | ||
autoload :InternationalShippingRatesLookup, 'usps/request/international_shipping_rates_lookup' | |||
autoload :Package, 'usps/request/package' | |||
end | end |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
module USPS::Request | |||
|
|||
class InternationalShippingRatesLookup < Base | |||
config( | |||
:api => 'IntlRateV2', | |||
:tag => 'IntlRateV2Request', | |||
:secure => false, | |||
:response => USPS::Response::InternationalShippingRatesLookup | |||
) | |||
|
|||
def initialize(*packages) | |||
@packages = packages.flatten | |||
if @packages.none? | |||
raise ArgumentError, 'An IntlRateV2 shipping rate lookup requires at least one package (USPS::Request::Package::InternationalPackage)' | |||
end | |||
end | |||
|
|||
def build | |||
super do |xml| | |||
xml.Revision 2 | |||
@packages.each do |package| | |||
xml.Package :ID => package.id do | |||
xml.Pounds package.pounds | |||
xml.Ounces package.ounces | |||
xml.Machinable 'true' # for Service=ALL | |||
xml.MailType package.mail_type | |||
xml.ValueOfContents 103 | |||
xml.Country package.country | |||
xml.Container package.container | |||
xml.Size package.size | |||
xml.Width package.width | |||
xml.Length package.length | |||
xml.Height package.height | |||
xml.Girth nil | |||
xml.CommercialFlag 'N' | |||
end | |||
end | |||
end | |||
end | |||
|
|||
end | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
module USPS::Request::Package | |||
autoload :Base, 'usps/request/package/base' | |||
autoload :DomesticPackage, 'usps/request/package/domestic_package' | |||
autoload :InternationalPackage, 'usps/request/package/international_package' | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
module USPS::Request::Package | |||
class Base | |||
attr_accessor :id | |||
attr_accessor :pounds, :ounces | |||
attr_accessor :container | |||
attr_accessor :size | |||
attr_accessor :width, :length, :height, :girth | |||
|
|||
@required = [:id, :pounds, :ounces, :size] | |||
|
|||
def initialize(fields) | |||
fields.each { |name, value| send("#{name}=", value) } | |||
|
|||
yield self if block_given? | |||
|
|||
if fields[:size] == 'LARGE' | |||
[:container, :width, :length, :height].each do |field| | |||
error "#{field} is required when Size=LARGE" unless send(field) | |||
end | |||
end | |||
|
|||
self.class.required_properties.each do |field| | |||
error "#{field} is required" unless send(field) | |||
end | |||
end | |||
|
|||
protected | |||
|
|||
def self.required_properties | |||
@required + (defined?(super) ? super : []) | |||
end | |||
|
|||
def error(message) | |||
raise ArgumentError.new message | |||
end | |||
end | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
module USPS::Request::Package | |||
class DomesticPackage < Base | |||
attr_accessor :service | |||
attr_accessor :first_class_mail_type | |||
attr_accessor :origin_zip, :destination_zip | |||
attr_accessor :value | |||
attr_accessor :amount_to_collect | |||
|
|||
@required = [:service, :origin_zip, :destination_zip] | |||
|
|||
def initialize(fields = {}) | |||
if fields[:service] == 'FIRST CLASS' and !fields[:first_class_mail_type] | |||
error "first_class_mail_type is required when Service=FIRST Class" | |||
end | |||
super | |||
end | |||
end | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
module USPS::Request::Package | |||
class InternationalPackage < Base | |||
attr_accessor :country, :mail_type | |||
|
|||
@required = [:country, :mail_type] | |||
|
|||
def initialize(fields = {}) | |||
super | |||
end | |||
end | |||
end |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,9 +1,11 @@ | |||
module USPS::Response | module USPS::Response | ||
autoload :Base, 'usps/response/base' | autoload :Base, 'usps/response/base' | ||
autoload :CityAndStateLookup, 'usps/response/city_and_state_lookup' | autoload :CityAndStateLookup, 'usps/response/city_and_state_lookup' | ||
autoload :DeliveryConfirmation, 'usps/response/delivery_confirmation' | autoload :DeliveryConfirmation, 'usps/response/delivery_confirmation' | ||
autoload :AddressStandardization, 'usps/response/address_standardization' | autoload :AddressStandardization, 'usps/response/address_standardization' | ||
autoload :TrackingLookup, 'usps/response/tracking_lookup' | autoload :TrackingLookup, 'usps/response/tracking_lookup' | ||
autoload :TrackingFieldLookup, 'usps/response/tracking_field_lookup' | autoload :TrackingFieldLookup, 'usps/response/tracking_field_lookup' | ||
autoload :ShippingRatesLookup, 'usps/response/shipping_rates_lookup' | autoload :ShippingRatesLookup, 'usps/response/shipping_rates_lookup' | ||
autoload :InternationalShippingRatesLookup, 'usps/response/international_shipping_rates_lookup' | |||
autoload :Package, 'usps/response/package' | |||
end | end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
module USPS::Response | |||
class InternationalShippingRatesLookup < Base | |||
attr_reader :packages | |||
|
|||
def initialize(xml) | |||
@packages = [] | |||
xml.search('Package').each do |package_node| | |||
@packages << Package::InternationalPackage.new do |package| | |||
package.id = package_node.attr('ID') | |||
package.services = package_node.search('Service').map do |postage| | |||
parse_service(postage) | |||
end | |||
end | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def parse_service(node) | |||
Package::InternationalPackage::Service.new.tap do |service| | |||
service.id = node.attr('ID') | |||
service.description = node.search('SvcDescription').text | |||
service.rate = node.search('Postage').text | |||
end | |||
end | |||
end | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
module USPS::Response::Package | |||
autoload :DomesticPackage, 'usps/response/package/domestic_package' | |||
autoload :InternationalPackage, 'usps/response/package/international_package' | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
module USPS::Response::Package | |||
class DomesticPackage | |||
class Postage | |||
attr_accessor :rate, :mail_service, :class_id | |||
end | |||
|
|||
attr_accessor :postages | |||
attr_accessor :id, :origin_zip, :destination_zip, :pounds, :ounces, :container, :size | |||
|
|||
def initialize(properties = {}) | |||
properties.each_pair { |k, v| send("#{k}=", v) } | |||
yield self if block_given? | |||
end | |||
end | |||
end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
module USPS::Response::Package | |||
class InternationalPackage | |||
class Service | |||
attr_accessor :id, :description, :rate | |||
end | |||
|
|||
attr_accessor :id | |||
attr_accessor :origin_zip, :destination_zip | |||
attr_accessor :pounds, :ounces, :container, :size | |||
|
|||
attr_accessor :services | |||
|
|||
def initialize(properties = {}) | |||
properties.each_pair { |k, v| send("#{k}=", v) } | |||
yield self if block_given? | |||
end | |||
end | |||
end |
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
Oops, something went wrong.