Module: JSSWebHooks::Event::Handlers
- Defined in:
- lib/jss/webhooks/event/handlers.rb
Constant Summary collapse
- DEFAULT_HANDLER_DIR =
Module constants
'/Library/Application Support/JSSWebHooks'.freeze
Class Method Summary collapse
-
.event_handlers ⇒ Hash{String => Array}
Getter for @event_handlers.
-
.event_name_from_handler_filename(filename) ⇒ String?
Given a handler filename, return the event name it wants to handle.
-
.load_handler(from_file) ⇒ Type
Load an even handler from a file.
-
.load_handlers(from_dir = CONFIG.handler_dir, reload = false) ⇒ void
Load all the event handlers from the handler_dir or an arbitrary dir.
-
.loaded_event_handler ⇒ Proc?
Getter for @loaded_event_handler.
-
.loaded_event_handler=(a_proc) ⇒ Object
Setter for @loaded_event_handler.
Class Method Details
.event_handlers ⇒ Hash{String => Array}
Getter for @event_handlers
the JSS to an Array of handlers for the event. The handlers are either Proc objects to call from within ruby, or Pathnames to executable files which will take raw JSON on stdin.
90 91 92 |
# File 'lib/jss/webhooks/event/handlers.rb', line 90 def self.event_handlers @event_handlers end |
.event_name_from_handler_filename(filename) ⇒ String?
Given a handler filename, return the event name it wants to handle
181 182 183 184 185 |
# File 'lib/jss/webhooks/event/handlers.rb', line 181 def self.event_name_from_handler_filename(filename) @event_names ||= JSSWebHooks::Event.event_to_class_names.keys desired_event_name = filename.basename.to_s.split(/\.|-|_/).first @event_names.select { |n| desired_event_name.casecmp(n).zero? }.first end |
.load_handler(from_file) ⇒ Type
Load an even handler from a file. Handler files must begin with the name of the event they handle, e.g. ComputerAdded, followed by: nothing, a dot, a dash, or and underscore. Case doesn’t matter. So all of these are OK: ComputerAdded computeradded.sh COMPUTERAdded_notify_team Computeradded-update-ldap There can be as many as desired for each event.
Each must be either:
- An executable file, which will have the raw JSON from the JSS piped
to it's stdin when executed
or
- A non-executable file of ruby code like this:
JSSWebHooks.event_handler do |event|
# your code goes here.
end
(see HERE for details about writing the ruby handlers)
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/jss/webhooks/event/handlers.rb', line 151 def self.load_handler(from_file) handler_file = Pathname.new from_file event_name = event_name_from_handler_filename(handler_file) return unless event_name # create an array for this event's handlers, if needed @event_handlers[event_name] ||= [] if handler_file.executable? # store as a Pathname, we'll pipe JSON to it @event_handlers[event_name] << handler_file unless \ @event_handlers[event_name].include? handler_file else # load the file. If written correctly, it will # put a Proc into @loaded_event_handler load handler_file.to_s # store as a Proc, to be called when the event is handled. @event_handlers[event_name] << @loaded_event_handler unless \ @event_handlers[event_name].include? @loaded_event_handler end # if handler_file.executable? end |
.load_handlers(from_dir = CONFIG.handler_dir, reload = false) ⇒ void
This method returns an undefined value.
Load all the event handlers from the handler_dir or an arbitrary dir.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jss/webhooks/event/handlers.rb', line 106 def self.load_handlers(from_dir = CONFIG.handler_dir, reload = false) from_dir ||= DEFAULT_HANDLER_DIR if reload @handlers_loaded_from = nil @event_handlers = {} @loaded_event_handler = nil end handler_dir = Pathname.new(from_dir) return unless handler_dir.directory? && handler_dir.readable? handler_dir.children.each do |handler_file| load_handler(handler_file) if handler_file.file? && handler_file.readable? end @handlers_loaded_from = handler_dir end |
.loaded_event_handler ⇒ Proc?
Getter for @loaded_event_handler
destined for storage in @event_handlers
69 70 71 |
# File 'lib/jss/webhooks/event/handlers.rb', line 69 def self.loaded_event_handler @loaded_event_handler end |
.loaded_event_handler=(a_proc) ⇒ Object
Setter for @loaded_event_handler
destined for storage in @event_handlers
78 79 80 |
# File 'lib/jss/webhooks/event/handlers.rb', line 78 def self.loaded_event_handler=(a_proc) @loaded_event_handler = a_proc end |