Class: Chook::HandledSubject

Inherits:
Subject
  • Object
show all
Defined in:
lib/chook/subject/handled_subject.rb

Overview

A subject that will be used within a HandledEvent class, to be received by a Webhook server and processed.

This is the parent class to all classes in the Chook::HandledSubjects module.

All constants, methods, and attributes that are common to HandledSubject classes are defined here.

Constant Summary

Constants inherited from Subject

Subject::COMPUTER, Subject::DEP_DEVICE, Subject::JAMF_SOFTWARE_SERVER, Subject::MOBILE_DEVICE, Subject::NAME_CONSTANT, Subject::PATCH_SW_UPDATE, Subject::POLICY_FINISHED, Subject::PUSH, Subject::REST_API_OPERATION, Subject::SCEP_CHALLENGE, Subject::SMART_GROUP

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Subject

classes

Constructor Details

#initialize(subject_data_from_json) ⇒ HandledSubject

All the subclassses will inherit this constructor

The argument is a Hash, the parsed ‘event_object’ data from the JSON blob for a webhook.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chook/subject/handled_subject.rb', line 63

def initialize(subject_data_from_json)
  my_classname = self.class.const_get Chook::Subject::NAME_CONSTANT
  my_attribs = Chook::Subject.classes[my_classname]

  subject_data_from_json.each do |key, value|
    # ignore unknown attributes. Shouldn't get any,but....
    next unless my_attribs[key]

    # does the value need conversion?
    converter = my_attribs[key][:converter]
    if converter
      value = converter.is_a?(Symbol) ? value.send(converter) : converter.call(value)
    end # if converter

    # set the value.
    instance_variable_set "@#{key}", value
  end # each key value
end

Class Method Details

.generate_classesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chook/subject/handled_subject.rb', line 38

def self.generate_classes
  Chook::Subject.classes.each do |classname, attribs|
    # Don't redefine anything.
    next if Chook::HandledSubjects.const_defined? classname

    # new subclass of Chook::HandledSubject
    new_class = Class.new(Chook::HandledSubject) do
      # add getters for all the attributes.
      # no need for setters, as handled objects are immutable
      attribs.keys.each { |attrib| attr_reader attrib }
    end

    # set a class constant so each class knows it's name
    new_class.const_set Chook::Subject::NAME_CONSTANT, classname

    # add the class to the Chook::HandledSubjects namespace module
    Chook::HandledSubjects.const_set classname, new_class
  end # classes.each do |classname, attribs|
end