Class: WebhookSystem::BaseEvent

Inherits:
Object
  • Object
show all
Includes:
PhModel
Defined in:
lib/webhook_system/base_event.rb

Overview

This is the class meant to be used as the base class for any Events sent through the Webhook system

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ BaseEvent

Returns a new instance of BaseEvent.



9
10
11
12
# File 'lib/webhook_system/base_event.rb', line 9

def initialize(*args, &block)
  super(*args, &block)
  @event_id = SecureRandom.uuid.freeze
end

Instance Attribute Details

#event_idObject (readonly)

Returns the value of attribute event_id.



14
15
16
# File 'lib/webhook_system/base_event.rb', line 14

def event_id
  @event_id
end

Class Method Details

.dispatch(args) ⇒ Object



46
47
48
# File 'lib/webhook_system/base_event.rb', line 46

def self.dispatch(args)
  WebhookSystem::Subscription.global.dispatch build(args)
end

.key_is_reserved?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/webhook_system/base_event.rb', line 42

def self.key_is_reserved?(key)
  key.to_s.in? %w[event event_id]
end

Instance Method Details

#as_jsonObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webhook_system/base_event.rb', line 30

def as_json
  result = {
    'event_name' => event_name,
    'event_id' => event_id,
  }
  each_attribute do |attribute_name, attribute_method|
    validate_attribute_name attribute_name
    result[attribute_name.to_s] = public_send(attribute_method).as_json
  end
  result.deep_stringify_keys
end

#event_nameObject



16
17
18
19
20
21
# File 'lib/webhook_system/base_event.rb', line 16

def event_name
  # :nocov:
  mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#event_name()'."
  raise with_caller_backtrace(RuntimeError.new(mesg), 2)
  # :nocov:
end

#payload_attributesObject



23
24
25
26
27
28
# File 'lib/webhook_system/base_event.rb', line 23

def payload_attributes
  # :nocov:
  mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#payload_attributes()'."
  raise with_caller_backtrace(RuntimeError.new(mesg), 2)
  # :nocov:
end