Class: Lich::Common::UpstreamHook

Inherits:
Object
  • Object
show all
Defined in:
documented/common/upstreamhook.rb

Overview

Handles upstream hooks for the Lich project. This class allows adding, running, removing, and listing hooks that can modify client strings.

Examples:

Adding a hook

UpstreamHook.add("example_hook", Proc.new { |str| str.upcase })

Class Method Summary collapse

Class Method Details

.add(name, action) ⇒ Boolean

Adds a new upstream hook.

Examples:

Adding a hook

UpstreamHook.add("example_hook", Proc.new { |str| str.upcase })

Parameters:

  • name (String)

    The name of the hook.

  • action (Proc)

    The action to be executed when the hook is run.

Returns:

  • (Boolean)

    Returns true if the hook was added successfully, false otherwise.

Raises:

  • (StandardError)

    Raises an error if action is not a Proc.



23
24
25
26
27
28
29
30
# File 'documented/common/upstreamhook.rb', line 23

def UpstreamHook.add(name, action)
  unless action.is_a?(Proc)
    echo "UpstreamHook: not a Proc (#{action})"
    return false
  end
  @@upstream_hook_sources[name] = (Script.current.name || "Unknown")
  @@upstream_hooks[name] = action
end

.hook_sourcesHash<String, String>

Retrieves the hash of upstream hook sources.

Examples:

Getting hook sources

sources = UpstreamHook.hook_sources

Returns:



85
86
87
# File 'documented/common/upstreamhook.rb', line 85

def UpstreamHook.hook_sources
  @@upstream_hook_sources
end

.listArray<String>

Lists all registered upstream hooks.

Examples:

Listing hooks

hooks = UpstreamHook.list

Returns:

  • (Array<String>)

    An array of hook names.



66
67
68
# File 'documented/common/upstreamhook.rb', line 66

def UpstreamHook.list
  @@upstream_hooks.keys.dup
end

.remove(name) ⇒ void

This method returns an undefined value.

Removes an upstream hook by name.

Examples:

Removing a hook

UpstreamHook.remove("example_hook")

Parameters:

  • name (String)

    The name of the hook to remove.



57
58
59
60
# File 'documented/common/upstreamhook.rb', line 57

def UpstreamHook.remove(name)
  @@upstream_hook_sources.delete(name)
  @@upstream_hooks.delete(name)
end

.run(client_string) ⇒ String?

Runs all registered upstream hooks in order.

Examples:

Running hooks

modified_string = UpstreamHook.run("input string")

Parameters:

  • client_string (String)

    The string to be modified by the hooks.

Returns:

  • (String, nil)

    Returns the modified string or nil if any hook returns nil.

Raises:

  • (StandardError)

    Catches exceptions from hook actions and removes the faulty hook.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'documented/common/upstreamhook.rb', line 38

def UpstreamHook.run(client_string)
  for key in @@upstream_hooks.keys
    begin
      client_string = @@upstream_hooks[key].call(client_string)
    rescue
      @@upstream_hooks.delete(key)
      respond "--- Lich: UpstreamHook: #{$!}"
      respond $!.backtrace.first
    end
    return nil if client_string.nil?
  end
  return client_string
end

.sourcesString

Displays the sources of all registered upstream hooks in a table format.

Examples:

Displaying hook sources

UpstreamHook.sources

Returns:

  • (String)

    A formatted string representation of the hook sources.



74
75
76
77
78
79
# File 'documented/common/upstreamhook.rb', line 74

def UpstreamHook.sources
  info_table = Terminal::Table.new :headings => ['Hook', 'Source'],
                                   :rows     => @@upstream_hook_sources.to_a,
                                   :style    => { :all_separators => true }
  Lich::Messaging.mono(info_table.to_s)
end