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



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

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

.key_is_reserved?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/webhook_system/base_event.rb', line 38

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

Instance Method Details

#as_jsonObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/webhook_system/base_event.rb', line 26

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
# File 'lib/webhook_system/base_event.rb', line 16

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

#payload_attributesObject



21
22
23
24
# File 'lib/webhook_system/base_event.rb', line 21

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