class ActiveShipping::NewZealandPost::RateRequest

Attributes

raw_responses[W]
urls[R]

Public Class Methods

from(*args) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 48
def self.from(*args)
  return International.new(*args) unless domestic?(args[0..1])
  Domestic.new(*args)
end
new(origin, destination, packages, options) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 53
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

Protected Class Methods

domestic?(locations) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 80
def self.domestic?(locations)
  locations.select { |location| new_zealand?(location) }.size == 2
end
new_zealand?(location) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 76
def self.new_zealand?(location)
  ['NZ', nil].include?(Location.from(location).country_code)
end

Public Instance Methods

new_zealand_origin?() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 70
def new_zealand_origin?
  self.class.new_zealand?(@origin)
end
rate_response() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 63
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

Protected Instance Methods

params(package) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 138
def params(package)
  @params.merge(api_params).merge(package.params)
end
parse_response(response) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 130
def parse_response(response)
  JSON.parse(response)
end
product_arrays() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 119
def product_arrays
  responses.map do |response|
    raise(response["message"]) unless response["status"] == "success"
    response["products"]
  end
end
products_hash() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 115
def products_hash
  product_arrays.flatten.group_by { |product| service_name(product) }
end
rate_options(products) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 97
def rate_options(products)
  {
    :total_price => products.sum { |product| price(product) },
    :currency => "NZD",
    :service_code => products.first["code"]
  }
end
rates() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 105
def rates
  rates_hash.map do |service, products|
    RateEstimate.new(@origin, @destination, NewZealandPost.name, service, rate_options(products))
  end
end
rates_hash() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 111
def rates_hash
  products_hash.select { |_service, products| products.size == @packages.size }
end
response_options() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 84
def response_options
  {
    :rates => @rates,
    :raw_responses => @raw_responses,
    :request => @urls,
    :test => @test
  }
end
response_params() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 93
def response_params
  { :responses => @responses }
end
responses() click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 126
def responses
  @responses = @raw_responses.map { |response| parse_response(response) }
end
url(package) click to toggle source
# File lib/active_shipping/carriers/new_zealand_post.rb, line 134
def url(package)
  "#{URL}/#{api}?#{params(package).to_query}"
end