Class: StandardId::Events::Event
- Inherits:
-
Object
- Object
- StandardId::Events::Event
- 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
-
#finished_at ⇒ Time
readonly
When the event finished.
-
#name ⇒ String
readonly
The full namespaced event name.
-
#payload ⇒ Hash
readonly
The event payload data.
-
#started_at ⇒ Time
readonly
When the event started.
-
#transaction_id ⇒ String
readonly
Unique identifier for this event instance.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Convenience method to access payload values.
-
#duration_ms ⇒ Float?
Calculate the duration of the event in milliseconds.
-
#event_id ⇒ String?
Get the unique event ID.
-
#event_type ⇒ String?
Get the event type from the payload.
-
#initialize(name:, payload:, started_at: nil, finished_at: nil, transaction_id: nil) ⇒ Event
constructor
A new instance of Event.
-
#inspect ⇒ String
String representation for debugging.
-
#key?(key) ⇒ Boolean
Check if the payload contains a key.
-
#short_name ⇒ String
Get the event name without the namespace prefix.
-
#timestamp ⇒ String?
Get the event timestamp.
-
#to_h ⇒ Hash
Convert event to a hash representation.
-
#to_json(*args) ⇒ String
Convert event to JSON.
Constructor Details
#initialize(name:, payload:, started_at: nil, finished_at: nil, transaction_id: nil) ⇒ Event
Returns a new instance of 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_at ⇒ Time (readonly)
When the event finished
13 14 15 |
# File 'lib/standard_id/events/event.rb', line 13 def finished_at @finished_at end |
#name ⇒ String (readonly)
The full namespaced event name
13 14 15 |
# File 'lib/standard_id/events/event.rb', line 13 def name @name end |
#payload ⇒ Hash (readonly)
The event payload data
13 14 15 |
# File 'lib/standard_id/events/event.rb', line 13 def payload @payload end |
#started_at ⇒ Time (readonly)
When the event started
13 14 15 |
# File 'lib/standard_id/events/event.rb', line 13 def started_at @started_at end |
#transaction_id ⇒ String (readonly)
Unique identifier for this event instance
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
77 78 79 |
# File 'lib/standard_id/events/event.rb', line 77 def [](key) payload[key] end |
#duration_ms ⇒ Float?
Calculate the duration of the event in milliseconds
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_id ⇒ String?
Get the unique event ID
51 52 53 |
# File 'lib/standard_id/events/event.rb', line 51 def event_id payload[:event_id] end |
#event_type ⇒ String?
Get the event type from the payload
43 44 45 |
# File 'lib/standard_id/events/event.rb', line 43 def event_type payload[:event_type] end |
#inspect ⇒ String
String representation for debugging
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
86 87 88 |
# File 'lib/standard_id/events/event.rb', line 86 def key?(key) payload.key?(key) end |
#short_name ⇒ String
Get the event name without the namespace prefix
35 36 37 |
# File 'lib/standard_id/events/event.rb', line 35 def short_name name.to_s.delete_prefix("#{Events::NAMESPACE}.") end |
#timestamp ⇒ String?
Get the event timestamp
59 60 61 |
# File 'lib/standard_id/events/event.rb', line 59 def payload[:timestamp] end |
#to_h ⇒ Hash
Convert event to a hash representation
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
110 111 112 |
# File 'lib/standard_id/events/event.rb', line 110 def to_json(*args) to_h.to_json(*args) end |