Sha256: 448d8df99b9a966cec0075237bb7496fb627e65f2578a58c84e088b6bfc5f855
Contents?: true
Size: 1.76 KB
Versions: 6
Compression:
Stored size: 1.76 KB
Contents
module WebhookSystem # This is the class meant to be used as the base class for any Events sent through the Webhook system class BaseEvent include PhModel def initialize(*args, &block) super(*args, &block) @event_id = SecureRandom.uuid.freeze end attr_reader :event_id def event_name mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#event_name()'." raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller } end def payload_attributes mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#payload_attributes()'." raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller } end 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 def self.key_is_reserved?(key) key.to_s.in? %w(event event_id) end private def validate_attribute_name(key) if 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 end 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 raise ArgumetError, "don't know how to deal with payload_attributes: #{payload_attributes.inspect}" end end end end
Version data entries
6 entries across 6 versions & 1 rubygems