Class: HookSniff::WebhookEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/hooksniff/webhook_event.rb

Overview

Represents a parsed webhook event from HookSniff.

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

Class Method Summary collapse

Instance Method Summary collapse

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 = timestamp
end

Instance Attribute Details

#dataObject (readonly)

Returns event payload data (typed data class or Hash).

Returns:

  • (Object)

    event payload data (typed data class or Hash)



129
130
131
# File 'lib/hooksniff/webhook_event.rb', line 129

def data
  @data
end

#eventString (readonly)

Returns event type name (e.g., “endpoint.created”).

Returns:

  • (String)

    event type name (e.g., “endpoint.created”)



126
127
128
# File 'lib/hooksniff/webhook_event.rb', line 126

def event
  @event
end

#timestampString (readonly)

Returns ISO 8601 timestamp string.

Returns:

  • (String)

    ISO 8601 timestamp string



132
133
134
# File 'lib/hooksniff/webhook_event.rb', line 132

def timestamp
  @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"] || {}
  timestamp = 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: 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_typeObject

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

#inspectObject



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.

Returns:

  • (Boolean)


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_sObject



168
169
170
# File 'lib/hooksniff/webhook_event.rb', line 168

def to_s
  "#<#{self.class.name} event=#{@event} timestamp=#{@timestamp}>"
end