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.
53 54 55 56 57 58 59 60 61 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 53 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
46 47 48 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 46 def raw_responses=(value) @raw_responses = value end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
45 46 47 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 45 def urls @urls end |
Class Method Details
.domestic?(locations) ⇒ Boolean (protected)
80 81 82 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 80 def self.domestic?(locations) locations.select { |location| new_zealand?(location) }.size == 2 end |
.from(*args) ⇒ Object
48 49 50 51 |
# 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 |
Instance Method Details
#new_zealand_origin? ⇒ Boolean
70 71 72 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 70 def new_zealand_origin? self.class.new_zealand?(@origin) end |
#params(package) ⇒ Object (protected)
138 139 140 |
# 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) ⇒ Object (protected)
130 131 132 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 130 def parse_response(response) JSON.parse(response) end |
#product_arrays ⇒ Object (protected)
119 120 121 122 123 124 |
# 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 ⇒ Object (protected)
115 116 117 |
# 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) ⇒ Object (protected)
97 98 99 100 101 102 103 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 97 def (products) { :total_price => products.sum { |product| price(product) }, :currency => "NZD", :service_code => products.first["code"] } end |
#rate_response ⇒ Object
63 64 65 66 67 68 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 63 def rate_response @rates = rates NewZealandPostRateResponse.new(true, "success", response_params, ) rescue => error NewZealandPostRateResponse.new(false, error., response_params, ) end |
#rates ⇒ Object (protected)
105 106 107 108 109 |
# 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, (products)) end end |
#rates_hash ⇒ Object (protected)
111 112 113 |
# 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 ⇒ Object (protected)
84 85 86 87 88 89 90 91 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 84 def { :rates => @rates, :raw_responses => @raw_responses, :request => @urls, :test => @test } end |
#response_params ⇒ Object (protected)
93 94 95 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 93 def response_params { :responses => @responses } end |
#responses ⇒ Object (protected)
126 127 128 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 126 def responses @responses = @raw_responses.map { |response| parse_response(response) } end |
#url(package) ⇒ Object (protected)
134 135 136 |
# File 'lib/active_shipping/carriers/new_zealand_post.rb', line 134 def url(package) "#{URL}/#{api}?#{params(package).to_query}" end |