Skip to content

Commit

Permalink
USPS hotfix for their API change that sends unescaped HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Soleone committed Jan 3, 2011
1 parent ffced9c commit cca7e73
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/active_shipping/shipping/carriers/usps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ def rates_from_response_node(response_node, packages)

package_node.each_element(service_node) do |service_response_node|
service_name = service_response_node.get_text(service_name_node).to_s


# workaround for USPS messing up and including unescaped html in their rate names since Jan 2, 2011
service_name.gsub!(/<sup>®<\/sup>/, '')

# aggregate specific package rates into a service-centric RateEstimate
# first package with a given service name will initialize these;
# later packages with same service will add to them
Expand Down
101 changes: 101 additions & 0 deletions test/fixtures/xml/usps/2011_rates_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0"?>
<RateV3Response>
<Package ID="0">
<ZipOrigination>83843</ZipOrigination>
<ZipDestination>70001</ZipDestination>
<Pounds>0</Pounds>
<Ounces>32.0</Ounces>
<Container/>
<Size>REGULAR</Size>
<Machinable>FALSE</Machinable>
<Zone>8</Zone>
<Postage CLASSID="3">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt;</MailService>
<Rate>34.70</Rate>
</Postage>
<Postage CLASSID="2">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Hold For Pickup</MailService>
<Rate>34.70</Rate>
</Postage>
<Postage CLASSID="23">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Sunday/Holiday Delivery</MailService>
<Rate>47.20</Rate>
</Postage>
<Postage CLASSID="13">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Flat Rate Envelope</MailService>
<Rate>18.30</Rate>
</Postage>
<Postage CLASSID="27">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Flat Rate Envelope Hold For Pickup</MailService>
<Rate>18.30</Rate>
</Postage>
<Postage CLASSID="25">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Sunday/Holiday Delivery Flat Rate Envelope</MailService>
<Rate>30.80</Rate>
</Postage>
<Postage CLASSID="30">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Legal Flat Rate Envelope</MailService>
<Rate>18.30</Rate>
</Postage>
<Postage CLASSID="31">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Legal Flat Rate Envelope Hold For Pickup</MailService>
<Rate>18.30</Rate>
</Postage>
<Postage CLASSID="32">
<MailService>Express Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Sunday/Holiday Delivery Legal Flat Rate Envelope</MailService>
<Rate>30.80</Rate>
</Postage>
<Postage CLASSID="1">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt;</MailService>
<Rate>10.20</Rate>
</Postage>
<Postage CLASSID="22">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Large Flat Rate Box</MailService>
<Rate>14.95</Rate>
</Postage>
<Postage CLASSID="17">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Medium Flat Rate Box</MailService>
<Rate>10.95</Rate>
</Postage>
<Postage CLASSID="28">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Small Flat Rate Box</MailService>
<Rate>5.20</Rate>
</Postage>
<Postage CLASSID="16">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="44">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Legal Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="29">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Padded Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="38">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Gift Card Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="42">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Small Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="40">
<MailService>Priority Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt; Window Flat Rate Envelope</MailService>
<Rate>4.95</Rate>
</Postage>
<Postage CLASSID="4">
<MailService>Parcel Post&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt;</MailService>
<Rate>8.09</Rate>
</Postage>
<Postage CLASSID="6">
<MailService>Media Mail&amp;lt;sup&amp;gt;&amp;amp;reg;&amp;lt;/sup&amp;gt;</MailService>
<Rate>2.77</Rate>
</Postage>
<Postage CLASSID="7">
<MailService>Library Mail</MailService>
<Rate>2.63</Rate>
</Postage>
</Package>
</RateV3Response>
36 changes: 36 additions & 0 deletions test/unit/carriers/usps_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,42 @@ def test_maximum_weight
assert Package.new((70 * 16) - 0.01, [5,5,5], :units => :imperial).mass < @carrier.maximum_weight
end

def test_updated_rate_name_format_with_unescaped_html
mock_response = xml_fixture('usps/2011_rates_response')
@carrier.expects(:commit).returns(mock_response)
rates_response = @carrier.find_rates(
@locations[:beverly_hills],
@locations[:ottawa],
@packages[:book],
:test => true
)
rate_names = [
'USPS Express Mail',
'USPS Express Mail Flat Rate Envelope',
'USPS Express Mail Flat Rate Envelope Hold For Pickup',
'USPS Express Mail Hold For Pickup',
'USPS Express Mail Legal Flat Rate Envelope',
'USPS Express Mail Legal Flat Rate Envelope Hold For Pickup',
'USPS Express Mail Sunday/Holiday Delivery',
'USPS Express Mail Sunday/Holiday Delivery Flat Rate Envelope',
'USPS Express Mail Sunday/Holiday Delivery Legal Flat Rate Envelope',
'USPS Library Mail',
'USPS Media Mail',
'USPS Parcel Post',
'USPS Priority Mail',
'USPS Priority Mail Flat Rate Envelope',
'USPS Priority Mail Gift Card Flat Rate Envelope',
'USPS Priority Mail Large Flat Rate Box',
'USPS Priority Mail Legal Flat Rate Envelope',
'USPS Priority Mail Medium Flat Rate Box',
'USPS Priority Mail Padded Flat Rate Envelope',
'USPS Priority Mail Small Flat Rate Box',
'USPS Priority Mail Small Flat Rate Envelope',
'USPS Priority Mail Window Flat Rate Envelope'
]
assert_equal rate_names, rates_response.rates.collect(&:service_name).sort
end

private

def build_service_node(options = {})
Expand Down

0 comments on commit cca7e73

Please sign in to comment.