Class: StandardId::Events::Subscribers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_id/events/subscribers/base.rb

Overview

Base class for event subscribers

Examples:

Single event subscription

class AuditSubscriber < StandardId::Events::Subscribers::Base
  subscribe_to StandardId::Events::AUTHENTICATION_SUCCEEDED

  def call(event)
    AuditLog.create!(
      event_type: event.short_name,
      account_id: event[:account]&.id,
      ip_address: event[:ip_address],
      metadata: event.payload
    )
  end
end

Multiple event subscription

class SecurityAlertSubscriber < StandardId::Events::Subscribers::Base
  subscribe_to StandardId::Events::AUTHENTICATION_FAILED,
               StandardId::Events::SESSION_REVOKED,
               StandardId::Events::ACCOUNT_LOCKED

  def call(event)
    SecurityMailer.alert(event.to_h).deliver_later
  end
end

Pattern subscription

class MetricsSubscriber < StandardId::Events::Subscribers::Base
  subscribe_to_pattern(/authentication/)

  def call(event)
    StatsD.increment("standard_id.#{event.short_name}")
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attachArray<Object>

Register this subscriber with the event system

Returns:

  • (Array<Object>)

    The subscription handles



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/standard_id/events/subscribers/base.rb', line 80

def attach
  instance = new
  @subscriptions ||= []

  if subscription_pattern
    @subscriptions << Events.subscribe(subscription_pattern) do |event|
      instance.handle(event)
    end
  end

  subscribed_events.each do |event_name|
    @subscriptions << Events.subscribe(event_name) do |event|
      instance.handle(event)
    end
  end

  @subscriptions
end

.attached?Boolean

Check if the subscriber is currently attached

Returns:

  • (Boolean)


116
117
118
# File 'lib/standard_id/events/subscribers/base.rb', line 116

def attached?
  @subscriptions&.any?
end

.detachvoid

This method returns an undefined value.

Unregister this subscriber from the event system



103
104
105
106
107
108
109
110
# File 'lib/standard_id/events/subscribers/base.rb', line 103

def detach
  return unless @subscriptions

  @subscriptions.each do |subscription|
    Events.unsubscribe(subscription)
  end
  @subscriptions = []
end

.subscribe_to(*event_names) ⇒ void

This method returns an undefined value.

Subscribe to specific event(s)

Parameters:

  • event_names (Array<String>)

    Event name constants



47
48
49
# File 'lib/standard_id/events/subscribers/base.rb', line 47

def subscribe_to(*event_names)
  @subscribed_events = event_names.flatten
end

.subscribe_to_pattern(pattern) ⇒ void

This method returns an undefined value.

Subscribe to events matching a pattern

Parameters:

  • pattern (Regexp)

    Pattern to match event names



56
57
58
# File 'lib/standard_id/events/subscribers/base.rb', line 56

def subscribe_to_pattern(pattern)
  @subscription_pattern = pattern
end

.subscribed_eventsArray<String>

Get the subscribed events

Returns:

  • (Array<String>)


64
65
66
# File 'lib/standard_id/events/subscribers/base.rb', line 64

def subscribed_events
  @subscribed_events || []
end

.subscription_patternRegexp?

Get the subscription pattern

Returns:

  • (Regexp, nil)


72
73
74
# File 'lib/standard_id/events/subscribers/base.rb', line 72

def subscription_pattern
  @subscription_pattern
end

Instance Method Details

#call(event) ⇒ Object

Process the event

Subclasses must implement this method.

Parameters:

Raises:

  • (NotImplementedError)

    If not implemented by subclass



143
144
145
# File 'lib/standard_id/events/subscribers/base.rb', line 143

def call(event)
  raise NotImplementedError, "#{self.class.name} must implement #call"
end

#handle(event) ⇒ void

This method returns an undefined value.

Handle an event

Override this method to add custom handling logic like async processing or error handling. By default, it calls the call method.

Parameters:



130
131
132
133
134
# File 'lib/standard_id/events/subscribers/base.rb', line 130

def handle(event)
  call(event)
rescue StandardError => e
  handle_error(e, event)
end

#handle_error(error, event) ⇒ Object

Handle errors during event processing

Override this method to customize error handling. By default, it logs the error and re-raises.

Parameters:

Raises:

  • (StandardError)

    Re-raises the error by default



156
157
158
159
160
161
# File 'lib/standard_id/events/subscribers/base.rb', line 156

def handle_error(error, event)
  StandardId.logger.error(
    "[StandardId::Events] Error in #{self.class.name} handling #{event.short_name}: #{error.message}"
  )
  raise error
end