class Chook::TestEvent

An event that will be sent to a Webhook Server, simulating one from the JSS.

This is the parent class to all of the classes in the Chook::TestEvents module, which are dynamically defined when this file is loaded.

All constants, methods, and attributes that are common to TestEvent classes are defined here.

Constants

EVENT_ATTRIBUTES

Public Class Methods

generate_classes() click to toggle source

For each event type in Chook::Event::EVENTS.keys generate a TestEvent class for it, set its SUBJECT_CLASS constant and add it to the TestEvents module.

@return [void]

# File lib/chook/event/test_event.rb, line 48
def self.generate_classes
  Chook::Event::EVENTS.each do |classname, subject|
    next if Chook::TestEvents.const_defined? classname
    # make the new TestEvent subclass
    new_class = Class.new(Chook::TestEvent) do
      # Setters & Getters
      EVENT_ATTRIBUTES.each do |attribute|
        # Getter
        attr_reader attribute
        # Setter
        if attribute == 'subject'
          define_method("#{attribute}=") do |new_val|
            raise "Invalid TestSubject: Chook::TestEvents::#{classname} requires a Chook::TestSubjects::#{EVENTS[classname]}" unless Chook::Validators.send(:valid_test_subject, classname, new_val)
            instance_variable_set(('@' + attribute.to_s), new_val)
          end # end define_method
        else
          define_method("#{attribute}=") do |new_val|
            instance_variable_set(('@' + attribute.to_s), new_val)
          end # end define_method
        end
      end # end EVENT_ATTRIBUTES.each do |attribute|
    end # end new_class

    # Set its EVENT_NAME constant
    new_class.const_set Chook::TestEvent::EVENT_NAME_CONST, classname

    # Set its SUBJECT_CLASS constant to the appropriate
    # class in the TestEvents module.
    new_class.const_set Chook::TestEvent::SUBJECT_CLASS_CONST, Chook::TestSubjects.const_get(subject)

    # Add the new class to the HandledEvents module.
    Chook::TestEvents.const_set(classname, new_class)
  end # each classname, subject
end
new(event_data = nil) click to toggle source

initialize The optional argument is a Hash with the appropriate keys defiend

@param [Hash] event_data nil or Hash @return [TestEvent] A new TestEvents subclass object

# File lib/chook/event/test_event.rb, line 127
def initialize(event_data = nil)
  if event_data
    event_data.each do |key, value|
      next unless EVENT_ATTRIBUTES.include? key
      instance_variable_set(('@' + key.to_s), value)
    end # event_data.each
  else
    EVENT_ATTRIBUTES.each { |attribute| instance_variable_set(('@' + attribute.to_s), nil) }
  end # end if event_data
end

Public Instance Methods

fire(server_url) click to toggle source

fire

@param [String] server_url The URL of a server that can handle an Event @return [void]

# File lib/chook/event/test_event.rb, line 112
def fire(server_url)
  raise 'Please provide a destination server URL' unless server_url
  uri = URI.parse(server_url)
  raise 'Please provide a valid destination server URL' if uri.host.nil?
  data = json_hash.to_json # This is the structural equivalent of the Chook::Event @raw_json form
  http_connection = Net::HTTP.new uri.host, uri.port
  http_connection.post(uri, data)
end
json_hash() click to toggle source

json_hash Used by the fire method

@return [Hash] A JSON Event payload formatted as a Hash.

# File lib/chook/event/test_event.rb, line 88
def json_hash
  raw_hash_form = {}
  raw_hash_form['webhook'.to_sym] = { 'webhookEvent'.to_sym => self.class.to_s.split('::')[-1] }
  EVENT_ATTRIBUTES.each do |json_attribute|
    next if json_attribute.include? 'json'
    if json_attribute == 'subject'
      raw_hash_form['event'.to_sym] = instance_variable_get('@' + json_attribute).json_hash
    else
      json_hash_attribute = json_attribute.split('webhook_')[1] || json_attribute
      nested_hash = Hash.new do |hash, key|
        hash[key] = {}
      end # end nested_hash
      nested_hash[json_hash_attribute.to_sym] = instance_variable_get('@' + json_attribute)
      raw_hash_form['webhook'.to_sym][nested_hash.keys[0]] = nested_hash[nested_hash.keys[0]]
    end
  end # end EVENT_ATTRIBUTES.each do |json_attribute|
  raw_hash_form # This is the structural equivalent of the Chook::Event @json_hash form
end