Class: ReactiveShipping::NewZealandPost::RateRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive_shipping/carriers/new_zealand_post.rb

Direct Known Subclasses

Domestic, International

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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/reactive_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

Instance Attribute Details

#raw_responses=(value) ⇒ Object (writeonly)

Sets the attribute raw_responses

Parameters:

  • value

    the value to set the attribute raw_responses to.



46
47
48
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 46

def raw_responses=(value)
  @raw_responses = value
end

#urlsObject (readonly)

Returns the value of attribute urls.



45
46
47
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 45

def urls
  @urls
end

Class Method Details

.domestic?(locations) ⇒ Boolean (protected)

Returns:

  • (Boolean)


80
81
82
# File 'lib/reactive_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/reactive_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_zealand?(location) ⇒ Boolean (protected)

Returns:

  • (Boolean)


76
77
78
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 76

def self.new_zealand?(location)
  ['NZ', nil].include?(Location.from(location).country_code)
end

Instance Method Details

#new_zealand_origin?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/reactive_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/reactive_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/reactive_shipping/carriers/new_zealand_post.rb', line 130

def parse_response(response)
  JSON.parse(response)
end

#product_arraysObject (protected)



119
120
121
122
123
124
# File 'lib/reactive_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_hashObject (protected)



115
116
117
# File 'lib/reactive_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/reactive_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

#rate_responseObject



63
64
65
66
67
68
# File 'lib/reactive_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

#ratesObject (protected)



105
106
107
108
109
# File 'lib/reactive_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_hashObject (protected)



111
112
113
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 111

def rates_hash
  products_hash.select { |_service, products| products.size == @packages.size }
end

#response_optionsObject (protected)



84
85
86
87
88
89
90
91
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 84

def response_options
  {
    :rates => @rates,
    :raw_responses => @raw_responses,
    :request => @urls,
    :test => @test
  }
end

#response_paramsObject (protected)



93
94
95
# File 'lib/reactive_shipping/carriers/new_zealand_post.rb', line 93

def response_params
  { :responses => @responses }
end

#responsesObject (protected)



126
127
128
# File 'lib/reactive_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/reactive_shipping/carriers/new_zealand_post.rb', line 134

def url(package)
  "#{URL}/#{api}?#{params(package).to_query}"
end