Class: HookSniff::WebhookEvent
- Inherits:
-
Object
- Object
- HookSniff::WebhookEvent
- Defined in:
- lib/hooksniff/webhook_event.rb
Overview
Represents a parsed webhook event from HookSniff.
Direct Known Subclasses
EndpointCreatedEvent, EndpointDeletedEvent, EndpointDisabledEvent, EndpointEnabledEvent, EndpointUpdatedEvent, MessageAttemptExhaustedEvent, MessageAttemptFailingEvent, MessageAttemptRecoveredEvent
Constant Summary collapse
- EVENT_TYPE_MAP =
Map of known event types to their classes
{ "endpoint.created" => "EndpointCreatedEvent", "endpoint.updated" => "EndpointUpdatedEvent", "endpoint.deleted" => "EndpointDeletedEvent", "endpoint.enabled" => "EndpointEnabledEvent", "endpoint.disabled" => "EndpointDisabledEvent", "message.attempt.exhausted" => "MessageAttemptExhaustedEvent", "message.attempt.failing" => "MessageAttemptFailingEvent", "message.atattempt.failing" => "MessageAttemptFailingEvent", "message.attempt.recovered" => "MessageAttemptRecoveredEvent", "message.atattempt.recovered" => "MessageAttemptRecoveredEvent", }.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Event payload data (typed data class or Hash).
-
#event ⇒ String
readonly
Event type name (e.g., “endpoint.created”).
-
#timestamp ⇒ String
readonly
ISO 8601 timestamp string.
Class Method Summary collapse
-
.parse(data) ⇒ Object
Parse a webhook payload hash into a typed WebhookEvent.
- .parse_attempt(raw) ⇒ Object
- .parse_event_data(event_type, raw) ⇒ Object
- .parse_last_attempt(raw) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access data values with bracket notation (backward compat).
-
#event_type ⇒ Object
Alias for
event— the event type name. -
#get(key) ⇒ Object
Get a value from the data (Hash or typed object).
-
#initialize(event:, data:, timestamp:) ⇒ WebhookEvent
constructor
A new instance of WebhookEvent.
- #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
Check if key exists in data.
- #to_s ⇒ Object
Constructor Details
#initialize(event:, data:, timestamp:) ⇒ WebhookEvent
Returns a new instance of WebhookEvent.
134 135 136 137 138 |
# File 'lib/hooksniff/webhook_event.rb', line 134 def initialize(event:, data:, timestamp:) @event = event @data = data @timestamp = end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns event payload data (typed data class or Hash).
129 130 131 |
# File 'lib/hooksniff/webhook_event.rb', line 129 def data @data end |
#event ⇒ String (readonly)
Returns event type name (e.g., “endpoint.created”).
126 127 128 |
# File 'lib/hooksniff/webhook_event.rb', line 126 def event @event end |
#timestamp ⇒ String (readonly)
Returns ISO 8601 timestamp string.
132 133 134 |
# File 'lib/hooksniff/webhook_event.rb', line 132 def @timestamp end |
Class Method Details
.parse(data) ⇒ Object
Parse a webhook payload hash into a typed WebhookEvent.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/hooksniff/webhook_event.rb', line 191 def self.parse(data) event_type = data[:event] || data["event"] || data[:eventType] || data["eventType"] || "" raw_data = data[:data] || data["data"] || {} = data[:timestamp] || data["timestamp"] || "" parsed_data = parse_event_data(event_type, raw_data) class_name = EVENT_TYPE_MAP[event_type] event_class = class_name ? const_get(class_name) : WebhookEvent event_class.new(event: event_type, data: parsed_data, timestamp: ) end |
.parse_attempt(raw) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/hooksniff/webhook_event.rb', line 276 def self.parse_attempt(raw) AttemptInfo.new( id: raw[:id] || raw["id"] || "", timestamp: raw[:timestamp] || raw["timestamp"] || "", response_status_code: raw[:responseStatusCode] || raw["responseStatusCode"] || raw[:response_status_code] || raw["response_status_code"] || 0 ) end |
.parse_event_data(event_type, raw) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/hooksniff/webhook_event.rb', line 205 def self.parse_event_data(event_type, raw) case event_type when "endpoint.created" EndpointCreatedEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", endpoint_id: raw[:endpointId] || raw["endpointId"] || raw[:endpoint_id] || raw["endpoint_id"] || "", app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "endpoint.updated" EndpointUpdatedEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", endpoint_id: raw[:endpointId] || raw["endpointId"] || raw[:endpoint_id] || raw["endpoint_id"] || "", app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "endpoint.deleted" EndpointDeletedEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", endpoint_id: raw[:endpointId] || raw["endpointId"] || raw[:endpoint_id] || raw["endpoint_id"] || "", app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "endpoint.enabled" EndpointEnabledEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", endpoint_id: raw[:endpointId] || raw["endpointId"] || raw[:endpoint_id] || raw["endpoint_id"] || "", app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "endpoint.disabled" EndpointDisabledEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", endpoint_id: raw[:endpointId] || raw["endpointId"] || raw[:endpoint_id] || raw["endpoint_id"] || "", app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"], fail_since: raw[:failSince] || raw["failSince"] || raw[:fail_since] || raw["fail_since"], trigger: raw[:trigger] || raw["trigger"] ) when "message.attempt.exhausted" last_raw = raw[:lastAttempt] || raw["lastAttempt"] || raw[:last_attempt] || raw["last_attempt"] || {} MessageAttemptExhaustedEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", msg_id: raw[:msgId] || raw["msgId"] || raw[:msg_id] || raw["msg_id"] || "", last_attempt: parse_last_attempt(last_raw), app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "message.attempt.failing", "message.atattempt.failing" attempt_raw = raw[:attempt] || raw["attempt"] || {} MessageAttemptFailingEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", msg_id: raw[:msgId] || raw["msgId"] || raw[:msg_id] || raw["msg_id"] || "", attempt: parse_attempt(attempt_raw), app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) when "message.atattempt.recovered", "message.attempt.recovered" attempt_raw = raw[:attempt] || raw["attempt"] || {} MessageAttemptRecoveredEventData.new( app_id: raw[:appId] || raw["appId"] || raw[:app_id] || raw["app_id"] || "", msg_id: raw[:msgId] || raw["msgId"] || raw[:msg_id] || raw["msg_id"] || "", attempt: parse_attempt(attempt_raw), app_uid: raw[:appUid] || raw["appUid"] || raw[:app_uid] || raw["app_uid"] ) else raw end end |
.parse_last_attempt(raw) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/hooksniff/webhook_event.rb', line 268 def self.parse_last_attempt(raw) LastAttemptInfo.new( id: raw[:id] || raw["id"] || "", timestamp: raw[:timestamp] || raw["timestamp"] || "", response_status_code: raw[:responseStatusCode] || raw["responseStatusCode"] || raw[:response_status_code] || raw["response_status_code"] || 0 ) end |
Instance Method Details
#[](key) ⇒ Object
Access data values with bracket notation (backward compat).
155 156 157 |
# File 'lib/hooksniff/webhook_event.rb', line 155 def [](key) get(key) end |
#event_type ⇒ Object
Alias for event — the event type name.
141 142 143 |
# File 'lib/hooksniff/webhook_event.rb', line 141 def event_type @event end |
#get(key) ⇒ Object
Get a value from the data (Hash or typed object).
146 147 148 149 150 151 152 |
# File 'lib/hooksniff/webhook_event.rb', line 146 def get(key) if @data.is_a?(Hash) @data[key.to_s] || @data[key.to_sym] else @data.respond_to?(key.to_sym) ? @data.send(key.to_sym) : nil end end |
#inspect ⇒ Object
172 173 174 |
# File 'lib/hooksniff/webhook_event.rb', line 172 def inspect to_s end |
#key?(key) ⇒ Boolean
Check if key exists in data.
160 161 162 163 164 165 166 |
# File 'lib/hooksniff/webhook_event.rb', line 160 def key?(key) if @data.is_a?(Hash) @data.key?(key.to_s) || @data.key?(key.to_sym) else @data.respond_to?(key.to_sym) end end |
#to_s ⇒ Object
168 169 170 |
# File 'lib/hooksniff/webhook_event.rb', line 168 def to_s "#<#{self.class.name} event=#{@event} timestamp=#{@timestamp}>" end |