Class: Attio::WebhookUtils::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/webhook/event.rb

Overview

Represents a webhook event payload

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = parse_timestamp(@raw_data["occurred_at"] || @raw_data[:occurred_at])
  @data = @raw_data["data"] || @raw_data[:data] || {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/attio/webhook/event.rb', line 7

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/attio/webhook/event.rb', line 7

def id
  @id
end

#occurred_atObject (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_dataObject (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

#typeObject (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

#changesObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


76
77
78
# File 'lib/attio/webhook/event.rb', line 76

def note_event?
  type&.start_with?("note.")
end

#object_typeObject

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/attio/webhook/event.rb', line 46

def present?
  true # Events are always present if they exist
end

#recordObject

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_dataObject

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

Returns:

  • (Boolean)


51
52
53
# File 'lib/attio/webhook/event.rb', line 51

def record_event?
  type&.start_with?("record.")
end

#record_idObject

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

Returns:

  • (Boolean)


81
82
83
# File 'lib/attio/webhook/event.rb', line 81

def task_event?
  type&.start_with?("task.")
end

#to_hObject

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/attio/webhook/event.rb', line 61

def updated_event?
  type&.end_with?(".updated")
end