Class: Workarea::Shipping::RateLookup

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/shipping/rate_lookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @origin = origin
  @destination = destination
  @packages = packages
  @options = options
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



4
5
6
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4

def destination
  @destination
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4

def options
  @options
end

#originObject (readonly)

Returns the value of attribute origin.



4
5
6
# File 'app/models/workarea/shipping/rate_lookup.rb', line 4

def origin
  @origin
end

#packagesObject (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_servicesObject



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

#responseObject



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

#subtotalObject



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

#valid_servicesObject



28
29
30
# File 'app/models/workarea/shipping/rate_lookup.rb', line 28

def valid_services
  localized_services & Shipping::Service.by_price(subtotal)
end