Class: JSSWebHooks::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/jss/webhooks/event.rb,
lib/jss/webhooks/event/webhook.rb,
lib/jss/webhooks/event/handlers.rb

Overview

The superclass of all webhook events.

Almost all processing happens here in the Superclass. The Subclasses define specfic-event-related details. As such, the Subclasses must define these constants:

HANDLER_KEY = The JSSWebHooks::Configuration key pointing to the handler

file for this event. See Event#handle for details.

OBJECT_CLASS = the JSSWebHooks::EventObjects (q.v.) class representing the

event object with this event

Defined Under Namespace

Modules: Handlers

Constant Summary collapse

WEBHOOK_ATTRIBUTES =
[
  :id,
  :name,
  :webhookEvent
].freeze
WebHook =
ImmutableStruct.new(*WEBHOOK_ATTRIBUTES)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_json, raw_json) ⇒ Event

Returns a new instance of Event.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jss/webhooks/event.rb', line 86

def initialize(event_json, raw_json)
  @event_json = event_json
  @raw_json = raw_json

  # make a WebHook instance out of the webhook data for the event
  @webhook = WebHook.new(event_json[:webhook])

  # make an Event Objects class instance for the event object that
  # came with this event.
  @event_object = self.class::OBJECT_CLASS.new(event_json[:event])

  # An array of handlers for this event type.
  @handlers = JSSWebHooks::Event::Handlers.event_handlers[self.class::EVENT_NAME]
  @handlers ||= []
end

Instance Attribute Details

#event_jsonHash (readonly)

Returns The parsed JSON recieved from the JSS.

Returns:

  • (Hash)

    The parsed JSON recieved from the JSS



75
76
77
# File 'lib/jss/webhooks/event.rb', line 75

def event_json
  @event_json
end

#event_objectJSSWebHooks::EventObjects class (readonly)

Returns The event object sent with the event.

Returns:



81
82
83
# File 'lib/jss/webhooks/event.rb', line 81

def event_object
  @event_object
end

#handlersArray<Proc,Pathname> (readonly)

Returns the handlers defined for this event.

Returns:



84
85
86
# File 'lib/jss/webhooks/event.rb', line 84

def handlers
  @handlers
end

#raw_jsonString (readonly)

Returns the raw JSON recieved from the JSS.

Returns:

  • (String)

    the raw JSON recieved from the JSS



72
73
74
# File 'lib/jss/webhooks/event.rb', line 72

def raw_json
  @raw_json
end

#webhookJSSWebHooks::WebHook (readonly)

Returns The webhook in the JSS that caused this event.

Returns:

  • (JSSWebHooks::WebHook)

    The webhook in the JSS that caused this event



78
79
80
# File 'lib/jss/webhooks/event.rb', line 78

def webhook
  @webhook
end

Class Method Details

.event_to_class_namesHash{String => Class} a mapping of Event names as they come from the JSS to the JSSWebHooks::Event subclasses that represent them

Getter for @event_to_class_names

Returns:

  • (Hash{String => Class} a mapping of Event names as they come from the JSS to the JSSWebHooks::Event subclasses that represent them)

    Hash=> Class a mapping of Event names as they come from the JSS to the JSSWebHooks::Event subclasses that represent them



54
55
56
# File 'lib/jss/webhooks/event.rb', line 54

def self.event_to_class_names
  @event_to_class_names
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:



65
66
67
68
69
# File 'lib/jss/webhooks/event.rb', line 65

def self.parse_event(raw_event_json)
  event_json = JSON.parse(raw_event_json, symbolize_names: true)
  event_name = event_json[:webhook][:webhookEvent]
  JSSWebHooks::Event.event_to_class_names[event_name].new event_json, raw_event_json
end

Instance Method Details

#handleObject

init



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jss/webhooks/event.rb', line 102

def handle
  @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, since
  # it's what gets called by the Post processor
  # and it's output it returned as the HTTP Result
  # body.
  "Processed by #{@handlers.count} handlers"
end

#handle_with_proc(handler) ⇒ Object



127
128
129
# File 'lib/jss/webhooks/event.rb', line 127

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

#pipe_to_executable(handler) ⇒ Object

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



121
122
123
124
125
# File 'lib/jss/webhooks/event.rb', line 121

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