Class: Chook::HandledEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/chook/event/handled_event.rb,
lib/chook/event/handled_event/handlers.rb

Overview

the server class

Defined Under Namespace

Modules: Handlers

Constant Summary

Constants inherited from Event

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

Instance Attribute Summary collapse

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(raw_event_json) ⇒ HandledEvent

Handled Events are always built from raw_json.


117
118
119
# File 'lib/chook/event/handled_event.rb', line 117

def initialize(raw_event_json)
  super raw_json: raw_event_json
end

Instance Attribute Details

#handlersArray<Proc,Pathname> (readonly)

Returns 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.

Returns:

  • (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.


111
112
113
# File 'lib/chook/event/handled_event.rb', line 111

def handlers
  @handlers
end

Class Method Details

.generate_classesvoid

This method returns an undefined value.

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.


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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

.parse_event(raw_event_json) ⇒ JSSWebHooks::Event subclass

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

Parameters:

  • raw_event_json (String)

    The JSON http POST content from the JSS

Returns:

  • (JSSWebHooks::Event subclass)

    the Event subclass matching the event


97
98
99
100
101
102
# 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

Instance Method Details

#event_class_nameObject

init


121
122
123
# File 'lib/chook/event/handled_event.rb', line 121

def event_class_name
  self.class.const_get(Chook::Event::EVENT_NAME_CONST)
end

#handleObject

Run all the general handlers for this event class


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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) ⇒ Object

run a single handler specified by filename


148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object


170
171
172
173
# 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

#loggerObject


175
176
177
# File 'lib/chook/event/handled_event.rb', line 175

def logger
  @logger ||= Chook::HandledEventLogger.new self
end

#pipe_to_executable(handler) ⇒ Object

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


163
164
165
166
167
168
# 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