Class: Spree::DeliveryRateProvider::ServiceCatalog

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/spree/delivery_rate_provider/service_catalog.rb

Overview

The answer to "which carrier services can this store offer?", as the admin service picker needs it: either the services themselves, or the reason the provider could not list them.

Providers fetch live where their API allows it. When it does not — an unconnected integration, a credential tier that forbids the listing endpoint, a carrier outage — they return ServiceCatalog.unavailable with the seller's own message, and the picker explains the situation instead of showing a silently empty or invented list.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listing(services) ⇒ ServiceCatalog

Parameters:

  • services (Array<Hash>) —

    each { carrier:, service:, label: }

Returns:



32
33
34
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 32

def self.listing(services)
  new(services: Array(services))
end

.none ⇒ ServiceCatalog

A provider that lists no services at all — the merchant enters carrier/service identifiers free-form and every quoted rate is offered. Not an error state.

Returns:



26
27
28
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 26

def self.none
  new
end

.unavailable(message) ⇒ ServiceCatalog

Parameters:

  • message (String) —

    the seller's message, shown to the merchant

Returns:



38
39
40
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 38

def self.unavailable(message)
  new(error_message: message)
end

Instance Method Details

#available? ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 43

def available?
  error_message.blank?
end

#error_message ⇒ String?

Returns why the services could not be listed.

Returns:

  • (String, nil) —

    why the services could not be listed



19
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 19

attribute :error_message, :string

#services ⇒ Array<Hash>

Returns [{ carrier:, service:, label: }].

Returns:

  • (Array<Hash>) —

    [{ carrier:, service:, label: }]



17
# File 'app/models/spree/delivery_rate_provider/service_catalog.rb', line 17

attribute :services, default: -> { [] }