diff --git a/lib/active_shipping/shipping/carriers/new_zealand_post.rb b/lib/active_shipping/shipping/carriers/new_zealand_post.rb index 3a3a9518c..cfbed72f0 100644 --- a/lib/active_shipping/shipping/carriers/new_zealand_post.rb +++ b/lib/active_shipping/shipping/carriers/new_zealand_post.rb @@ -4,8 +4,10 @@ module ActiveMerchant module Shipping class NewZealandPost < Carrier - class NewZealandPostRateResponse < RateResponse - end + # class NewZealandPostRateResponse < RateResponse + # end + + @@name = "NewZealandPost" URL = "http://workshop.nzpost.co.nz/api/v1/rate.xml" @@ -50,22 +52,35 @@ def build_rectangular_request_params(origin, destination, line_items = [], optio combine_line_items(line_items).merge(params) end - def combine_line_items(line_items) + def combine_line_items(line_items) { - :height => line_items.first.centimetres(:height).to_s, - :thickness => line_items.first.centimetres(:width).to_s, - :length => line_items.first.centimetres(:length).to_s, + :height => "#{line_items.first.centimetres(:height) * 10}", + :thickness => "#{line_items.first.centimetres(:width) * 10}", + :length => "#{line_items.first.centimetres(:length) * 10}", :weight =>"%.1f" % (line_items.first.weight.amount / 1000.0) } end def parse_rate_response(origin, destination, packages, response, options={}) - # xml = REXML::Document.new(response) - # if success = response_success?(xml) - # rate_estimates = [] - # xml.elements.each('hash/products-by-service') do || - # end - # end + xml = REXML::Document.new(response) + if response_success?(xml) + rate_estimates = [] + xml.elements.each('hash/products/product') do |prod| + rate_estimates << RateEstimate.new(origin, + destination, + @@name, + prod.get_text('service-group-description').to_s, + :total_price => prod.get_text('cost').to_s.to_f, + :currency => 'NZD', + :service_code => prod.get_text('service').to_s, + :packages => packages) + end + + RateResponse.new(true, "Success", Hash.from_xml(response), :rates => rate_estimates, :xml => response) + else + error_message = response_message(response_message) + RateResponse.new(false, error_message, Hash.from_xml(response), :rates => rate_estimates, :xml => response) + end end def response_success?(xml) diff --git a/test/fixtures/xml/newzealandpost/example_response.xml b/test/fixtures/xml/newzealandpost/example_response.xml index 6fb20f7e3..632d159e3 100644 --- a/test/fixtures/xml/newzealandpost/example_response.xml +++ b/test/fixtures/xml/newzealandpost/example_response.xml @@ -1,422 +1,242 @@ - - - success - false - - - - 4.20 - 1-3 Working days Moday to Friday - 0.4 - - 165*235*70 3000gms - 25 - PCB3C5 - 15 - postage_only - C5 70mm Thick - https://www.nzpost.co.nz/cart/checkout - parcel_post_tracking - 2 - - - 4.50 - 1-3 Working days Moday to Friday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 125*230*70 3000gms - 25 - PICFDLE - 15 - flat - DLE Flat Bag - - parcel_post_tracking - 2 - - - 5.50 - 1-3 Working days Moday to Friday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 3000gms - 25 - PICBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post_tracking - 2 - - - - - 4.00 - Next working day - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 3000gms - 25 - PIXFC5 - 15 - flat - C5 Flat Bag - - parcel_post_po_box_priority - 2 - - - - - 2.40 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKTDLE - 15 - postage_included_envelopes - DLE Postage Included Envelopes - - parcel_post - 2 - - - 2.40 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKPDLE - 15 - permitpost - DLE - - parcel_post - 2 - - - 4.00 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIKFC5 - 15 - flat - C5 Flat Bag - - parcel_post - 2 - - - 4.50 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIKBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post - 2 - - - - - 3.00 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKTDLE, PKTEF - 15 - postage_included_envelopes - DLE Postage Included Envelopes - - parcel_post_fast - 2 - - - 3.00 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKFDLE - 15 - permitpost - DLE - - parcel_post_fast - 2 - - - 4.60 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIFFC5 - 15 - flat - C5 Flat Bag - - parcel_post_fast - 2 - - - 5.10 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIFBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post_fast - 2 - - - - - 5.50 - Next working day - 0.4 - - 15000*15000*15000 25000gms - 25 - PCBXT - 15 - postage_only - Across town - https://www.nzpost.co.nz/cart/checkout - parcel_post_tracked_zonal - 2 - - - - - - 2.40 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKTDLE - 15 - postage_included_envelopes - DLE Postage Included Envelopes - - parcel_post - 2 - - - 3.00 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKTDLE, PKTEF - 15 - postage_included_envelopes - DLE Postage Included Envelopes - - parcel_post_fast - 2 - - - 2.40 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKPDLE - 15 - permitpost - DLE - - parcel_post - 2 - - - 3.00 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 130*235*70 1500gms - 25 - PKFDLE - 15 - permitpost - DLE - - parcel_post_fast - 2 - - - 4.00 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIKFC5 - 15 - flat - C5 Flat Bag - - parcel_post - 2 - - - 4.50 - 1-3 Working days Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIKBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post - 2 - - - 4.60 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIFFC5 - 15 - flat - C5 Flat Bag - - parcel_post_fast - 2 - - - 5.10 - Next working day Moday to Saturday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 1500gms - 25 - PIFBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post_fast - 2 - - - 4.20 - 1-3 Working days Moday to Friday - 0.4 - - 165*235*70 3000gms - 25 - PCB3C5 - 15 - postage_only - C5 70mm Thick - https://www.nzpost.co.nz/cart/checkout - parcel_post_tracking - 2 - - - 4.50 - 1-3 Working days Moday to Friday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 125*230*70 3000gms - 25 - PICFDLE - 15 - flat - DLE Flat Bag - - parcel_post_tracking - 2 - - - 5.50 - 1-3 Working days Moday to Friday - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 3000gms - 25 - PICBC5 - 15 - bubble - C5 Bubble Bag - - parcel_post_tracking - 2 - - - 5.50 - Next working day - 0.4 - - 15000*15000*15000 25000gms - 25 - PCBXT - 15 - postage_only - Across town - https://www.nzpost.co.nz/cart/checkout - parcel_post_tracked_zonal - 2 - - - 4.00 - Next working day - 0.4 - http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ - 185*270*70 3000gms - 25 - PIXFC5 - 15 - flat - C5 Flat Bag - - parcel_post_po_box_priority - 2 - - - across_town - - xml - 2 - 6012 - 0.4 - 123 - rate - 25 - 15 - api - 6011 - - \ No newline at end of file + + + across_town + success + false + + xml + 150 + 6012 + 0.4 + 123 + rate + 20 + 250 + api + 6011 + + + + 20 + parcel_post + parcel_post + 250 + PKTC4 + 4.20 + 0.4 + 150 + C4 Postage Included Envelopes + 1-3 Working days Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 260*325*70 1500gms + Parcel Post + postage_included_envelopes + + + + 20 + parcel_post_fast + parcel_post_fast + 250 + PKTC4, PKTE + 4.80 + 0.4 + 150 + C4 Postage Included Envelopes + Next working day Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 260*325*70 1500gms + Parcel Post Fast + postage_included_envelopes + + + + 20 + parcel_post + parcel_post + 250 + PKPC4 + 4.20 + 0.4 + 150 + C4 + 1-3 Working days Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 260*325*70 1500gms + Parcel Post + permitpost + + + + 20 + parcel_post_fast + parcel_post_fast + 250 + PKFC4 + 4.80 + 0.4 + 150 + C4 + Next working day Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 260*325*70 1500gms + Parcel Post Fast + permitpost + + + + 20 + parcel_post + parcel_post + 250 + PIKFC5 + 4.00 + 0.4 + 150 + C5 Flat Bag + 1-3 Working days Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 1500gms + Parcel Post + flat + + + + 20 + parcel_post + parcel_post + 250 + PIKBC5 + 4.50 + 0.4 + 150 + C5 Bubble Bag + 1-3 Working days Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 1500gms + Parcel Post + bubble + + + + 20 + parcel_post_fast + parcel_post_fast + 250 + PIFFC5 + 4.60 + 0.4 + 150 + C5 Flat Bag + Next working day Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 1500gms + Parcel Post Fast + flat + + + + 20 + parcel_post_fast + parcel_post_fast + 250 + PIFBC5 + 5.10 + 0.4 + 150 + C5 Bubble Bag + Next working day Moday to Saturday + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 1500gms + Parcel Post Fast + bubble + + + + 20 + parcel_post_tracked + parcel_post_tracked + 250 + PCB3C4 + 5.40 + 0.4 + 150 + C4 Postage Only + parcel_post_tracked + + 260*325*70 3000gms + Parcel Post Tracked + postage_only + https://www.nzpost.co.nz/cart/checkout + + + 20 + parcel_post_tracked + parcel_post_tracked + 250 + PICFC5 + 5.00 + 0.4 + 150 + C5 Flat Bag + parcel_post_tracked + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 3000gms + Parcel Post Tracked + flat + + + + 20 + parcel_post_tracked + parcel_post_tracked + 250 + PICBC5 + 5.50 + 0.4 + 150 + C5 Bubble Bag + parcel_post_tracked + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 3000gms + Parcel Post Tracked + bubble + + + + 20 + parcel_post_tracked_zonal + parcel_post_tracked_zonal + 250 + PCBXT + 5.50 + 0.4 + 150 + Across town + Next working day + + 15000*15000*15000 25000gms + Parcel Post Tracked Zonal + postage_only + https://www.nzpost.co.nz/cart/checkout + + + 20 + parcel_post_po_box_priority + parcel_post_po_box_priority + 250 + PIXFC5 + 4.00 + 0.4 + 150 + C5 Flat Bag + Next working day + http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ + 185*270*70 3000gms + Parcel Post Po Box Priority + flat + + + + + diff --git a/test/unit/carriers/new_zealand_post_test.rb b/test/unit/carriers/new_zealand_post_test.rb index f2b0a46cf..d56289e9b 100644 --- a/test/unit/carriers/new_zealand_post_test.rb +++ b/test/unit/carriers/new_zealand_post_test.rb @@ -22,9 +22,9 @@ def test_build_request_rectangular params = @carrier.send(:build_rectangular_request_params, @origin, @destination, @line_items) assert_equal '123', params[:api_key] - assert_equal '25', params[:length] - assert_equal '15', params[:thickness] - assert_equal '2', params[:height] + assert_equal '250', params[:length] + assert_equal '150', params[:thickness] + assert_equal '20', params[:height] assert_equal '0.4', params[:weight] assert_equal '6011', params[:postcode_src] assert_equal '6012', params[:postcode_dest] @@ -37,6 +37,21 @@ def test_build_request_multiple_rectangular end def test_parse_response + rate_response = @carrier.send(:parse_rate_response, @origin, @destination, @line_items, @response) + assert_not_nil rate_response + assert_equal 13, rate_response.rates.size + + # test first element + first_element = rate_response.rates.first + assert_equal 420, first_element.price + assert_equal 'parcel_post', first_element.service_code + assert_equal 'Parcel Post', first_element.service_name + + # test last element + last_element = rate_response.rates.last + assert_equal 400, last_element.price + assert_equal 'parcel_post_po_box_priority', last_element.service_code + assert_equal 'Parcel Post Po Box Priority', last_element.service_name end def test_response_success_with_successful_response