class WebhookSystem::BaseEvent

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

Attributes

event_id[R]

Public Class Methods

dispatch(args) click to toggle source
# File lib/webhook_system/base_event.rb, line 46
def self.dispatch(args)
  WebhookSystem::Subscription.global.dispatch build(args)
end
key_is_reserved?(key) click to toggle source
# File lib/webhook_system/base_event.rb, line 42
def self.key_is_reserved?(key)
  key.to_s.in? %w[event event_id]
end
new(*args, &block) click to toggle source
Calls superclass method
# File lib/webhook_system/base_event.rb, line 9
def initialize(*args, &block)
  super(*args, &block)
  @event_id = SecureRandom.uuid.freeze
end

Public Instance Methods

as_json() click to toggle source
# 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_name() click to toggle source
# 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_attributes() click to toggle source
# 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

Private Instance Methods

each_attribute() { |attribute_name, attribute_name| ... } click to toggle source
# File lib/webhook_system/base_event.rb, line 67
def each_attribute(&block)
  case payload_attributes
  when Array
    payload_attributes.each do |attribute_name|
      yield(attribute_name, attribute_name)
    end
  when Hash
    payload_attributes.each(&block)
  else
    # :nocov:
    raise ArgumentError, "don't know how to deal with payload_attributes: #{payload_attributes.inspect}"
    # :nocov:
  end
end
validate_attribute_name(key) click to toggle source
# File lib/webhook_system/base_event.rb, line 59
def validate_attribute_name(key)
  return unless self.class.key_is_reserved?(key)

  message = "#{self.class.name} should not be defining an attribute named #{key} since its reserved"
  raise ArgumentError, message

end
with_caller_backtrace(exception, backtrack = 2) click to toggle source
# File lib/webhook_system/base_event.rb, line 52
def with_caller_backtrace(exception, backtrack = 2)
  # :nocov:
  exception.set_backtrace(caller[backtrack..])
  exception
  # :nocov:
end