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

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


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

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.


114
115
116
# File 'lib/chook/event/handled_event.rb', line 114

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.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chook/event/handled_event.rb', line 74

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


101
102
103
104
105
# File 'lib/chook/event/handled_event.rb', line 101

def self.parse_event(raw_event_json)
  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

#handleObject

init


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chook/event/handled_event.rb', line 124

def handle
  handlers = Handlers.handlers[self.class.const_get(Chook::Event::EVENT_NAME_CONST)]
  handlers.each do |handler|
    case handler
    when Pathname
      pipe_to_executable handler
    when Proc
      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} handlers\n"
end

#handle_with_proc(handler) ⇒ Object


148
149
150
# File 'lib/chook/event/handled_event.rb', line 148

def handle_with_proc(handler)
  _thread = Thread.new { handler.call self }
end

#pipe_to_executable(handler) ⇒ Object

TODO: Add something here that cleans up old threads and forks


142
143
144
145
146
# File 'lib/chook/event/handled_event.rb', line 142

def pipe_to_executable(handler)
  _thread = Thread.new do
    IO.popen([handler.to_s], 'w') { |h| h.puts @raw_json }
  end
end