Class: StandardId::Events::Event

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

Overview

Event object that wraps ActiveSupport::Notifications event data

Provides a clean interface for accessing event information in subscribers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, payload:, started_at: nil, finished_at: nil, transaction_id: nil) ⇒ Event

Returns a new instance of Event.

Parameters:

  • name (String)

    The full namespaced event name

  • payload (Hash)

    The event payload

  • started_at (Time) (defaults to: nil)

    When the event started

  • finished_at (Time) (defaults to: nil)

    When the event finished

  • transaction_id (String) (defaults to: nil)

    Unique identifier for the event



21
22
23
24
25
26
27
# File 'lib/standard_id/events/event.rb', line 21

def initialize(name:, payload:, started_at: nil, finished_at: nil, transaction_id: nil)
  @name = name
  @payload = payload.with_indifferent_access
  @started_at = started_at
  @finished_at = finished_at
  @transaction_id = transaction_id
end

Instance Attribute Details

#finished_atTime (readonly)

When the event finished

Returns:

  • (Time)

    the current value of finished_at



13
14
15
# File 'lib/standard_id/events/event.rb', line 13

def finished_at
  @finished_at
end

#nameString (readonly)

The full namespaced event name

Returns:

  • (String)

    the current value of name



13
14
15
# File 'lib/standard_id/events/event.rb', line 13

def name
  @name
end

#payloadHash (readonly)

The event payload data

Returns:

  • (Hash)

    the current value of payload



13
14
15
# File 'lib/standard_id/events/event.rb', line 13

def payload
  @payload
end

#started_atTime (readonly)

When the event started

Returns:

  • (Time)

    the current value of started_at



13
14
15
# File 'lib/standard_id/events/event.rb', line 13

def started_at
  @started_at
end

#transaction_idString (readonly)

Unique identifier for this event instance

Returns:

  • (String)

    the current value of transaction_id



13
14
15
# File 'lib/standard_id/events/event.rb', line 13

def transaction_id
  @transaction_id
end

Instance Method Details

#[](key) ⇒ Object

Convenience method to access payload values

Parameters:

  • key (Symbol, String)

    The payload key

Returns:

  • (Object)

    The value from the payload



77
78
79
# File 'lib/standard_id/events/event.rb', line 77

def [](key)
  payload[key]
end

#duration_msFloat?

Calculate the duration of the event in milliseconds

Returns:

  • (Float, nil)

    Duration in milliseconds, or nil if timing not available



67
68
69
70
# File 'lib/standard_id/events/event.rb', line 67

def duration_ms
  return nil unless started_at && finished_at
  (finished_at - started_at) * 1000
end

#event_idString?

Get the unique event ID

Returns:

  • (String, nil)

    The event UUID



51
52
53
# File 'lib/standard_id/events/event.rb', line 51

def event_id
  payload[:event_id]
end

#event_typeString?

Get the event type from the payload

Returns:

  • (String, nil)

    The event type



43
44
45
# File 'lib/standard_id/events/event.rb', line 43

def event_type
  payload[:event_type]
end

#inspectString

String representation for debugging

Returns:

  • (String)


118
119
120
# File 'lib/standard_id/events/event.rb', line 118

def inspect
  "#<#{self.class.name} name=#{name} event_id=#{event_id} duration_ms=#{duration_ms}>"
end

#key?(key) ⇒ Boolean

Check if the payload contains a key

Parameters:

  • key (Symbol, String)

    The payload key

Returns:

  • (Boolean)


86
87
88
# File 'lib/standard_id/events/event.rb', line 86

def key?(key)
  payload.key?(key)
end

#short_nameString

Get the event name without the namespace prefix

Examples:

event.short_name # => "authentication.attempt.succeeded"

Returns:

  • (String)

    The short event name



35
36
37
# File 'lib/standard_id/events/event.rb', line 35

def short_name
  name.to_s.delete_prefix("#{Events::NAMESPACE}.")
end

#timestampString?

Get the event timestamp

Returns:

  • (String, nil)

    ISO8601 formatted timestamp



59
60
61
# File 'lib/standard_id/events/event.rb', line 59

def timestamp
  payload[:timestamp]
end

#to_hHash

Convert event to a hash representation

Returns:

  • (Hash)

    The event as a hash



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/standard_id/events/event.rb', line 94

def to_h
  {
    name: name,
    short_name: short_name,
    transaction_id: transaction_id,
    started_at: started_at&.iso8601,
    finished_at: finished_at&.iso8601,
    duration_ms: duration_ms,
    payload: payload.to_h
  }
end

#to_json(*args) ⇒ String

Convert event to JSON

Returns:

  • (String)

    JSON representation



110
111
112
# File 'lib/standard_id/events/event.rb', line 110

def to_json(*args)
  to_h.to_json(*args)
end