Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

NZ Post: Add International API and switch to JSON

Add support for the New Zealand Post International Rate Finder JSON API:
http://api.nzpost.co.nz/ratefinder/international/documentation

Update the existing New Zealand Domestic Rate Finder API integration:
 * Switch from the XML to the JSON API
 * Support returning multiples rates for packages
 * Support all packaging types returned by the API
  • Loading branch information...
commit 07bc3faf54aff739c42418d77c2e68cc401bf304 1 parent 43082ce
@isaac isaac authored
View
1  active_shipping.gemspec
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_dependency('i18n')
s.add_dependency('active_utils', '>= 1.0.1')
s.add_dependency('builder')
+ s.add_dependency('json', '>= 1.5.1') if RUBY_VERSION =~ /^1\.8\./
s.add_development_dependency('rake')
s.add_development_dependency('mocha')
View
328 lib/active_shipping/shipping/carriers/new_zealand_post.rb
@@ -2,138 +2,268 @@ module ActiveMerchant
module Shipping
class NewZealandPost < Carrier
- # class NewZealandPostRateResponse < RateResponse
- # end
-
cattr_reader :name
@@name = "New Zealand Post"
- URL = "http://workshop.nzpost.co.nz/api/v1/rate.xml"
+ URL = "http://api.nzpost.co.nz/ratefinder"
- # Override to return required keys in options hash for initialize method.
def requirements
[:key]
end
- # Override with whatever you need to get the rates
def find_rates(origin, destination, packages, options = {})
- packages = Array(packages)
- rate_responses = []
- packages.each do |package|
- if package.tube?
- request_hash = build_tube_request_params(origin, destination, package, options)
- else
- request_hash = build_rectangular_request_params(origin, destination, package, options)
- end
- url = URL + '?' + request_hash.to_param
- response = ssl_get(url)
- rate_responses << parse_rate_response(origin, destination, package, response, options)
- end
- combine_rate_responses(rate_responses, packages)
- end
-
- def maximum_weight
- Mass.new(20, :kilograms)
+ options = @options.merge(options)
+ request = RateRequest.from(origin, destination, packages, options)
+ request.raw_responses = commit(request.urls) if request.new_zealand_origin?
+ request.rate_response
end
protected
- # Override in subclasses for non-U.S.-based carriers.
- def self.default_location
- Location.new(:postal_code => '6011')
+ def commit(urls)
+ save_request(urls).map { |url| ssl_get(url) }
end
- private
-
- def build_rectangular_request_params(origin, destination, package, options = {})
- params = {
- :postcode_src => origin.postal_code,
- :postcode_dest => destination.postal_code,
- :api_key => @options[:key],
- :height => "#{package.centimetres(:height) * 10}",
- :thickness => "#{package.centimetres(:width) * 10}",
- :length => "#{package.centimetres(:length) * 10}",
- :weight =>"%.1f" % (package.weight.amount / 1000.0)
- }
+ def self.default_location
+ Location.new({
+ :country => "NZ",
+ :city => "Wellington",
+ :address1 => "22 Waterloo Quay",
+ :address2 => "Pipitea",
+ :postal_code => "6011"
+ })
end
- def build_tube_request_params(origin, destination, package, options = {})
- params = {
- :postcode_src => origin.postal_code,
- :postcode_dest => destination.postal_code,
- :api_key => @options[:key],
- :diameter => "#{package.centimetres(:width) * 10}",
- :length => "#{package.centimetres(:length) * 10}",
- :weight => "%.1f" % (package.weight.amount / 1000.0)
- }
+ class NewZealandPostRateResponse < RateResponse
+
+ attr_reader :raw_responses
+
+ def initialize(success, message, params = {}, options = {})
+ @raw_responses = options[:raw_responses]
+ super
+ end
end
- def parse_rate_response(origin, destination, package, response, options={})
- xml = REXML::Document.new(response)
- if response_success?(xml)
- rate_estimates = []
- xml.elements.each('hash/products/product') do |prod|
- if( prod.get_text('packaging') == 'postage_only' )
- 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('code').to_s,
- :package => package)
- end
+ class RateRequest
+
+ attr_reader :urls
+ attr_writer :raw_responses
+
+ def self.from(*args)
+ return International.new(*args) unless domestic?(args[0..1])
+ Domestic.new(*args)
+ end
+
+ def initialize(origin, destination, packages, options)
+ @origin = Location.from(origin)
+ @destination = Location.from(destination)
+ @packages = Array(packages).map { |package| NewZealandPostPackage.new(package, api) }
+ @params = { :format => "json", :api_key => options[:key] }
+ @test = options[:test]
+ @rates = @responses = @raw_responses = []
+ @urls = @packages.map { |package| url(package) }
+ end
+
+ def rate_response
+ @rates = rates
+ NewZealandPostRateResponse.new(true, "success", response_params, response_options)
+ rescue => error
+ NewZealandPostRateResponse.new(false, error.message, response_params, response_options)
+ end
+
+ def new_zealand_origin?
+ self.class.new_zealand?(@origin)
+ end
+
+ protected
+
+ def self.new_zealand?(location)
+ [ 'NZ', nil ].include?(Location.from(location).country_code)
+ end
+
+ def self.domestic?(locations)
+ locations.select { |location| new_zealand?(location) }.size == 2
+ end
+
+ def response_options
+ {
+ :rates => @rates,
+ :raw_responses => @raw_responses,
+ :request => @urls,
+ :test => @test
+ }
+ end
+
+ def response_params
+ { :responses => @responses }
+ end
+
+ def rate_options(products)
+ {
+ :total_price => products.sum { |product| price(product) },
+ :currency => "NZD",
+ :service_code => products.first["code"]
+ }
+ end
+
+ def rates
+ rates_hash.map do |service, products|
+ RateEstimate.new(@origin, @destination, NewZealandPost.name, service, rate_options(products))
end
-
- RateResponse.new(true, "Success", Hash.from_xml(response), :rates => rate_estimates, :xml => response)
- else
- error_message = response_message(xml)
- RateResponse.new(false, error_message, Hash.from_xml(response), :rates => rate_estimates, :xml => response)
end
- end
- def combine_rate_responses(rate_responses, packages)
- #if there are any failed responses, return on that response
- rate_responses.each do |r|
- return r if !r.success?
+ def rates_hash
+ products_hash.select { |service, products| products.size == @packages.size }
end
- #group rate estimates by delivery type so that we can exclude any incomplete delviery types
- rate_estimate_delivery_types = {}
- rate_responses.each do |rr|
- rr.rate_estimates.each do |re|
- (rate_estimate_delivery_types[re.service_code] ||= []) << re
+ def products_hash
+ product_arrays.flatten.group_by { |product| service_name(product) }
+ end
+
+ def product_arrays
+ responses.map do |response|
+ raise(response["message"]) unless response["status"] == "success"
+ response["products"]
end
end
- rate_estimate_delivery_types.delete_if{ |type, re| re.size != packages.size }
-
- #combine cost estimates for remaining packages
- combined_rate_estimates = []
- rate_estimate_delivery_types.each do |type, re|
- total_price = re.sum(&:total_price)
- r = re.first
- combined_rate_estimates << RateEstimate.new(r.origin, r.destination, r.carrier,
- r.service_name,
- :total_price => total_price,
- :currency => r.currency,
- :service_code => r.service_code,
- :packages => packages)
- end
- RateResponse.new(true, "Success", {}, :rates => combined_rate_estimates)
+
+ def responses
+ @responses = @raw_responses.map { |response| parse_response(response) }
+ end
+
+ def parse_response(response)
+ JSON.parse(response)
+ end
+
+ def url(package)
+ "#{URL}/#{api}?#{params(package).to_query}"
+ end
+
+ def params(package)
+ @params.merge(api_params).merge(package.params)
+ end
+
end
- def response_success?(xml)
- xml.get_text('hash/status').to_s == 'success'
+ class Domestic < RateRequest
+ def service_name(product)
+ [ product["service_group_description"], product["description"] ].join(" ")
+ end
+
+ def api
+ :domestic
+ end
+
+ def api_params
+ {
+ :postcode_src => @origin.postal_code,
+ :postcode_dest => @destination.postal_code,
+ :carrier => "all"
+ }
+ end
+
+ def price(product)
+ product["cost"].to_f
+ end
end
- def response_message(xml)
- if response_success?(xml)
- 'Success'
- else
- xml.get_text('hash/message').to_s
+ class International < RateRequest
+
+ def rates
+ raise "New Zealand Post packages must originate in New Zealand" unless new_zealand_origin?
+ super
+ end
+
+ def service_name(product)
+ [ product["group"], product["name"] ].join(" ")
+ end
+
+ def api
+ :international
+ end
+
+ def api_params
+ { :country_code => @destination.country_code }
+ end
+
+ def price(product)
+ product["price"].to_f
end
end
+ class NewZealandPostPackage
+
+ def initialize(package, api)
+ @package = package
+ @api = api
+ @params = { :weight => weight, :length => length }
+ end
+
+ def params
+ @params.merge(api_params).merge(shape_params)
+ end
+
+ protected
+
+ def weight
+ @package.kg
+ end
+
+ def length
+ mm(:length)
+ end
+
+ def height
+ mm(:height)
+ end
+
+ def width
+ mm(:width)
+ end
+
+ def shape
+ return :cylinder if @package.cylinder?
+ :cuboid
+ end
+
+ def api_params
+ send("#{@api}_params")
+ end
+
+ def international_params
+ { :value => value }
+ end
+
+ def domestic_params
+ {}
+ end
+
+ def shape_params
+ send("#{shape}_params")
+ end
+
+ def cuboid_params
+ { :height => height, :thickness => width }
+ end
+
+ def cylinder_params
+ { :diameter => width }
+ end
+
+ def mm(measurement)
+ @package.cm(measurement) * 10
+ end
+
+ def value
+ return 0 unless @package.value && currency == "NZD"
+ @package.value / 100
+ end
+
+ def currency
+ @package.currency || "NZD"
+ end
+
+ end
end
end
end
View
2  test/fixtures.yml
@@ -14,4 +14,4 @@ shipwire:
canada_post:
login: CPC_DEMO_XML
new_zealand_post:
- api_key: '123'
+ key: '4d9dc0f0-dda0-012e-066f-000c29b44ac0'
View
1  test/fixtures/json/newzealandpost/domestic_book.json
@@ -0,0 +1 @@
+{"courier_defaulting_to_nationwide":false,"post_defaulting_to_nationwide":false,"post_large_delivery_type":"within_island","info":{"lat_long_src":"-41.2829189300537,174.7841796875","dimensions":"Weight 0.25kg; Volumetric Weight 0.1064kg; 190.0x140.0x20.0","resolved_postcode_dest":"1010","resolved_postcode_src":"6011","resolved_dest_txt":"1010","lat_long_dest":"-36.8051338195801,174.981109619141","resolved_source_txt":"6011"},"products":[{"size":"C5","cost":"3.00","code":"PKTC5","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"165*235*70 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=5-c5-PKTC5","service_code":"PKPC5","height":165,"service_group_description":"ParcelPost","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"C5 Postage Only","width":70},{"size":"C5","cost":"3.60","code":"PKTC5,PKTEF","image_url":null,"product_url":null,"delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"165*235*70 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=6-c5-PKTC5,PKTEF","service_code":"PKFC5","height":165,"service_group_description":"ParcelPost Fast","packaging":"postage_only","speed_description":"Next working day","length":235,"description":"C5 Postage Only","width":70},{"size":"C5","cost":"4.00","code":"PIKFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2086/pikfc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/13-c5-PIKFC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"flat","speed_description":"1 - 3 working days","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.20","code":"PCB3C5","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"165*235*70 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=21-c5-PCB3C5","service_code":"PCM3C5","height":165,"service_group_description":"ParcelPost Tracked","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"C5 Postage Only - Tracked","width":70},{"size":"C5","cost":"4.50","code":"PIFFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2090/piffc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/15-c5-PIFFC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"flat","speed_description":"Next working day","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.50","code":"PIKBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2088/pikbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/14-c5-PIKBC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.00","code":"PICFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2095/picfc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/26-c5-PICFC5","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"185*270* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Tracked","packaging":"flat","speed_description":"1 - 3 working days","length":270,"description":"C5 Flat Bag - Tracked","width":null},{"size":"C5","cost":"5.00","code":"PIFBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2092/pifbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/16-c5-PIFBC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"bubble","speed_description":"Next working day","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.50","code":"PICBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2099/picbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/27-c5-PICBC5","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"185*270* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Tracked","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag - Tracked","width":null},{"size":"C5","cost":"6.55","code":"NZPROA5","image_url":null,"product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/44-c5-NZPROA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Flat Bag","width":null},{"size":"C5","cost":"7.15","code":"NZPRBA5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2040/nzprba5.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/46-c5-NZPRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag","width":null},{"size":"C5","cost":"8.80","code":"NZSROA5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2039/nzproa5.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/45-c5-NZSROA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Flat Bag S/R","width":null},{"size":"C5","cost":"8.90","code":"NZSRBA5","image_url":null,"product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/47-c5-NZSRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag S/R","width":null}],"courier_large_delivery_type":null,"status":"success"}
View
1  test/fixtures/json/newzealandpost/domestic_default.json
@@ -0,0 +1 @@
+{"courier_defaulting_to_nationwide":false,"post_defaulting_to_nationwide":false,"post_large_delivery_type":"within_island","info":{"lat_long_src":"-41.2829189300537,174.7841796875","dimensions":"Weight 0.0kg; Volumetric Weight 0.0kg; 0.0x0.0x0.0","resolved_postcode_dest":"1010","resolved_postcode_src":"6011","resolved_dest_txt":"1010","lat_long_dest":"-36.8051338195801,174.981109619141","resolved_source_txt":"6011"},"products":[{"size":"DLE","cost":"2.40","code":"PKTDLE","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"130*235*70 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=1-dle-PKTDLE","service_code":"PKPDLE","height":130,"service_group_description":"ParcelPost","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"DLE Postage Only","width":70},{"size":"DLE","cost":"3.00","code":"PKTDLE,PKTEF","image_url":null,"product_url":null,"delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"130*235*70 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=2-dle-PKTDLE,PKTEF","service_code":"PKFDLE","height":130,"service_group_description":"ParcelPost Fast","packaging":"postage_only","speed_description":"Next working day","length":235,"description":"DLE Postage Only","width":70},{"size":"C5","cost":"4.00","code":"PIKFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2086/pikfc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/13-c5-PIKFC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"flat","speed_description":"1 - 3 working days","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.20","code":"PCB3C5","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"165*235*70 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=21-c5-PCB3C5","service_code":"PCM3C5","height":165,"service_group_description":"ParcelPost Tracked","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"C5 Postage Only - Tracked","width":70},{"size":"DLE","cost":"4.50","code":"PICFDLE","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2094/picfdle_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/25-dle-PICFDLE","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"125*230* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":125,"service_group_description":"ParcelPost Tracked","packaging":"flat","speed_description":"1 - 3 working days","length":230,"description":"DLE Flat Bag - Tracked","width":null},{"size":"C5","cost":"4.50","code":"PIFFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2090/piffc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/15-c5-PIFFC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"flat","speed_description":"Next working day","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.50","code":"PIKBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2088/pikbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/14-c5-PIKBC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.00","code":"PIFBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2092/pifbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/16-c5-PIFBC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"bubble","speed_description":"Next working day","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.50","code":"PICBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2099/picbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/27-c5-PICBC5","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"185*270* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Tracked","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag - Tracked","width":null},{"size":"DLE","cost":"5.89","code":"NZPRODL","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2037/nzprodl.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/42-dle-NZPRODL","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"130*240* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":130,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":240,"description":"DLE Ready To Go Courier Flat Bag","width":null},{"size":"C5","cost":"7.15","code":"NZPRBA5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2040/nzprba5.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/46-c5-NZPRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag","width":null},{"size":"DLE","cost":"8.30","code":"NZSRODL","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2038/nzprodl.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/43-dle-NZSRODL","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"130*240* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":130,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":240,"description":"DLE Ready To Go Courier Flat Bag S/R","width":null},{"size":"C5","cost":"8.90","code":"NZSRBA5","image_url":null,"product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/47-c5-NZSRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag S/R","width":null}],"courier_large_delivery_type":null,"status":"success"}
View
1  test/fixtures/json/newzealandpost/domestic_error.json
@@ -0,0 +1 @@
+{"errors":["Weight can only be between 0 and 25kg"],"message":"Weight can only be between 0 and 25kg","status":"failure"}
View
1  test/fixtures/json/newzealandpost/domestic_poster.json
@@ -0,0 +1 @@
+{"courier_defaulting_to_nationwide":false,"post_defaulting_to_nationwide":false,"post_large_delivery_type":"within_island","info":{"lat_long_src":"-41.2829189300537,174.7841796875","dimensions":"Weight 0.1kg; Volumetric Weight 0.0kg; 930.0x0.0x0.0","resolved_postcode_dest":"1010","resolved_postcode_src":"6011","resolved_dest_txt":"1010","lat_long_dest":"-36.8051338195801,174.981109619141","resolved_source_txt":"6011"},"products":[{"size":null,"cost":"7.50","code":"PCBWI","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked_zonal","weight":25.0,"product_max_dimensions":"15000*15000*15000 25000gms","priority":"parcel_post_tracked_zonal","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=37--PCBWI","service_code":"PCMW5","height":15000,"service_group_description":"ParcelPost Tracked Zonal","packaging":"postage_only","speed_description":"1 - 2 working days","length":15000,"description":"Postage Only - Zonal - Within Island - Tracked","width":15000}],"courier_large_delivery_type":null,"status":"success"}
View
1  test/fixtures/json/newzealandpost/domestic_small_half_pound.json
@@ -0,0 +1 @@
+{"courier_defaulting_to_nationwide":false,"post_defaulting_to_nationwide":false,"post_large_delivery_type":"within_island","info":{"lat_long_src":"-41.2829189300537,174.7841796875","dimensions":"Weight 0.226796185kg; Volumetric Weight 0.0032774128kg; 25.4x25.4x25.4","resolved_postcode_dest":"1010","resolved_postcode_src":"6011","resolved_dest_txt":"1010","lat_long_dest":"-36.8051338195801,174.981109619141","resolved_source_txt":"6011"},"products":[{"size":"DLE","cost":"2.40","code":"PKTDLE","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"130*235*70 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=1-dle-PKTDLE","service_code":"PKPDLE","height":130,"service_group_description":"ParcelPost","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"DLE Postage Only","width":70},{"size":"DLE","cost":"3.00","code":"PKTDLE,PKTEF","image_url":null,"product_url":null,"delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"130*235*70 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=2-dle-PKTDLE,PKTEF","service_code":"PKFDLE","height":130,"service_group_description":"ParcelPost Fast","packaging":"postage_only","speed_description":"Next working day","length":235,"description":"DLE Postage Only","width":70},{"size":"C5","cost":"4.00","code":"PIKFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2086/pikfc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/13-c5-PIKFC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"flat","speed_description":"1 - 3 working days","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.20","code":"PCB3C5","image_url":null,"product_url":null,"delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"165*235*70 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":"https://www.nzpost.co.nz/cart/checkout?code=21-c5-PCB3C5","service_code":"PCM3C5","height":165,"service_group_description":"ParcelPost Tracked","packaging":"postage_only","speed_description":"1 - 3 working days","length":235,"description":"C5 Postage Only - Tracked","width":70},{"size":"DLE","cost":"4.50","code":"PICFDLE","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2094/picfdle_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/25-dle-PICFDLE","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"125*230* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":125,"service_group_description":"ParcelPost Tracked","packaging":"flat","speed_description":"1 - 3 working days","length":230,"description":"DLE Flat Bag - Tracked","width":null},{"size":"C5","cost":"4.50","code":"PIFFC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2090/piffc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/15-c5-PIFFC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"flat","speed_description":"Next working day","length":270,"description":"C5 Flat Bag","width":null},{"size":"C5","cost":"4.50","code":"PIKBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2088/pikbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/14-c5-PIKBC5","delivery_rank":1.5,"tracked":false,"service":"parcel_post","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.00","code":"PIFBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2092/pifbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/16-c5-PIFBC5","delivery_rank":1,"tracked":false,"service":"parcel_post_fast","weight":1.5,"product_max_dimensions":"185*270* 1500gms","priority":"parcel_post_fast","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Fast","packaging":"bubble","speed_description":"Next working day","length":270,"description":"C5 Bubble Bag","width":null},{"size":"C5","cost":"5.50","code":"PICBC5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2099/picbc5_0.gif","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/27-c5-PICBC5","delivery_rank":1.5,"tracked":true,"service":"parcel_post_tracked","weight":3.0,"product_max_dimensions":"185*270* 3000gms","priority":"parcel_post_tracked","signature":false,"carrier":"nzpost","label_url":null,"service_code":null,"height":185,"service_group_description":"ParcelPost Tracked","packaging":"bubble","speed_description":"1 - 3 working days","length":270,"description":"C5 Bubble Bag - Tracked","width":null},{"size":"DLE","cost":"5.89","code":"NZPRODL","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2037/nzprodl.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/42-dle-NZPRODL","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"130*240* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":130,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":240,"description":"DLE Ready To Go Courier Flat Bag","width":null},{"size":"C5","cost":"7.15","code":"NZPRBA5","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2040/nzprba5.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/46-c5-NZPRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":false,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag","width":null},{"size":"DLE","cost":"8.30","code":"NZSRODL","image_url":"http://www.nzpost.co.nz/sites/default/files/imagecache/product_full/product/2038/nzprodl.jpg","product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/43-dle-NZSRODL","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"130*240* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":130,"service_group_description":"Courier","packaging":"flat","speed_description":"Next working day","length":240,"description":"DLE Ready To Go Courier Flat Bag S/R","width":null},{"size":"C5","cost":"8.90","code":"NZSRBA5","image_url":null,"product_url":"http://www.nzpost.co.nz/products-services/postage-packaging/47-c5-NZSRBA5","delivery_rank":1,"tracked":true,"service":"courier","weight":3.0,"product_max_dimensions":"185*280* 3000gms","priority":"courier","signature":true,"carrier":"courierpost","label_url":null,"service_code":null,"height":185,"service_group_description":"Courier","packaging":"bubble","speed_description":"Next working day","length":280,"description":"C5 Ready To Go Courier Bubble Bag S/R","width":null}],"courier_large_delivery_type":null,"status":"success"}
View
1  test/fixtures/json/newzealandpost/international_book.json
@@ -0,0 +1 @@
+{"products":[{"group":"International Express Courier","price":"65.25","name":"Int Express Pcl Zone C 500gm","code":"ICPNC500","max_delivery_target":5,"signature_required":true,"min_delivery_target":1,"has_tracking":true},{"group":"International Economy Courier","price":"42.5","name":"Int Econ Cour Pcl Zn C 500gm","code":"IEZPC500","max_delivery_target":6,"signature_required":true,"min_delivery_target":2,"has_tracking":true},{"group":"International Air","price":"12.5","name":"Zone C AirPost Cust Pcl 500gm","code":"IACNC500","max_delivery_target":10,"signature_required":false,"min_delivery_target":3,"has_tracking":false},{"group":"International Economy","price":"11.25","name":"Zone C EconomyPost Pcl 500gm","code":"IECNC500","max_delivery_target":25,"signature_required":false,"min_delivery_target":10,"has_tracking":false}],"status":"success"}
View
1  test/fixtures/json/newzealandpost/international_new_zealand_wii.json
@@ -0,0 +1 @@
+{"products":[{"group":"International Express Courier","price":"130.73","name":"Int Express Pcl Zone C 3.5kg","code":"ICPNC35","max_delivery_target":5,"signature_required":true,"min_delivery_target":1,"has_tracking":true}],"status":"success"}
View
1  test/fixtures/json/newzealandpost/international_small_half_pound.json
@@ -0,0 +1 @@
+{"products":[{"group":"International Express Courier","price":"65.25","name":"Int Express Pcl Zone C 500gm","code":"ICPNC500","max_delivery_target":5,"signature_required":true,"min_delivery_target":1,"has_tracking":true},{"group":"International Economy Courier","price":"42.5","name":"Int Econ Cour Pcl Zn C 500gm","code":"IEZPC500","max_delivery_target":6,"signature_required":true,"min_delivery_target":2,"has_tracking":true},{"group":"International Air","price":"12.1","name":"Zone C AirPost Cust Pcl 500gm","code":"IACNC500","max_delivery_target":10,"signature_required":false,"min_delivery_target":3,"has_tracking":false},{"group":"International Economy","price":"10.89","name":"Zone C EconomyPost Pcl 500gm","code":"IECNC500","max_delivery_target":25,"signature_required":false,"min_delivery_target":10,"has_tracking":false}],"status":"success"}
View
1  test/fixtures/json/newzealandpost/international_wii.json
@@ -0,0 +1 @@
+{"products":[{"group":"International Express Courier","price":"130.73","name":"Int Express Pcl Zone C 3.5kg","code":"ICPNC35","max_delivery_target":5,"signature_required":true,"min_delivery_target":1,"has_tracking":true},{"group":"International Economy Courier","price":"86.15","name":"Int Econ Cour Pcl Zn C 3.5kg","code":"IEZPC35","max_delivery_target":6,"signature_required":true,"min_delivery_target":2,"has_tracking":true},{"group":"International Air","price":"68.65","name":"Zone C AirPost Cust Pcl 3.5kg","code":"IACNC35","max_delivery_target":10,"signature_required":false,"min_delivery_target":3,"has_tracking":false},{"group":"International Economy","price":"61.79","name":"Zone C EconomyPost Pcl 3.5kg","code":"IECNC35","max_delivery_target":25,"signature_required":false,"min_delivery_target":10,"has_tracking":false}],"status":"success"}
View
242 test/fixtures/xml/newzealandpost/example_response.xml
@@ -1,242 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<hash>
- <large-delivery-type type="symbol">across_town</large-delivery-type>
- <status>success</status>
- <defaulting-to-nationwide type="boolean">false</defaulting-to-nationwide>
- <params>
- <format>xml</format>
- <thickness>150</thickness>
- <postcode-dest>6012</postcode-dest>
- <weight>0.4</weight>
- <api-key>123</api-key>
- <action>rate</action>
- <height>20</height>
- <length>250</length>
- <controller>api</controller>
- <postcode-src>6011</postcode-src>
- </params>
- <products type="array">
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post</priority>
- <service>parcel_post</service>
- <length type="integer">250</length>
- <code>PKTC4</code>
- <cost>4.20</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C4 Postage Included Envelopes</description>
- <speed-description>1-3 Working days Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>260*325*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post</service-group-description>
- <packaging>postage_included_envelopes</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_fast</priority>
- <service>parcel_post_fast</service>
- <length type="integer">250</length>
- <code>PKTC4, PKTE</code>
- <cost>4.80</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C4 Postage Included Envelopes</description>
- <speed-description>Next working day Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>260*325*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post Fast</service-group-description>
- <packaging>postage_included_envelopes</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post</priority>
- <service>parcel_post</service>
- <length type="integer">250</length>
- <code>PKPC4</code>
- <cost>4.20</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C4 </description>
- <speed-description>1-3 Working days Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>260*325*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post</service-group-description>
- <packaging>permitpost</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_fast</priority>
- <service>parcel_post_fast</service>
- <length type="integer">250</length>
- <code>PKFC4</code>
- <cost>4.80</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C4 </description>
- <speed-description>Next working day Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>260*325*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post Fast</service-group-description>
- <packaging>permitpost</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post</priority>
- <service>parcel_post</service>
- <length type="integer">250</length>
- <code>PIKFC5</code>
- <cost>4.00</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Flat Bag</description>
- <speed-description>1-3 Working days Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post</service-group-description>
- <packaging>flat</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post</priority>
- <service>parcel_post</service>
- <length type="integer">250</length>
- <code>PIKBC5</code>
- <cost>4.50</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Bubble Bag</description>
- <speed-description>1-3 Working days Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post</service-group-description>
- <packaging>bubble</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_fast</priority>
- <service>parcel_post_fast</service>
- <length type="integer">250</length>
- <code>PIFFC5</code>
- <cost>4.60</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Flat Bag</description>
- <speed-description>Next working day Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post Fast</service-group-description>
- <packaging>flat</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_fast</priority>
- <service>parcel_post_fast</service>
- <length type="integer">250</length>
- <code>PIFBC5</code>
- <cost>5.10</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Bubble Bag</description>
- <speed-description>Next working day Moday to Saturday</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 1500gms</product-max-dimensions>
- <service-group-description>Parcel Post Fast</service-group-description>
- <packaging>bubble</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_tracked</priority>
- <service>parcel_post_tracked</service>
- <length type="integer">250</length>
- <code>PCB3C4</code>
- <cost>5.40</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C4 Postage Only</description>
- <speed-description>parcel_post_tracked</speed-description>
- <product-url nil="true"></product-url>
- <product-max-dimensions>260*325*70 3000gms</product-max-dimensions>
- <service-group-description>Parcel Post Tracked</service-group-description>
- <packaging>postage_only</packaging>
- <label-url>https://www.nzpost.co.nz/cart/checkout</label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_tracked</priority>
- <service>parcel_post_tracked</service>
- <length type="integer">250</length>
- <code>PICFC5</code>
- <cost>5.00</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Flat Bag</description>
- <speed-description>parcel_post_tracked</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 3000gms</product-max-dimensions>
- <service-group-description>Parcel Post Tracked</service-group-description>
- <packaging>flat</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_tracked</priority>
- <service>parcel_post_tracked</service>
- <length type="integer">250</length>
- <code>PICBC5</code>
- <cost>5.50</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Bubble Bag</description>
- <speed-description>parcel_post_tracked</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 3000gms</product-max-dimensions>
- <service-group-description>Parcel Post Tracked</service-group-description>
- <packaging>bubble</packaging>
- <label-url nil="true"></label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_tracked_zonal</priority>
- <service>parcel_post_tracked_zonal</service>
- <length type="integer">250</length>
- <code>PCBXT</code>
- <cost>5.50</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>Across town</description>
- <speed-description>Next working day</speed-description>
- <product-url nil="true"></product-url>
- <product-max-dimensions>15000*15000*15000 25000gms</product-max-dimensions>
- <service-group-description>Parcel Post Tracked Zonal</service-group-description>
- <packaging>postage_only</packaging>
- <label-url>https://www.nzpost.co.nz/cart/checkout</label-url>
- </product>
- <product>
- <thickness type="integer">20</thickness>
- <priority>parcel_post_po_box_priority</priority>
- <service>parcel_post_po_box_priority</service>
- <length type="integer">250</length>
- <code>PIXFC5</code>
- <cost>4.00</cost>
- <weight type="float">0.4</weight>
- <height type="integer">150</height>
- <description>C5 Flat Bag</description>
- <speed-description>Next working day</speed-description>
- <product-url>http://www.nzpost.co.nz/products-services/postage-packaging/bags/parcelpost-fast/10-x-c5-flat-parcelpost-fast-postage-includ</product-url>
- <product-max-dimensions>185*270*70 3000gms</product-max-dimensions>
- <service-group-description>Parcel Post Po Box Priority</service-group-description>
- <packaging>flat</packaging>
- <label-url nil="true"></label-url>
- </product>
- </products>
-</hash>
-
View
18 test/fixtures/xml/newzealandpost/example_response_error.xml
@@ -1,18 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<hash>
- <status>failed</status>
- <exception>DimensionParameterInvalid</exception>
- <message>weight Must be less than 25 Kg</message>
- <params>
- <format>xml</format>
- <thickness>2</thickness>
- <postcode-dest>6012</postcode-dest>
- <weight>55.4</weight>
- <api-key>123</api-key>
- <action>rate</action>
- <height>25</height>
- <length>15</length>
- <controller>api</controller>
- <postcode-src>6011</postcode-src>
- </params>
-</hash>
View
169 test/remote/new_zealand_post_test.rb
@@ -6,66 +6,141 @@ def setup
@packages = TestFixtures.packages
@locations = TestFixtures.locations
@carrier = NewZealandPost.new(fixtures(:new_zealand_post).merge(:test => true))
+ @wellington = @locations[:wellington]
+ @auckland = @locations[:auckland]
+ @ottawa = @locations[:ottawa]
end
-
+
def test_valid_credentials
assert @carrier.valid_credentials?
end
-
- def test_successful_rates_request
- response = @carrier.find_rates(@locations[:wellington],
- @locations[:wellington],
- @packages.values_at(:book, :wii))
+
+ def test_domestic_response
+ response = @carrier.find_rates(@wellington, @auckland, @packages[:wii])
+
assert response.is_a?(RateResponse)
assert response.success?
assert response.rates.any?
assert response.rates.first.is_a?(RateEstimate)
+ assert_equal 1, response.params["responses"].size
+ assert_equal 1, response.request.size
+ assert_equal 1, response.raw_responses.size
+ assert response.request.first.size > 0
+ assert response.params["responses"].first.size > 0
+ assert response.raw_responses.first.size > 0
end
- def test_failure_rates_request
- begin
- @carrier.find_rates(@locations[:wellington],
- @locations[:wellington],
- @packages[:shipping_container])
-
- flunk "expected an ActiveMerchant::Shipping::ResponseError to be raised"
- rescue ActiveMerchant::Shipping::ResponseError => e
- assert_equal 'length Value is too large', e.message
- end
+ def test_domestic_combined_response
+ response = @carrier.find_rates(@wellington, @auckland, @packages.values_at(:book, :small_half_pound))
+
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert response.rates.any?
+ assert response.rates.first.is_a?(RateEstimate)
+ assert_equal 2, response.params["responses"].size
+ assert_equal 2, response.request.size
+ assert_equal 2, response.raw_responses.size
+ assert response.request.first.size > 0
+ assert response.params["responses"].first.size > 0
+ assert response.raw_responses.first.size > 0
end
- def test_multiple_packages_are_combined_correctly
- response_wii = @carrier.find_rates(@locations[:wellington],
- @locations[:wellington],
- @packages.values_at(:wii))
- response_book = @carrier.find_rates(@locations[:wellington],
- @locations[:wellington],
- @packages.values_at(:book))
- response_combined = @carrier.find_rates(@locations[:wellington],
- @locations[:wellington],
- @packages.values_at(:book, :wii))
-
-
- wii_rates, book_rates, combined_rates = {}, {}, {}
- response_wii.rate_estimates.each{ |r| wii_rates[r.service_code] = r.total_price }
- response_book.rate_estimates.each{ |r| book_rates[r.service_code] = r.total_price }
- response_combined.rate_estimates.each{ |r| combined_rates[r.service_code] = r.total_price }
-
- # every item in combined rates is made up of entries from the other two rates
- combined_rates.each do |service_code, total_price|
- assert_equal (wii_rates[service_code] + book_rates[service_code]), total_price
+ def test_domestic_failed_response_raises
+ assert_raises ActiveMerchant::Shipping::ResponseError do
+ @carrier.find_rates(@wellington, @auckland, @packages[:shipping_container])
end
+ end
+
+ def test_domestic_failed_response_message
+ error = @carrier.find_rates(@wellington, @auckland, @packages[:shipping_container]) rescue $!
+ assert_match /Length can only be between 0 and 150cm/, error.message
+ end
+
+ def test_domestic_combined_response_prices
+ response_book = @carrier.find_rates(@wellington, @auckland, @packages[:book])
+ response_small_half_pound = @carrier.find_rates(@wellington, @auckland, @packages[:small_half_pound])
+ response_combined = @carrier.find_rates(@wellington, @auckland, @packages.values_at(:book, :small_half_pound))
- # the size of the elements common between wii and book rates is the size of the
- # combined rates hash.
- assert_equal (wii_rates.keys & book_rates.keys).count, combined_rates.size
-
- #uncomment this test for visual display of combining rates
- #puts "\nWii:"
- #response_wii.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name}" }
- #puts "\nBook:"
- #response_book.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name}" }
- #puts "\nCombined"
- #response_combined.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name}" }
+ assert response_combined.is_a?(RateResponse)
+ assert response_combined.success?
+ assert response_book.rates.first.is_a?(RateEstimate)
+ assert response_small_half_pound.rates.first.is_a?(RateEstimate)
+ assert response_combined.rates.first.is_a?(RateEstimate)
+
+ sum_book_prices = response_book.rates.sum { |rate| rate.price }
+ sum_small_half_pound_prices = response_small_half_pound.rates.sum { |rate| rate.price }
+ sum_combined_prices = response_combined.rates.sum { |rate| rate.price }
+
+ assert sum_book_prices > 0
+ assert sum_small_half_pound_prices > 0
+ assert sum_combined_prices > 0
+ assert sum_combined_prices <= sum_book_prices + sum_small_half_pound_prices
+
+ # uncomment this test for visual display of combining rates
+ # puts "\nBook:"
+ # response_wii.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name} (#{r.service_code})" }
+ # puts "\Small half pound:"
+ # response_book.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name} (#{r.service_code})" }
+ # puts "\nCombined"
+ # response_combined.rate_estimates.each{ |r| puts "\nTotal Price: #{r.total_price}\nService Name: #{r.service_name} (#{r.service_code})" }
end
+
+ def test_international_book_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:book])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert response.rates.any?
+ assert response.rates.first.is_a?(RateEstimate)
+ end
+
+ def test_international_poster_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:poster])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert response.rates.any?
+ assert response.rates.first.is_a?(RateEstimate)
+ end
+
+ def test_international_combined_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages.values_at(:book, :poster))
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert response.rates.any?
+ assert response.rates.first.is_a?(RateEstimate)
+ assert_equal 2, response.params["responses"].size
+ assert_equal 2, response.request.size
+ assert_equal 2, response.raw_responses.size
+ assert response.request.first.size > 0
+ assert response.params["responses"].first.size > 0
+ assert response.raw_responses.first.size > 0
+ end
+
+ def test_international_shipping_container_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:shipping_container])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert_equal 0, response.rates.size
+ end
+
+ def test_international_gold_bar_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:largest_gold_bar])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert_equal 0, response.rates.size
+ end
+
+ def test_international_empty_package_response
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:just_zero_weight])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert_equal 0, response.rates.size
+ end
+
+ def test_international_just_country_given
+ response = @carrier.find_rates(@wellington, Location.new(:country => 'CZ'), @packages[:book])
+ assert response.is_a?(RateResponse)
+ assert response.success?
+ assert response.rates.size > 0
+ end
+
end
View
16 test/test_helper.rb
@@ -51,6 +51,10 @@ def xml_fixture(path) # where path is like 'usps/beverly_hills_to_ottawa_respons
open(File.join(File.dirname(__FILE__),'fixtures','xml',"#{path}.xml")) {|f| f.read}
end
+ def json_fixture(path) # where path is like 'usps/beverly_hills_to_ottawa_response'
+ open(File.join(File.dirname(__FILE__),'fixtures','json',"#{path}.json")) {|f| f.read}
+ end
+
def symbolize_keys(hash)
return unless hash.is_a?(Hash)
@@ -71,17 +75,20 @@ module TestFixtures
@@packages = {
:just_ounces => Package.new(16, nil, :units => :imperial),
:just_grams => Package.new(1000, nil),
+ :just_zero_grams => Package.new(0, nil),
:all_imperial => Package.new(16, [1,8,12], :units => :imperial),
:all_metric => Package.new(1000, [2,20,40]),
:book => Package.new(250, [14, 19, 2]),
:wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'GBP'),
:american_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'USD'),
+ :new_zealand_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'NZD'),
:worthless_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 0.0, :currency => 'USD'),
:poster => Package.new(100, [93,10], :cylinder => true),
:small_half_pound => Package.new(8, [1,1,1], :units => :imperial),
:big_half_pound => Package.new((16 * 50), [24,24,36], :units => :imperial),
:chocolate_stuff => Package.new(80, [2,6,12], :units => :imperial),
- :shipping_container => Package.new(2200000, [2440, 2600, 6058], :description => '20 ft Standard Container', :units => :metric)
+ :shipping_container => Package.new(2200000, [2440, 2600, 6058], :description => '20 ft Standard Container', :units => :metric),
+ :largest_gold_bar => Package.new(250000, [ 45.5, 22.5, 17 ], :value => 15300000)
}
@@locations = {
@@ -182,7 +189,12 @@ module TestFixtures
:city => 'Wellington',
:address1 => '85 Victoria St',
:address2 => 'Te Aro',
- :postal_code => '6011')
+ :postal_code => '6011'),
+ :auckland => Location.new(
+ :country => 'NZ',
+ :city => 'Auckland',
+ :address1 => '192 Victoria St West',
+ :postal_code => '1010')
}
end
View
211 test/unit/carriers/new_zealand_post_test.rb
@@ -1,85 +1,176 @@
-require 'test_helper'
+require "test_helper"
class NewZealandPostTest < Test::Unit::TestCase
def setup
- @carrier = NewZealandPost.new(:key => '123')
+ @carrier = NewZealandPost.new(:key => "4d9dc0f0-dda0-012e-066f-000c29b44ac0")
+ @packages = TestFixtures.packages
+ @locations = TestFixtures.locations
+ @wellington = @locations[:wellington]
+ @auckland = @locations[:auckland]
+ @ottawa = @locations[:ottawa]
+ end
+
+ def test_domestic_book_request
+ url = "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&format=json&height=20&length=190&postcode_dest=1010&postcode_src=6011&thickness=140&weight=0.25"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/domestic_book") ])
+ @carrier.find_rates(@wellington, @auckland, @packages[:book])
+ end
+
+ def test_domestic_poster_request
+ url = "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&diameter=100&format=json&length=930&postcode_dest=1010&postcode_src=6011&weight=0.1"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/domestic_poster") ])
+ @carrier.find_rates(@wellington, @auckland, @packages[:poster])
+ end
+
+ def test_domestic_combined_request
+ urls = [
+ "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&format=json&height=20&length=190&postcode_dest=1010&postcode_src=6011&thickness=140&weight=0.25",
+ "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&format=json&height=25.4&length=25.4&postcode_dest=1010&postcode_src=6011&thickness=25.4&weight=0.226796185"
+ ]
+ @carrier.expects(:commit).with(urls).returns([ json_fixture("newzealandpost/domestic_book"), json_fixture("newzealandpost/domestic_small_half_pound") ])
+ @carrier.find_rates(@wellington, @auckland, @packages.values_at(:book, :small_half_pound))
+ end
+
+ def test_domestic_book_response
+ @carrier.expects(:commit).returns([ json_fixture("newzealandpost/domestic_book") ])
+ response = @carrier.find_rates(@wellington, @auckland, @packages[:book])
+ assert_equal 13, response.rates.size
+ assert_equal [ 300, 360, 400, 420, 450, 450, 500, 500, 550, 655, 715, 880, 890 ], response.rates.map(&:price)
+ end
- @response = xml_fixture('newzealandpost/example_response')
- @bad_response = xml_fixture('newzealandpost/example_response_error')
+ def test_domestic_poster_response
+ @carrier.expects(:commit).returns([ json_fixture("newzealandpost/domestic_poster") ])
+ response = @carrier.find_rates(@wellington, @auckland, @packages[:poster])
+ assert_equal 1, response.rates.size
+ assert_equal [ 750 ], response.rates.map(&:price)
+ end
- @origin = Location.new(:postal_code => "6011")
- @destination = Location.new(:postal_code => "6012")
- @line_items = [Package.new(400,
- [25, 15, 2],
- :description => "Edmonds Cookbook",
- :units => :metric),
- Package.new(300,
- [85, 55],
- :cylinder => true,
- :description => "Movie Poster",
- :units => :metric)]
+ def test_domestic_combined_response_parsing
+ @carrier.expects(:commit).returns([ json_fixture("newzealandpost/domestic_book"), json_fixture("newzealandpost/domestic_small_half_pound") ])
+ response = @carrier.find_rates(@wellington, @auckland, @packages.values_at(:book, :small_half_pound))
+ assert_equal 8, response.rates.size
+ assert_equal [ 800, 840, 900, 900, 1000, 1100, 1430, 1780 ], response.rates.map(&:price)
+ assert_equal [ "PIKFC5", "PCB3C5", "PIFFC5", "PIKBC5", "PIFBC5", "PICBC5", "NZPRBA5", "NZSRBA5" ], response.rates.map(&:service_code)
+ names = [
+ "ParcelPost C5 Flat Bag",
+ "ParcelPost Tracked C5 Postage Only - Tracked",
+ "ParcelPost Fast C5 Flat Bag",
+ "ParcelPost C5 Bubble Bag",
+ "ParcelPost Fast C5 Bubble Bag",
+ "ParcelPost Tracked C5 Bubble Bag - Tracked",
+ "Courier C5 Ready To Go Courier Bubble Bag",
+ "Courier C5 Ready To Go Courier Bubble Bag S/R"
+ ]
+ assert_equal names, response.rates.map(&:service_name)
end
- def test_build_request_rectangular
- params = @carrier.send(:build_rectangular_request_params, @origin, @destination, @line_items[0])
+ def test_domestic_shipping_container_response_error
+ @carrier.expects(:commit).returns([ json_fixture("newzealandpost/domestic_error") ])
+ error = @carrier.find_rates(@wellington, @auckland, @packages[:shipping_container]) rescue $!
+ assert_equal ActiveMerchant::Shipping::ResponseError, error.class
+ assert_equal "Weight can only be between 0 and 25kg", error.message
+ assert_equal [ json_fixture("newzealandpost/domestic_error") ], error.response.raw_responses
+ response_params = { "responses" => [ JSON.parse(json_fixture("newzealandpost/domestic_error")) ] }
+ assert_equal response_params, error.response.params
+ end
- assert_equal '123', params[:api_key]
- 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]
+ def test_domestic_blank_package_response
+ url = "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&format=json&height=0&length=0&postcode_dest=1010&postcode_src=6011&thickness=0&weight=0.0"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/domestic_default") ])
+ response = @carrier.find_rates(@wellington, @auckland, @packages[:just_zero_grams])
+ assert_equal [ 240, 300, 400, 420, 450, 450, 450, 500, 550, 589, 715, 830, 890 ], response.rates.map(&:price)
end
- def test_build_request_cylinder
- params = @carrier.send(:build_tube_request_params, @origin, @destination, @line_items[1])
+ def test_domestic_book_response_params
+ url = "http://api.nzpost.co.nz/ratefinder/domestic?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&carrier=all&format=json&height=20&length=190&postcode_dest=1010&postcode_src=6011&thickness=140&weight=0.25"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/domestic_book") ])
+ response = @carrier.find_rates(@wellington, @auckland, @packages[:book])
+ assert_equal [ url ], response.request
+ assert_equal [ json_fixture("newzealandpost/domestic_book") ], response.raw_responses
+ assert_equal [ JSON.parse(json_fixture("newzealandpost/domestic_book")) ], response.params["responses"]
+ end
- assert_equal '123', params[:api_key]
- assert_equal '850', params[:length]
- assert_equal '550', params[:diameter]
- assert_equal '0.3', params[:weight]
- assert_equal '6011', params[:postcode_src]
- assert_equal '6012', params[:postcode_dest]
+ def test_international_book_request
+ url = "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=20&length=190&thickness=140&value=0&weight=0.25"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/international_book") ])
+ @carrier.find_rates(@wellington, @ottawa, @packages[:book])
end
+ def test_international_wii_request
+ url = "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=114.3&length=381.0&thickness=254.0&value=269&weight=3.401942775"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/international_new_zealand_wii") ])
+ @carrier.find_rates(@wellington, @ottawa, @packages[:new_zealand_wii])
+ end
- def test_parse_response
- @carrier.expects(:ssl_get).returns(@response)
- rate_response = @carrier.find_rates(@origin, @destination, @line_items[0])
- assert_not_nil rate_response
- assert_equal 2, rate_response.rates.size
-
- # test first element
-
- first_element = rate_response.rates.find{|rate| rate.service_code == 'PCBXT' }
- assert_equal 550, first_element.price
- assert_equal 'Parcel Post Tracked Zonal', first_element.service_name
-
- # test last element
- last_element = first_element = rate_response.rates.find{|rate| rate.service_code == 'PCB3C4' }
- assert_equal 540, last_element.price
- assert_equal 'Parcel Post Tracked', last_element.service_name
+ def test_international_uk_wii_request
+ url = "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=114.3&length=381.0&thickness=254.0&value=0&weight=3.401942775"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/international_wii") ])
+ @carrier.find_rates(@wellington, @ottawa, @packages[:wii])
end
- def test_response_success_with_successful_response
- xml = REXML::Document.new(@response)
- assert_equal true, @carrier.send(:response_success?, xml)
+ def test_international_book_response_params
+ url = "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=20&length=190&thickness=140&value=0&weight=0.25"
+ @carrier.expects(:commit).with([ url ]).returns([ json_fixture("newzealandpost/international_book") ])
+ response = @carrier.find_rates(@wellington, @ottawa, @packages[:book])
+ assert_equal [ url ], response.request
+ assert_equal [ json_fixture("newzealandpost/international_book") ], response.raw_responses
+ assert_equal [ JSON.parse(json_fixture("newzealandpost/international_book")) ], response.params["responses"]
end
- def test_response_success_with_bad_response
- xml = REXML::Document.new(@bad_response)
- assert_equal false, @carrier.send(:response_success?, xml)
+ def test_international_combined_request
+ urls = [
+ "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=20&length=190&thickness=140&value=0&weight=0.25",
+ "http://api.nzpost.co.nz/ratefinder/international?api_key=4d9dc0f0-dda0-012e-066f-000c29b44ac0&country_code=CA&format=json&height=25.4&length=25.4&thickness=25.4&value=0&weight=0.226796185"
+ ]
+ @carrier.expects(:commit).with(urls).returns([ json_fixture("newzealandpost/international_book"), json_fixture("newzealandpost/international_wii") ])
+ @carrier.find_rates(@wellington, @ottawa, @packages.values_at(:book, :small_half_pound))
end
- def test_response_message_with_successful_response
- xml = REXML::Document.new(@response)
- assert_equal 'Success', @carrier.send(:response_message, xml)
+ def test_international_combined_response_parsing
+ @carrier.expects(:commit).returns([ json_fixture("newzealandpost/international_book"), json_fixture("newzealandpost/international_small_half_pound") ])
+ response = @carrier.find_rates(@wellington, @ottawa, @packages.values_at(:book, :small_half_pound))
+ assert_equal 4, response.rates.size
+ assert_equal [ 13050, 8500, 2460, 2214 ], response.rates.map(&:price)
+ assert_equal [ "ICPNC500", "IEZPC500", "IACNC500", "IECNC500" ], response.rates.map(&:service_code)
+ names = [
+ "International Express Courier Int Express Pcl Zone C 500gm",
+ "International Economy Courier Int Econ Cour Pcl Zn C 500gm",
+ "International Air Zone C AirPost Cust Pcl 500gm",
+ "International Economy Zone C EconomyPost Pcl 500gm"
+ ]
+ assert_equal names, response.rates.map(&:service_name)
end
- def test_response_message_with_bad_response
- xml = REXML::Document.new(@bad_response)
- assert_equal 'weight Must be less than 25 Kg', @carrier.send(:response_message, xml)
+ def test_international_empty_json_response_error
+ @carrier.expects(:commit).returns([ "" ])
+ error = @carrier.find_rates(@wellington, @ottawa, @packages[:book]) rescue $!
+ assert_equal ActiveMerchant::Shipping::ResponseError, error.class
+ assert_equal "A JSON text must at least contain two octets!", error.message
+ assert_equal [ "" ], error.response.raw_responses
+ response_params = { "responses" => [] }
+ assert_equal response_params, error.response.params
end
+
+ def test_international_invalid_json_response_error
+ @carrier.expects(:commit).returns([ "<>" ])
+ error = @carrier.find_rates(@wellington, @ottawa, @packages[:book]) rescue $!
+ assert_equal ActiveMerchant::Shipping::ResponseError, error.class
+ assert error.message.include?("unexpected token")
+ assert_equal [ "<>" ], error.response.raw_responses
+ response_params = { "responses" => [] }
+ assert_equal response_params, error.response.params
+ end
+
+ def test_international_invalid_origin_country_response
+ error = @carrier.find_rates(@ottawa, @wellington, @packages[:book]) rescue $!
+ assert_equal ActiveMerchant::Shipping::ResponseError, error.class
+ assert_equal "New Zealand Post packages must originate in New Zealand", error.message
+ assert_equal [], error.response.raw_responses
+ assert_equal Array, error.response.request.class
+ assert_equal 1, error.response.request.size
+ response_params = { "responses" => [] }
+ assert_equal response_params, error.response.params
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.