Class: Attio::WebhookUtils::Event
- Inherits:
-
Object
- Object
- Attio::WebhookUtils::Event
- Defined in:
- lib/attio/webhook/event.rb
Overview
Represents a webhook event payload
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#occurred_at ⇒ Object
readonly
Returns the value of attribute occurred_at.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#changes ⇒ Object
Get changes from updated events.
-
#created_event? ⇒ Boolean
Check if this is a created event.
-
#deleted_event? ⇒ Boolean
Check if this is a deleted event.
-
#initialize(payload) ⇒ Event
constructor
A new instance of Event.
-
#list_entry_event? ⇒ Boolean
Check if this is a list entry event.
-
#note_event? ⇒ Boolean
Check if this is a note event.
-
#object_type ⇒ Object
Get the object type from the event data.
-
#present? ⇒ Boolean
Add present? method to match Rails expectations.
-
#record ⇒ Object
Get the record from the event data.
-
#record_data ⇒ Object
Get the record data.
-
#record_event? ⇒ Boolean
Check if this is a record event.
-
#record_id ⇒ Object
Get the record ID.
-
#task_event? ⇒ Boolean
Check if this is a task event.
-
#to_h ⇒ Object
Convert to hash.
-
#to_json(*args) ⇒ Object
Convert event back to JSON.
-
#updated_event? ⇒ Boolean
Check if this is an updated event.
Constructor Details
#initialize(payload) ⇒ Event
Returns a new instance of Event.
9 10 11 12 13 14 15 |
# File 'lib/attio/webhook/event.rb', line 9 def initialize(payload) @raw_data = payload.is_a?(String) ? JSON.parse(payload) : payload @id = @raw_data["id"] || @raw_data[:id] @type = @raw_data["type"] || @raw_data[:type] @occurred_at = (@raw_data["occurred_at"] || @raw_data[:occurred_at]) @data = @raw_data["data"] || @raw_data[:data] || {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/attio/webhook/event.rb', line 7 def data @data end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/attio/webhook/event.rb', line 7 def id @id end |
#occurred_at ⇒ Object (readonly)
Returns the value of attribute occurred_at.
7 8 9 |
# File 'lib/attio/webhook/event.rb', line 7 def occurred_at @occurred_at end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
7 8 9 |
# File 'lib/attio/webhook/event.rb', line 7 def raw_data @raw_data end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/attio/webhook/event.rb', line 7 def type @type end |
Instance Method Details
#changes ⇒ Object
Get changes from updated events
41 42 43 |
# File 'lib/attio/webhook/event.rb', line 41 def changes @data["changes"] || @data[:changes] end |
#created_event? ⇒ Boolean
Check if this is a created event
56 57 58 |
# File 'lib/attio/webhook/event.rb', line 56 def created_event? type&.end_with?(".created") end |
#deleted_event? ⇒ Boolean
Check if this is a deleted event
66 67 68 |
# File 'lib/attio/webhook/event.rb', line 66 def deleted_event? type&.end_with?(".deleted") end |
#list_entry_event? ⇒ Boolean
Check if this is a list entry event
71 72 73 |
# File 'lib/attio/webhook/event.rb', line 71 def list_entry_event? type&.start_with?("list_entry.") end |
#note_event? ⇒ Boolean
Check if this is a note event
76 77 78 |
# File 'lib/attio/webhook/event.rb', line 76 def note_event? type&.start_with?("note.") end |
#object_type ⇒ Object
Get the object type from the event data
18 19 20 |
# File 'lib/attio/webhook/event.rb', line 18 def object_type @data["object"] || @data[:object] end |
#present? ⇒ Boolean
Add present? method to match Rails expectations
46 47 48 |
# File 'lib/attio/webhook/event.rb', line 46 def present? true # Events are always present if they exist end |
#record ⇒ Object
Get the record from the event data
23 24 25 |
# File 'lib/attio/webhook/event.rb', line 23 def record @data["record"] || @data[:record] end |
#record_data ⇒ Object
Get the record data
36 37 38 |
# File 'lib/attio/webhook/event.rb', line 36 def record_data record || {} end |
#record_event? ⇒ Boolean
Check if this is a record event
51 52 53 |
# File 'lib/attio/webhook/event.rb', line 51 def record_event? type&.start_with?("record.") end |
#record_id ⇒ Object
Get the record ID
28 29 30 31 32 33 |
# File 'lib/attio/webhook/event.rb', line 28 def record_id record_data = record return nil unless record_data record_data["id"] || record_data[:id] end |
#task_event? ⇒ Boolean
Check if this is a task event
81 82 83 |
# File 'lib/attio/webhook/event.rb', line 81 def task_event? type&.start_with?("task.") end |
#to_h ⇒ Object
Convert to hash
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/attio/webhook/event.rb', line 86 def to_h { id: id, type: type, occurred_at: occurred_at&.iso8601, object_type: object_type, record_id: record_id, record_data: record_data, changes: changes, data: data }.compact end |
#to_json(*args) ⇒ Object
Convert event back to JSON
100 101 102 |
# File 'lib/attio/webhook/event.rb', line 100 def to_json(*args) @raw_data.to_json(*args) end |
#updated_event? ⇒ Boolean
Check if this is an updated event
61 62 63 |
# File 'lib/attio/webhook/event.rb', line 61 def updated_event? type&.end_with?(".updated") end |