Class: Workarea::Shipping::RateLookup
- Inherits:
-
Object
- Object
- Workarea::Shipping::RateLookup
- Defined in:
- app/models/workarea/shipping/rate_lookup.rb
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#packages ⇒ Object
readonly
Returns the value of attribute packages.
Instance Method Summary collapse
-
#initialize(origin, destination, packages, options = {}) ⇒ RateLookup
constructor
A new instance of RateLookup.
- #localized_services ⇒ Object
- #response ⇒ Object
- #subtotal ⇒ Object
- #valid_services ⇒ Object
Constructor Details
#initialize(origin, destination, packages, options = {}) ⇒ RateLookup
Returns a new instance of RateLookup.
6 7 8 9 10 11 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 6 def initialize(origin, destination, packages, = {}) @origin = origin @destination = destination @packages = packages @options = end |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
4 5 6 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4 def destination @destination end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4 def @options end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
4 5 6 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4 def origin @origin end |
#packages ⇒ Object (readonly)
Returns the value of attribute packages.
4 5 6 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4 def packages @packages end |
Instance Method Details
#localized_services ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 13 def localized_services @localized_services ||= if destination.present? Shipping::Service.for_location( destination.country, destination.region ) else Shipping::Service.all.to_a end end |
#response ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 32 def response ActiveShipping::RateResponse.new( true, # success 'success', # message {}, # params rates: valid_services.map do |service| total = service.find_rate(subtotal).try(:price) next unless total.present? ActiveShipping::RateEstimate.new( origin, destination, service.carrier, service.name, service_code: service.service_code, total_price: total.cents, currency: total.currency.to_s ) end.compact ) end |
#subtotal ⇒ Object
24 25 26 |
# File 'app/models/workarea/shipping/rate_lookup.rb', line 24 def subtotal @subtotal ||= packages.map { |p| Money.new(p.value, p.currency) }.sum end |