Sha256: 7408a27a8b88ac4ed192ec8fb0502e314db72ed656ccd301204b37354f412dc4

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 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 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' => event_name }
      each_attribute do |attribute_name, attribute_method|
        result[attribute_name.to_s] = public_send(attribute_method).as_json
      end
      result
    end

    private

    def each_attribute
      case payload_attributes
      when Array
        payload_attributes.each do |attribute_name|
          yield(attribute_name, attribute_name)
        end
      when Hash
        payload_attributes.each
      else
        raise ArgumetError, "don't know how to deal with payload_attributes: #{payload_attributes.inspect}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webhook_system-0.1.0 lib/webhook_system/base_event.rb
webhook_system-0.0.1 lib/webhook_system/base_event.rb