Skip to content

Commit

Permalink
Add dimensions and FirstClassMailType to Package
Browse files Browse the repository at this point in the history
When package Size=LARGE, Height, Width, Length are required.
When Service=FIRST CLASS, FirstClassMailType is required.

[Delivers #32378857]
Closes #2

Squashed commit of the following:

commit 40353ce
Author: Nick Yianilos <npy@reenhanced.com>
Date:   Mon Jul 16 12:38:28 2012 -0400

    added test for no dimension attributes, refactored tests

commit 7eafae5
Author: Nick Yianilos <npy@reenhanced.com>
Date:   Mon Jul 16 11:54:27 2012 -0400

    updated test to cover dimension attributes

commit 14640f4
Author: Nick Yianilos <npy@reenhanced.com>
Date:   Mon Jul 16 10:01:54 2012 -0400

    removed output statement

commit 2b7272a
Author: Nick Yianilos <npy@reenhanced.com>
Date:   Thu Jul 12 14:24:36 2012 -0400

    added dimensions and FirstClassMailType to Package
  • Loading branch information
Nick Yianilos committed Jul 16, 2012
1 parent 0a5d016 commit 95bdba0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/usps/package.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module USPS

class Package < Struct.new(:id, :service, :origin_zip, :destination_zip, :pounds, :ounces, :container, :size,
:width, :length, :height, :girth, :value, :amount_to_collect)
:width, :length, :height, :girth, :value, :amount_to_collect, :first_class_mail_type)

@@required_properties = %w{id service origin_zip destination_zip pounds ounces container size}

Expand Down
6 changes: 6 additions & 0 deletions lib/usps/request/shipping_rates_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ def build
@packages.each do |package|
xml.Package :ID => package.id do
xml.Service package.service
xml.FirstClassMailType(package.first_class_mail_type) if package.first_class_mail_type
xml.ZipOrigination package.origin_zip
xml.ZipDestination package.destination_zip
xml.Pounds package.pounds
xml.Ounces package.ounces
xml.Container package.container
xml.Size package.size
if package.size == 'LARGE'
xml.Width package.width
xml.Length package.length
xml.Height package.height
end
xml.Machinable 'true' # for Service=ALL
end
end
Expand Down
69 changes: 47 additions & 22 deletions spec/request/shipping_rates_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,56 @@
}.to raise_exception(ArgumentError)
end

let(:required_properties) do
{
:id => "42",
:service => "ALL",
:origin_zip => "20171",
:destination_zip => "08540",
:pounds => 5,
:ounces => 4,
:container => 'VARIABLE',
:size => 'LARGE',
}
end

def package_xml_from_attributes(attributes)
package = USPS::Package.new(attributes)
request = USPS::Request::ShippingRatesLookup.new(package)
Nokogiri::XML.parse(request.build)
end

it "builds a valid XML request for USPS RateV4" do
package = USPS::Package.new do |new_package|
new_package.id = "42"
new_package.service = "ALL"
new_package.origin_zip = "20171"
new_package.destination_zip = "08540"
new_package.pounds = 5
new_package.ounces = 4
new_package.container = 'VARIABLE'
new_package.size = 'LARGE'
end
package_properties = required_properties.merge(
:first_class_mail_type => 'PARCEL',
:height => 13,
:width => 10,
:length => 15
)
package_xml = package_xml_from_attributes(package_properties)

request = USPS::Request::ShippingRatesLookup.new(package).build
xml = Nokogiri::XML.parse(request)
package_xml.search('Package').count.should == 1
first_package = package_xml.search('Package').first
first_package.attr('ID').should == "42"
first_package.search('Service').text.should == 'ALL'
first_package.search('FirstClassMailType').text.should == 'PARCEL'
first_package.search('ZipOrigination').text.should == '20171'
first_package.search('ZipDestination').text.should == '08540'
first_package.search('Pounds').text.should == '5'
first_package.search('Ounces').text.should == '4'
first_package.search('Container').text.should == 'VARIABLE'
first_package.search('Size').text.should == 'LARGE'
first_package.search('Height').text.should == '13'
first_package.search('Width').text.should == '10'
first_package.search('Length').text.should == '15'
end

xml.search('Package').count.should == 1
it "doesn't serialize dimension properties when package size is REGULAR" do
package_xml = package_xml_from_attributes(required_properties.merge :size => 'REGULAR')
first_package = package_xml.search('Package').first

first_package = xml.search('Package').first
first_package.attr('ID').should == "42"
first_package.search('Service').text.should == 'ALL'
first_package.search('ZipOrigination').text.should == '20171'
first_package.search('ZipDestination').text.should == '08540'
first_package.search('Pounds').text.should == '5'
first_package.search('Ounces').text.should == '4'
first_package.search('Container').text.should == 'VARIABLE'
first_package.search('Size').text.should == 'LARGE'
%w{Height Width Length}.each do |dimension|
first_package.search(dimension).should be_empty
end
end
end

0 comments on commit 95bdba0

Please sign in to comment.