Class: Spree::Integration

Inherits:
Object
  • Object
show all
Includes:
PreferenceSchema, ProviderListing, SecretPreferences, SingleStoreResource
Defined in:
app/models/spree/integration.rb

Constant Summary collapse

WebhookSignatureError =

Raised by #parse_webhook_event when a webhook's signature is missing or does not match this integration's secret. The webhook controller answers 401, so the sender knows its configuration is wrong rather than retrying forever.

Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SecretPreferences

#separated_secret_preferences

Methods included from PreferenceSchema

#serialized_preferences

Instance Attribute Details

#connection_error_message ⇒ String?

This attribute is used to temporarily store connection-related error messages that can be displayed to users when testing or validating integration connections. It is not persisted to the database and is reset on each new connection attempt.

Parameters:

  • message (String, nil) —

    The error message to be stored

Returns:

  • (String, nil) —

    The current error message



61
62
63
# File 'app/models/spree/integration.rb', line 61

def connection_error_message
  @connection_error_message
end

Class Method Details

.api_type ⇒ Object

Wire shorthand for the admin API. Provider gems follow the SpreeEasyPost::Integration convention, where demodulizing collapses every gem to 'integration' — so classes named exactly Integration derive the shorthand from their outer module instead (SpreeEasyPost → 'easy_post').



115
116
117
118
119
120
121
122
# File 'app/models/spree/integration.rb', line 115

def self.api_type
  return super unless name.demodulize == 'Integration'

  outer = name.deconstantize.delete_prefix('Spree')
  return super if outer.blank?

  outer.underscore
end

.description ⇒ String?

One-line marketing description shown on the integration's gallery card. Localize by shipping a Rails translation instead (or additionally): spree.integrations.<api_type>.description wins over this fallback.

Returns:

  • (String, nil)


75
76
77
# File 'app/models/spree/integration.rb', line 75

def self.description
  nil
end

.discovery_entries ⇒ Array<Hash>

Wire entries for the admin types-discovery endpoint — every registered integration with its gallery metadata and configuration schema, stably ordered by name.

Returns:

  • (Array<Hash>)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/spree/integration.rb', line 93

def self.discovery_entries
  registered_classes.map do |klass|
    {
      type: klass.api_type,
      name: klass.integration_name,
      group: klass.integration_group,
      description: klass.human_description,
      **klass.provider_listing,
      schema: klass.preference_json_schema
    }
  end.sort_by { |entry| entry[:name] }
end

.human_description ⇒ String?

Description for the current locale: the gem's Rails translation when present, otherwise the description fallback. Named per the human_name/human_description convention of the rule families.

Returns:

  • (String, nil)


84
85
86
# File 'app/models/spree/integration.rb', line 84

def self.human_description
  I18n.t("spree.integrations.#{api_type}.description", default: description)
end

.integration_group ⇒ Object

Associates the integration to a group. The name is read as a translation key under spree. to display the group name. Leave blank to leave the integration ungrouped.



66
67
68
# File 'app/models/spree/integration.rb', line 66

def self.integration_group
  nil
end

.integration_name ⇒ Object



106
107
108
# File 'app/models/spree/integration.rb', line 106

def self.integration_name
  name.demodulize.titleize.strip
end

.registered_classes ⇒ Array<Class>

Integration classes registered via Spree.integrations. Entries may be class-name strings (the documented extension form) or classes.

Returns:

  • (Array<Class>)


28
29
30
# File 'app/models/spree/integration.rb', line 28

def self.registered_classes
  Spree.integrations.map { |entry| entry.is_a?(Class) ? entry : entry.to_s.constantize }
end

Instance Method Details

#can_connect? ⇒ Boolean

Checks if the integration can establish a connection. This is a base implementation that always returns true. Subclasses should override this method to implement their own connection validation logic.

Returns:

  • (Boolean) —

    true if the integration can connect, false otherwise



151
152
153
# File 'app/models/spree/integration.rb', line 151

def can_connect?
  true
end

#name ⇒ Object



124
125
126
# File 'app/models/spree/integration.rb', line 124

def name
  self.class.integration_name
end

#parse_webhook_event(_raw_post, _headers) ⇒ Hash?

Verifies and parses a provider webhook addressed to this integration.

The base implementation accepts nothing: an integration that takes webhooks overrides this, verifies the signature against its own secret (raising WebhookSignatureError when it is missing or wrong), and returns the event translated into vocabulary core understands. Carrier integrations return Deliveries::UpdateTracking keyword arguments plus tracking_code; nil means "not an event we act on", which the endpoint acknowledges without doing anything.

Parameters:

  • _raw_post (String) —

    the request body exactly as received — HMAC signatures are computed over the raw bytes, so it must not be re-parsed or re-encoded before verification

  • _headers (#[]) —

    the request headers

Returns:

  • (Hash, nil)


143
144
145
# File 'app/models/spree/integration.rb', line 143

def parse_webhook_event(_raw_post, _headers)
  nil
end