Class: Calculator::ActiveShipping

Inherits:
Calculator
  • Object
show all
Includes:
ActiveMerchant::Shipping
Defined in:
app/models/calculator/active_shipping.rb

Overview

This is a base calculator for shipping calcualations using the ActiveShipping plugin. It is not intended to be instantiated directly. Create sublcass for each specific shipping method you wish to support instead.

Direct Known Subclasses

Fedex::Base, Ups::Base, Usps::Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registerObject



7
8
9
10
# File 'app/models/calculator/active_shipping.rb', line 7

def self.register
  super
  ShippingMethod.register_calculator(self)
end

Instance Method Details

#compute(object) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/calculator/active_shipping.rb', line 12

def compute(object)
  if object.is_a?(Array)
    order = object.first.order
  elsif object.is_a?(Shipment)
    order = object.order
  else
    order = object
  end
  origin= Location.new(:country => Spree::ActiveShipping::Config[:origin_country],
                       :city => Spree::ActiveShipping::Config[:origin_city],
                       :state => Spree::ActiveShipping::Config[:origin_state],
                       :zip => Spree::ActiveShipping::Config[:origin_zip])

  addr = order.ship_address

  destination = Location.new(:country => addr.country.iso,
                            :state => (addr.state ? addr.state.abbr : addr.state_name),
                            :city => addr.city,
                            :zip => addr.zipcode)

  rates = Rails.cache.fetch(cache_key(order)) do
    rates = retrieve_rates(origin, destination, packages(order))
  end

  return nil if rates.empty?
  rate = rates[self.class.description].to_f + (Spree::ActiveShipping::Config[:handling_fee].to_f || 0.0)
  return nil unless rate
  # divide by 100 since active_shipping rates are expressed as cents
  return rate/100.0
end

#timing(line_items) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/calculator/active_shipping.rb', line 44

def timing(line_items)
  order = line_items.first.order
  origin      = Location.new(:country => Spree::ActiveShipping::Config[:origin_country],
                             :city => Spree::ActiveShipping::Config[:origin_city],
                             :state => Spree::ActiveShipping::Config[:origin_state],
                             :zip => Spree::ActiveShipping::Config[:origin_zip])
  addr = order.ship_address
  destination = Location.new(:country => addr.country.iso,
                            :state => (addr.state ? addr.state.abbr : addr.state_name),
                            :city => addr.city,
                            :zip => addr.zipcode)
  timings = Rails.cache.fetch(cache_key(line_items)+"-timings") do
    timings = retrieve_timings(origin, destination, packages(order))
  end
  return nil if timings.nil? || !timings.is_a?(Hash) || timings.empty?
  return timings[self.description]

end