Class: Chook::TestEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/chook/event/test_event.rb

Overview

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.

Constant Summary collapse

EVENT_ATTRIBUTES =
%w(webhook_id webhook_name subject).freeze

Constants inherited from Event

Event::EVENTS, Event::EVENT_NAME_CONST, Event::SUBJECT_CLASS_CONST

Instance Attribute Summary

Attributes inherited from Event

#id, #parsed_json, #raw_json, #subject, #webhook_id, #webhook_name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_data = nil) ⇒ TestEvent

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

Parameters:

  • event_data (Hash) (defaults to: nil)

    nil or Hash



127
128
129
130
131
132
133
134
135
136
# 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

Class Method Details

.generate_classesvoid

This method returns an undefined value.

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

Instance Method Details

#fire(server_url) ⇒ void

This method returns an undefined value.

fire

Parameters:

  • server_url (String)

    The URL of a server that can handle an Event



112
113
114
115
116
117
118
119
# 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_hashHash

json_hash Used by the fire method

Returns:

  • (Hash)

    A JSON Event payload formatted as a Hash.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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