Class: Chook::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/chook/event.rb

Overview

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.

Direct Known Subclasses

HandledEvent, TestEvent

Constant Summary collapse

EVENTS =

A mapping of the Event Names (as they are known to the JSS) to the the matching Subject class names

The 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 the TestEvent module.

E.g. the HandledEvents class that handles the ‘ComputerPolicyFinished’ event is Chook::HandledEvent::ComputerPolicyFinished

and

the TestEvents that simulates ‘ComputerPolicyFinished’ is Chook::TestEvents::ComputerPolicyFinished

And, those classes take the matching ‘Computer’ subject, either Chook::HandledSubjects::Computer or Chook::TestSubjects::Computer

{
  'ComputerAdded' => Chook::Subject::COMPUTER,
  'ComputerCheckIn' => Chook::Subject::COMPUTER,
  'ComputerInventoryCompleted' => Chook::Subject::COMPUTER,
  'ComputerPolicyFinished' => Chook::Subject::POLICY_FINISHED,
  'ComputerPushCapabilityChanged' => Chook::Subject::COMPUTER,
  'DeviceAddedToDEP' => Chook::Subject::DEP_DEVICE,
  'JSSShutdown' => Chook::Subject::JAMF_SOFTWARE_SERVER,
  'JSSStartup' => Chook::Subject::JAMF_SOFTWARE_SERVER,
  'MobileDeviceCheckIn' => Chook::Subject::MOBILE_DEVICE,
  'MobileDeviceCommandCompleted' => Chook::Subject::MOBILE_DEVICE,
  'MobileDeviceEnrolled' => Chook::Subject::MOBILE_DEVICE,
  'MobileDevicePushSent' => Chook::Subject::MOBILE_DEVICE,
  'MobileDeviceUnEnrolled' => Chook::Subject::MOBILE_DEVICE,
  'PatchSoftwareTitleUpdated' => Chook::Subject::PATCH_SW_UPDATE,
  'PushSent' => Chook::Subject::PUSH,
  'RestAPIOperation' => Chook::Subject::REST_API_OPERATION,
  'SCEPChallenge' => Chook::Subject::SCEP_CHALLENGE,
  'SmartGroupComputerMembershipChange' => Chook::Subject::SMART_GROUP,
  'SmartGroupMobileDeviceMembershipChange' => Chook::Subject::SMART_GROUP
}.freeze
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.

'EVENT_NAME'.freeze
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.

'SUBJECT_CLASS'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Event

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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

Instance Attribute Details

#idString (readonly)

Returns A unique identifier for this chook event.

Returns:

  • (String)

    A unique identifier for this chook event



99
100
101
# File 'lib/chook/event.rb', line 99

def id
  @id
end

#parsed_jsonHash? (readonly)

Returns If this event object was initialized with a JSON blob as from the JSS, the Hash parsed from it will be stored here.

Returns:

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



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

def parsed_json
  @parsed_json
end

#raw_jsonString? (readonly)

Returns If this event object was initialized with a JSON blob as from the JSS, it will be stored here.

Returns:

  • (String, nil)

    If this event object was initialized with a JSON blob as from the JSS, it will be stored here.



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

def raw_json
  @raw_json
end

#subjectObject (readonly)

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

Returns:

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



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

def subject
  @subject
end

#webhook_idInteger (readonly)

Returns The webhook id in the JSS that caused this event.

Returns:

  • (Integer)

    The webhook id in the JSS that caused this event



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

def webhook_id
  @webhook_id
end

#webhook_nameString (readonly)

Returns The webhook name in the JSS that caused this event.

Returns:

  • (String)

    The webhook name in the JSS that caused this event



105
106
107
# File 'lib/chook/event.rb', line 105

def webhook_name
  @webhook_name
end