Class: Calculator::Shipping::DoordashQuote
- Inherits:
-
ShippingCalculator
- Object
- ShippingCalculator
- Calculator::Shipping::DoordashQuote
- Defined in:
- app/models/spree/calculator/shipping/doordash_quote.rb
Overview
Prices a "DoorDash Delivery" shipping method against a real, live
DoorDash Drive quote rather than a flat/configured rate.
Spree::Stock::Estimator calls calculator.compute(package), which
Spree::Calculator#compute dispatches to compute_package by
demodulizing the argument's class name (Spree::Stock::Package ->
"package") — no override of #compute itself needed.
Class Method Summary collapse
Instance Method Summary collapse
-
#available?(package) ⇒ Boolean
Called by Estimator to filter which shipping methods even attempt a compute_package call.
- #compute_package(package) ⇒ Object
Class Method Details
.description ⇒ Object
17 18 19 |
# File 'app/models/spree/calculator/shipping/doordash_quote.rb', line 17 def self.description 'DoorDash Drive (live quote)' end |
Instance Method Details
#available?(package) ⇒ Boolean
Called by Estimator to filter which shipping methods even attempt a compute_package call. Skips the DoorDash API round-trip entirely for an order with no ship address yet (early checkout, before Estimator would sensibly be asked at all) — the real "can DoorDash serve this address" check still happens inside SpreeDoordash::Quote itself (compute_package returning nil is what actually makes the rate not show up).
54 55 56 |
# File 'app/models/spree/calculator/shipping/doordash_quote.rb', line 54 def available?(package) package.order.ship_address.present? end |
#compute_package(package) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/spree/calculator/shipping/doordash_quote.rb', line 21 def compute_package(package) result = SpreeDoordash::Quote.call(package.order) if result.nil? # A real bug found live: a nil Result here (bad/missing phone, an # address DoorDash genuinely can't serve, DoorDash API down — # SpreeDoordash::Quote#call collapses all of these to the same # "unavailable" nil, deliberately, so this stays generic to *why*) # used to just make the rate silently vanish with zero signal # anywhere in the UI. Spree::Order#warnings is the same # transient, request-scoped mechanism core's own # `ensure_available_shipping_rates` already uses for the # identical class of problem (a line item that can't ship at # all) — it flows through to the Store API's `cart.warnings` # with no new API surface needed. The storefront renders its own # localized copy for this code; `message` here is English-only, # a fallback for any other API consumer, not the UI text itself. package.order.warnings |= [{ code: 'doordash_quote_unavailable', message: 'We could not get a DoorDash delivery quote for this address.' }] return nil end result.fee_cents / 100.0 end |