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

Class Method Details

.event_handlersHash{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.

Returns:



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

Parameters:

  • filename (Pathname)

    The filename from which to glean the event name.

Returns:

  • (String, nil)

    The matching event name or nil if no match



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)

Parameters:

  • from_file (Pathname)

    the file from which to

Returns:

  • (Type)

    description of returned object



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.

Parameters:

  • from_dir (String, Pathname) (defaults to: CONFIG.handler_dir)

    directory from which to load the handlers

  • reload (Boolean) (defaults to: false)

    should we reload handlers if they’ve already been loaded?



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_handlerProc?

Getter for @loaded_event_handler

destined for storage in @event_handlers

Returns:

  • (Proc, nil)

    the most recent Proc loaded from a handler file.



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

Parameters:

  • a_proc (Proc)

    the most recent Proc loaded from a handler file.



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