Class: JSSWebHooks::Event
- Inherits:
-
Object
- Object
- JSSWebHooks::Event
- 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
Direct Known Subclasses
ComputerAddedEvent, ComputerCheckInEvent, ComputerInventoryCompletedEvent, ComputerPolicyFinishedEvent, ComputerPushCapabilityChangedEvent, JSSShutdownEvent, JSSStartupEvent, MobileDeviceCheckInEvent, MobileDeviceCommandCompletedEvent, MobileDeviceEnrolledEvent, MobileDevicePushSentEvent, MobileDeviceUnEnrolledEvent, PatchSoftwareTitleUpdatedEvent, PushSentEvent, RestAPIOperationEvent, SCEPChallengeEvent, SmartGroupComputerMembershipChangeEvent, SmartGroupMobileDeviceMembershipChangeEvent
Defined Under Namespace
Modules: Handlers
Constant Summary collapse
- WEBHOOK_ATTRIBUTES =
[ :id, :name, :webhookEvent ].freeze
- WebHook =
ImmutableStruct.new(*WEBHOOK_ATTRIBUTES)
Instance Attribute Summary collapse
-
#event_json ⇒ Hash
readonly
The parsed JSON recieved from the JSS.
-
#event_object ⇒ JSSWebHooks::EventObjects class
readonly
The event object sent with the event.
-
#handlers ⇒ Array<Proc,Pathname>
readonly
The handlers defined for this event.
-
#raw_json ⇒ String
readonly
The raw JSON recieved from the JSS.
-
#webhook ⇒ JSSWebHooks::WebHook
readonly
The webhook in the JSS that caused this event.
Class Method Summary collapse
-
.event_to_class_names ⇒ Hash{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.
-
.parse_event(raw_event_json) ⇒ JSSWebHooks::Event subclass
Given the raw json from the JSS webhook, create an object of the correct Event subclass.
Instance Method Summary collapse
-
#handle ⇒ Object
init.
- #handle_with_proc(handler) ⇒ Object
-
#initialize(event_json, raw_json) ⇒ Event
constructor
A new instance of Event.
-
#pipe_to_executable(handler) ⇒ Object
TODO: have something here that cleans up old threads and forks.
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_json ⇒ Hash (readonly)
Returns 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_object ⇒ JSSWebHooks::EventObjects class (readonly)
Returns The event object sent with the event.
81 82 83 |
# File 'lib/jss/webhooks/event.rb', line 81 def event_object @event_object end |
#handlers ⇒ Array<Proc,Pathname> (readonly)
Returns the handlers defined for this event.
84 85 86 |
# File 'lib/jss/webhooks/event.rb', line 84 def handlers @handlers end |
#raw_json ⇒ String (readonly)
Returns the raw JSON recieved from the JSS.
72 73 74 |
# File 'lib/jss/webhooks/event.rb', line 72 def raw_json @raw_json end |
#webhook ⇒ JSSWebHooks::WebHook (readonly)
Returns 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_names ⇒ Hash{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
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
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
#handle ⇒ Object
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 |