Class: Spree::Calculator::Shipping::Frenet::Base

Inherits:
ShippingCalculator
  • Object
show all
Defined in:
app/models/spree/calculator/shipping/frenet/base.rb

Direct Known Subclasses

Azul, Pac, PacMini, Sedex

Instance Method Summary collapse

Instance Method Details

#compute(object = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'app/models/spree/calculator/shipping/frenet/base.rb', line 6

def compute(object = nil)
  # Low level cache to avoid multiple calls since frenet returns all options in the first call
  shipping_quotes = Rails.cache.fetch("#{object.order.number}/frenet_quotes", expires_in: 10.seconds) do
    ::Frenet::Api.get_shipping_quote(object)
  end
  get_rate_for_shipping_method(shipping_quotes)
end

#get_rate_for_shipping_method(shipping_quotes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/spree/calculator/shipping/frenet/base.rb', line 14

def get_rate_for_shipping_method(shipping_quotes)
  availabe_services = shipping_quotes["ShippingSevicesArray"].reject { |service| service["Error"] == true }
  quote_for_method = availabe_services.detect { |service| service["ServiceCode"] == shipping_method_service_code }
  return nil unless quote_for_method
  {
    delivery_business_days: quote_for_method["DeliveryTime"],
    delivery_transit_business_days: quote_for_method["DeliveryTime"],
    estimated_carrier_price: quote_for_method["ShippingPrice"],
    cost: quote_for_method["ShippingPrice"],
  }
end

#shipping_method_service_codeObject



26
27
28
29
# File 'app/models/spree/calculator/shipping/frenet/base.rb', line 26

def shipping_method_service_code
  # Constant defined on each shipping method calculator
  self.class::FRENET_SERVICE_CODE
end