class Chook::HandledEvent

An event that has been recieved and needs to be handled.

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

All constants, methods, and attributes that are common to HandledEvent classes are defined here, including the interaction with the Handlers module.

Subclasses are automatically generated from the keys and values of Chook::Event::EVENTS

Each subclass will have a constant SUBJECT_CLASS containing the class of their subject attribute.

the server class

Attributes

handlers[R]

@return [Array<Proc,Pathname>] the handlers defined for this event.

Each is either a proc, in which case it is called with this
instance as its sole paramter, or its a Pathname to an executable
file, in which case the @raw_json is passed to its stdin.
See the Chook::HandledEvent::Handlers module.

Public Class Methods

generate_classes() click to toggle source

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

@return [void]

# File lib/chook/event/handled_event.rb, line 70
def self.generate_classes
  Chook::Event::EVENTS.each do |class_name, subject|
    next if Chook::HandledEvents.const_defined? class_name

    # make the new HandledEvent subclass
    the_class = Class.new(Chook::HandledEvent)

    # Set its EVENT_NAME constant, which is used
    # for finding it's handlers, among other things.
    the_class.const_set Chook::Event::EVENT_NAME_CONST, class_name

    # Set its SUBJECT_CLASS constant to the appropriate
    # class in the HandledSubjects module.
    the_class.const_set Chook::Event::SUBJECT_CLASS_CONST, Chook::HandledSubjects.const_get(subject)

    # Add the new class to the HandledEvents module.
    Chook::HandledEvents.const_set(class_name, the_class)
  end # each classname, subject
end
new(raw_event_json) click to toggle source

Handled Events are always built from raw_json.

Calls superclass method Chook::Event::new
# File lib/chook/event/handled_event.rb, line 117
def initialize(raw_event_json)
  super raw_json: raw_event_json
end
parse_event(raw_event_json) click to toggle source

Given the raw json from the JSS webhook, create an object of the correct Event subclass

@param [String] raw_event_json The JSON http POST content from the JSS

@return [JSSWebHooks::Event subclass] the Event subclass matching the event

# File lib/chook/event/handled_event.rb, line 97
def self.parse_event(raw_event_json)
  return nil if raw_event_json.to_s.empty?
  event_json = JSON.parse(raw_event_json, symbolize_names: true)
  event_name = event_json[:webhook][:webhookEvent]
  Chook::HandledEvents.const_get(event_name).new raw_event_json
end

Public Instance Methods

event_class_name() click to toggle source
# File lib/chook/event/handled_event.rb, line 121
def event_class_name
  self.class.const_get(Chook::Event::EVENT_NAME_CONST)
end
handle() click to toggle source

Run all the general handlers for this event class

# File lib/chook/event/handled_event.rb, line 127
def handle
  handlers = Handlers.handlers[event_class_name]
  return "No handlers loaded for #{event_class_name} events" unless handlers.is_a? Array

  handlers.each do |handler|
    case handler
    when Pathname
      pipe_to_executable handler
    when Object
      handle_with_proc handler
    end # case
  end # @handlers.each do |handler|

  # the handle method should return a string,
  # which is the body of the HTTP result for
  # POSTing the event
  "Processed by #{handlers.count} general handlers"
end
handle_by_name(handler_to_run) click to toggle source

run a single handler specified by filename

# File lib/chook/event/handled_event.rb, line 148
def handle_by_name(handler_to_run)
  handler = Handlers.named_handlers[handler_to_run]
  return "No named handler '#{handler_to_run}'" unless handler

  if handler.is_a? Pathname
    pipe_to_executable handler
  else
    handle_with_proc handler
  end # if
  "Processed by named handler '#{handler_to_run}'"
end
handle_with_proc(handler) click to toggle source
# File lib/chook/event/handled_event.rb, line 170
def handle_with_proc(handler)
  logger.debug "INTERNAL: Running Handler defined in #{handler.handler_file}"
  _thread = Thread.new { handler.handle self }
end
logger() click to toggle source
# File lib/chook/event/handled_event.rb, line 175
def logger
  @logger ||= Chook::HandledEventLogger.new self
end
pipe_to_executable(handler) click to toggle source

TODO: these threads will die midstream when the server stops. Find a way to .join them or otherwise clean them up.

# File lib/chook/event/handled_event.rb, line 163
def pipe_to_executable(handler)
  logger.debug "EXTERNAL: Sending JSON to stdin of '#{handler.basename}'"
  _thread = Thread.new do
    IO.popen([handler.to_s], 'w') { |h| h.puts @raw_json }
  end
end