Class: Spree::DeliveryRateProvider::Base

Inherits:
Object
  • Object
show all
Extended by:
ApiTyped
Includes:
IntegrationBackedProvider
Defined in:
app/models/spree/delivery_rate_provider/base.rb

Overview

Strategy that answers "what does this delivery method cost for this package, and what metadata comes with the quote?".

Stored as a string class name on DeliveryMethod and constantized at call time; registered via Spree.delivery_rate_providers. The Estimator dispatches here in place of calling the calculator directly, so filtering, markup, tax resolution and sorting stay in one place for every provider.

External providers resolve credentials through the delivery method's store integration — they never store credentials themselves.

Direct Known Subclasses

Freight, Internal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiTyped

api_type, api_type_for, api_typed_family, class_name_for, find, registered_class

Methods included from IntegrationBackedProvider

#integration_for

Constructor Details

#initialize(delivery_method) ⇒ Base

Returns a new instance of Base.

Parameters:



64
65
66
# File 'app/models/spree/delivery_rate_provider/base.rb', line 64

def initialize(delivery_method)
  @delivery_method = delivery_method
end

Instance Attribute Details

#delivery_method ⇒ Object (readonly)

Returns the value of attribute delivery_method.



18
19
20
# File 'app/models/spree/delivery_rate_provider/base.rb', line 18

def delivery_method
  @delivery_method
end

Class Method Details

.requires_address? ⇒ Boolean

Whether this provider quotes real shipments to an address — true for carriers, which therefore can only price methods whose fulfillment provider ships. The Internal (calculator) provider prices anything.

Returns:

  • (Boolean)


27
28
29
# File 'app/models/spree/delivery_rate_provider/base.rb', line 27

def requires_address?
  false
end

.service_catalog(_integration) ⇒ Spree::DeliveryRateProvider::ServiceCatalog

The carrier services this store can offer, for the admin service picker. Fetch them live from the carrier wherever the API allows — a hardcoded list shows services the merchant has not enabled and hides the ones they have.

Return ServiceCatalog.listing with [{ carrier:, service:, label: }], ServiceCatalog.unavailable with the seller's message when the listing cannot be fetched (unconnected integration, credential tier without access, carrier outage), or ServiceCatalog.none — the default — when the provider lists no services and the merchant types identifiers free-form.

Never raise: the picker degrades to free-form entry, and a failed listing must not block configuring a delivery method.

Parameters:

Returns:



58
59
60
# File 'app/models/spree/delivery_rate_provider/base.rb', line 58

def service_catalog(_integration)
  ServiceCatalog.none
end

.uses_calculator? ⇒ Boolean

Whether the method's calculator decides the price. False for carrier providers, which quote live rates — their methods still carry a calculator (the Estimator consults available?) but its amount is never used, so admin UIs hide the pricing form.

Returns:

  • (Boolean)


37
38
39
# File 'app/models/spree/delivery_rate_provider/base.rb', line 37

def uses_calculator?
  false
end

Instance Method Details

#book(_delivery_rate) ⇒ Object

Reserves the quote with the carrier once a customer selects it. Spree does not invoke this yet — it is reserved for the rate booking flow; until then a provider gem calls it from its own fulfillment provider.



97
# File 'app/models/spree/delivery_rate_provider/base.rb', line 97

def book(_delivery_rate); end

#estimate(_package) ⇒ Spree::DeliveryRateProvider::Estimate?

Quotes this delivery method for a package, for providers that yield a single rate.

Returning nil suppresses the method, matching the calculator contract where a nil cost hides it — that equivalence is what lets FlatRate's thresholds keep working through the Internal provider.

Parameters:

Returns:

Raises:

  • (NotImplementedError)


89
90
91
# File 'app/models/spree/delivery_rate_provider/base.rb', line 89

def estimate(_package)
  raise NotImplementedError, "Please implement 'estimate' or 'estimates' in your delivery rate provider: #{self.class.name}"
end

#estimates(package) ⇒ Array<Spree::DeliveryRateProvider::Estimate>

Every quote this delivery method yields for a package — the entry point the Estimator calls. Carrier providers override this to return one Estimate per service ("UPS Ground", "UPS Express", …) from a single API call; single-quote providers implement #estimate and get wrapped automatically. An empty array offers nothing.

Parameters:

Returns:



76
77
78
# File 'app/models/spree/delivery_rate_provider/base.rb', line 76

def estimates(package)
  Array(estimate(package)).compact
end

#release(_delivery_rate) ⇒ Object

Releases a previously booked quote. Same status as #book.



100
# File 'app/models/spree/delivery_rate_provider/base.rb', line 100

def release(_delivery_rate); end