Class: ActiveShipping::NewZealandPost::RateRequest
- Inherits:
-
Object
- Object
- ActiveShipping::NewZealandPost::RateRequest
- Defined in:
- lib/active_shipping/carriers/new_zealand_post.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#raw_responses ⇒ Object
writeonly
Sets the attribute raw_responses.
-
#urls ⇒ Object
readonly
Returns the value of attribute urls.
Class Method Summary collapse
- .domestic?(locations) ⇒ Boolean protected
- .from(*args) ⇒ Object
- .new_zealand?(location) ⇒ Boolean protected
Instance Method Summary collapse
-
#initialize(origin, destination, packages, options) ⇒ RateRequest
constructor
A new instance of RateRequest.
- #new_zealand_origin? ⇒ Boolean
- #params(package) ⇒ Object protected
- #parse_response(response) ⇒ Object protected
- #product_arrays ⇒ Object protected
- #products_hash ⇒ Object protected
- #rate_options(products) ⇒ Object protected
- #rate_response ⇒ Object
- #rates ⇒ Object protected
- #rates_hash ⇒ Object protected
- #response_options ⇒ Object protected
- #response_params ⇒ Object protected
- #responses ⇒ Object protected
- #url(package) ⇒ Object protected
Constructor Details
#initialize(origin, destination, packages, options) ⇒ RateRequest
Returns a new instance of RateRequest.
55 56 57 58 59 60 61 62 63 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 55 def initialize(origin, destination, packages, ) @origin = Location.from(origin) @destination = Location.from(destination) @packages = Array(packages).map { |package| NewZealandPostPackage.new(package, api) } @params = { :format => "json", :api_key => [:key] } @test = [:test] @rates = @responses = @raw_responses = [] @urls = @packages.map { |package| url(package) } end |
Instance Attribute Details
#raw_responses=(value) ⇒ Object (writeonly)
Sets the attribute raw_responses
48 49 50 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 48 def raw_responses=(value) @raw_responses = value end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
47 48 49 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 47 def urls @urls end |
Class Method Details
.domestic?(locations) ⇒ Boolean (protected)
82 83 84 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 82 def self.domestic?(locations) locations.select { |location| new_zealand?(location) }.size == 2 end |
.from(*args) ⇒ Object
50 51 52 53 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 50 def self.from(*args) return International.new(*args) unless domestic?(args[0..1]) Domestic.new(*args) end |
Instance Method Details
#new_zealand_origin? ⇒ Boolean
72 73 74 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 72 def new_zealand_origin? self.class.new_zealand?(@origin) end |
#params(package) ⇒ Object (protected)
140 141 142 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 140 def params(package) @params.merge(api_params).merge(package.params) end |
#parse_response(response) ⇒ Object (protected)
132 133 134 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 132 def parse_response(response) JSON.parse(response) end |
#product_arrays ⇒ Object (protected)
121 122 123 124 125 126 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 121 def product_arrays responses.map do |response| raise(response["message"]) unless response["status"] == "success" response["products"] end end |
#products_hash ⇒ Object (protected)
117 118 119 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 117 def products_hash product_arrays.flatten.group_by { |product| service_name(product) } end |
#rate_options(products) ⇒ Object (protected)
99 100 101 102 103 104 105 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 99 def (products) { :total_price => products.sum { |product| price(product) }, :currency => "NZD", :service_code => products.first["code"] } end |
#rate_response ⇒ Object
65 66 67 68 69 70 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 65 def rate_response @rates = rates NewZealandPostRateResponse.new(true, "success", response_params, ) rescue => error NewZealandPostRateResponse.new(false, error., response_params, ) end |
#rates ⇒ Object (protected)
107 108 109 110 111 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 107 def rates rates_hash.map do |service, products| RateEstimate.new(@origin, @destination, NewZealandPost.name, service, (products)) end end |
#rates_hash ⇒ Object (protected)
113 114 115 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 113 def rates_hash products_hash.select { |_service, products| products.size == @packages.size } end |
#response_options ⇒ Object (protected)
86 87 88 89 90 91 92 93 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 86 def { :rates => @rates, :raw_responses => @raw_responses, :request => @urls, :test => @test } end |
#response_params ⇒ Object (protected)
95 96 97 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 95 def response_params { :responses => @responses } end |
#responses ⇒ Object (protected)
128 129 130 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 128 def responses @responses = @raw_responses.map { |response| parse_response(response) } end |
#url(package) ⇒ Object (protected)
136 137 138 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 136 def url(package) "#{URL}/#{api}?#{params(package).to_query}" end |