class Chook::Event
This is the MetaClass for all Event
objects, both handled and test. It holds constants, methods, and attributes that are common to all events in Chook
.
An 'event' within Chook
is the ruby-abstraction of a JSON payload as sent by a JSS Webhook representing an event that takes place in the JSS.
All events contain a subject
attribute, which represent the thing upon which the event acted, e.g. a computer, mobile divice, or the JSS itself. The subject
attribute contains an object which is a subclass of Chook::Subject
, q.v.
Constants
- EVENTS
A mapping of the
Event
Names (as they are known to the JSS) to the the matchingSubject
class namesThe event names from the JSS are in the JSON hash as the value of JSON_hash[:webhookEvent] and they are also the names of the matching event classes within the
HandledEvent
module and theTestEvent
module.E.g. the
HandledEvents
class that handles the 'ComputerPolicyFinished' event is Chook::HandledEvent::ComputerPolicyFinishedand
the
TestEvents
that simulates 'ComputerPolicyFinished' is Chook::TestEvents::ComputerPolicyFinishedAnd, those classes take the matching 'Computer' subject, either Chook::HandledSubjects::Computer or Chook::TestSubjects::Computer
- EVENT_NAME_CONST
Event
subclasses will have an EVENT_NAME constant, which contains the name of the event, one of the keys from the Event::EVENTS Hash.- SUBJECT_CLASS_CONST
Event
subclasses will have a SUBJECT_CLASS constant, which contains the class of the subject of the event, based on one of the values from the Event::EVENTS Hash.
Attributes
@return [String] A unique identifier for this chook event
@return [Hash, nil] If this event object was initialized with a JSON
blob as from the JSS, the Hash parsed from it will be stored here.
@return [String, nil] If this event object was initialized with a JSON
blob as from the JSS, it will be stored here.
@return [Object] The subject of this event - i.e. the thing it acted upon.
An instance of a class from either the Chook::HandledSubjects module or the Chook::TestSubjects module
@return [Integer] The webhook id in the JSS that caused this event
@return [String] The webhook name in the JSS that caused this event
Public Class Methods
Args are a hash (or group of named params) with these possible keys:
raw_json: [String] A raw JSON blob for a full event as sent by the JSS If this is present, all other keys are ignored and the instance is built with this data. parsed_json: [Hash] A pre-parsed JSON blob for a full event. If this is present, all other keys are ignored and the instance is built with this data (however raw_json wins if both are provided) webhook_event: [String] The name of the event, one of the keys of EVENTS webhook_id: [Integer] The id of the webhook defined in the JSS webhook_name: [String] The name of the webhook defined in the JSS
The remaning keys are the attributes of the Subject
subclass for this event. Any not provided will be nil.
# File lib/chook/event.rb, line 134 def initialize(**args) @id = "#{Time.now.to_i}-#{SecureRandom.urlsafe_base64 8}" if args[:raw_json] @raw_json = args[:raw_json] @parsed_json = JSON.parse @raw_json, symbolize_names: true elsif args[:parsed_json] @parsed_json = args[:parsed_json] end if @parsed_json @webhook_name = @parsed_json[:webhook][:name] @webhook_id = @parsed_json[:webhook][:id] subject_data = @parsed_json[:event] else @webhook_name = args.delete :webhook_name @webhook_id = args.delete :webhook_id subject_data = args end @subject = self.class::SUBJECT_CLASS.new subject_data end