Class: Hooksmith::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hooksmith/configuration.rb

Overview

Configuration holds the registry of all processors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



13
14
15
16
17
# File 'lib/hooksmith/configuration.rb', line 13

def initialize
  # Registry structure: { provider_symbol => [{ event: event_symbol, processor: ProcessorClass }, ...] }
  @registry = Hash.new { |hash, key| hash[key] = [] }
  @event_store_config = Hooksmith::Config::EventStore.new
end

Instance Attribute Details

#event_store_configHooksmith::Config::EventStore (readonly)

Returns configuration for event persistence.

Returns:



11
12
13
# File 'lib/hooksmith/configuration.rb', line 11

def event_store_config
  @event_store_config
end

#registryHash (readonly)

Returns a registry mapping provider symbols to arrays of processor entries.

Returns:

  • (Hash)

    a registry mapping provider symbols to arrays of processor entries.



9
10
11
# File 'lib/hooksmith/configuration.rb', line 9

def registry
  @registry
end

Instance Method Details

#event_store {|Hooksmith::Config::EventStore| ... } ⇒ Object

Configure event store persistence.

Examples:

Hooksmith.configure do |config|
  config.event_store do |store|
    store.enabled = true
    store.model_class_name = 'MyApp::WebhookEvent'
    store.record_timing = :before # or :after, or :both
    store.mapper = ->(provider:, event:, payload:) {
      { provider:, event: event.to_s, payload:, received_at: (Time.respond_to?(:current) ? Time.current : Time.now) }
    }
  end
end

Yields:



61
62
63
# File 'lib/hooksmith/configuration.rb', line 61

def event_store
  yield(@event_store_config)
end

#processors_for(provider, event) ⇒ Array<Hash>

Returns all processor entries for a given provider and event.

Parameters:

  • provider (Symbol, String)

    the provider name

  • event (Symbol, String)

    the event name

Returns:

  • (Array<Hash>)

    the array of matching entries.



43
44
45
# File 'lib/hooksmith/configuration.rb', line 43

def processors_for(provider, event)
  registry[provider.to_sym].select { |entry| entry[:event] == event.to_sym }
end

#provider(provider_name) {|Hooksmith::Config::Provider| ... } ⇒ Object

Groups registrations under a specific provider.

Parameters:

  • provider_name (Symbol, String)

    the provider name (e.g., :stripe)

Yields:



23
24
25
26
27
# File 'lib/hooksmith/configuration.rb', line 23

def provider(provider_name)
  provider_config = Hooksmith::Config::Provider.new(provider_name)
  yield(provider_config)
  registry[provider_name.to_sym].concat(provider_config.entries)
end

#register_processor(provider, event, processor_class_name) ⇒ Object

Direct registration of a processor.

Parameters:

  • provider (Symbol, String)

    the provider name

  • event (Symbol, String)

    the event name

  • processor_class_name (String)

    the processor class name



34
35
36
# File 'lib/hooksmith/configuration.rb', line 34

def register_processor(provider, event, processor_class_name)
  registry[provider.to_sym] << { event: event.to_sym, processor: processor_class_name }
end