Module: Chook::HandledEvent::Handlers

Defined in:
lib/chook/event/handled_event/handlers.rb

Overview

The Handlers namespace module

Constant Summary collapse

DEFAULT_HANDLER_DIR =

Module Constants

'/Library/Application Support/Chook'.freeze

Class Method Summary collapse

Class Method Details

.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


196
197
198
199
200
# File 'lib/chook/event/handled_event/handlers.rb', line 196

def self.event_name_from_handler_filename(filename)
  @event_names ||= Chook::Event::EVENTS.keys
  desired_event_name = filename.basename.to_s.split(/\.|-|_/).first
  @event_names.select { |n| desired_event_name.casecmp(n).zero? }.first
end

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

  • (Hash{String => Array})

    a mapping of Event Names as the come from


97
98
99
# File 'lib/chook/event/handled_event/handlers.rb', line 97

def self.handlers
  @handlers
end

.load_handler(from_file) ⇒ void

This method returns an undefined value.

Load an event 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:
  Chook.event_handler do |event|
    # your code goes here.
  end

(see the Chook README for details about writing the ruby handlers)

Parameters:

  • from_file (Pathname)

    the file from which to load the handler


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chook/event/handled_event/handlers.rb', line 160

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
  @handlers[event_name] ||= []

  if handler_file.executable?
    # store as a Pathname, we'll pipe JSON to it
    unless @handlers[event_name].include? handler_file
      @handlers[event_name] << handler_file
      puts "===> Loaded executable handler file '#{handler_file.basename}'"
    end
    return
  end

  # load the file. If written correctly, it will
  # put a Proc into @loaded_handler
  load handler_file.to_s
  if @loaded_handler
    @handlers[event_name] << @loaded_handler
    puts "===> Loaded internal handler file '#{handler_file.basename}'"
    @loaded_handler = nil
  else
    puts "===> FAILED loading internal handler file '#{handler_file.basename}'"
  end
end

.load_handlers(from_dir: Chook::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)

    directory from which to load the handlers. Defaults to CONFIG.handler_dir or DEFAULT_HANDLER_DIR if config is unset

  • reload (Boolean)

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


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chook/event/handled_event/handlers.rb', line 115

def self.load_handlers(from_dir: Chook::CONFIG.handler_dir, reload: false)
  from_dir ||= DEFAULT_HANDLER_DIR
  if reload
    @handlers_loaded_from = nil
    @handlers = {}
    @loaded_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
  @handlers.values.flatten.size
end

.loaded_handlerProc?

Getter for @loaded_handler

destined for storage in @handlers

Returns:

  • (Proc, nil)

    the most recent Proc loaded from a handler file.


73
74
75
# File 'lib/chook/event/handled_event/handlers.rb', line 73

def self.loaded_handler
  @loaded_handler
end

.loaded_handler=(a_proc) ⇒ Object

Setter for @loaded_event_handler

Parameters:

  • a_proc (Proc)

    a Proc object for storage in @handlers


81
82
83
# File 'lib/chook/event/handled_event/handlers.rb', line 81

def self.loaded_handler=(a_proc)
  @loaded_handler = a_proc
end