Class: Lich::Common::UpstreamHook

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

Overview

A class that manages upstream hooks for processing client strings.

Class Method Summary collapse

Class Method Details

.add(name, action) ⇒ Boolean

Adds a new upstream hook with a given name and action.

Examples:

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

Parameters:

  • name (String)

    the name of the hook

  • action (Proc)

    the action to be executed when the hook is triggered

Returns:

  • (Boolean)

    returns false if action is not a Proc, otherwise returns nil

Raises:

  • (StandardError)

    raises an error if action is not a Proc



19
20
21
22
23
24
25
26
# File 'lib/common/upstreamhook.rb', line 19

def UpstreamHook.add(name, action)
  unless action.class == 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>

Returns a hash of upstream hook sources.

Examples:

sources = UpstreamHook.hook_sources

Returns:

  • (Hash<String, String>)

    a hash mapping hook names to their sources



86
87
88
# File 'lib/common/upstreamhook.rb', line 86

def UpstreamHook.hook_sources
  @@upstream_hook_sources
end

.listArray<String>

Lists all registered upstream hooks.

Examples:

hooks = UpstreamHook.list

Returns:

  • (Array<String>)

    an array of hook names



65
66
67
# File 'lib/common/upstreamhook.rb', line 65

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

.remove(name) ⇒ nil

Removes an upstream hook by its name.

Examples:

UpstreamHook.remove("example_hook")

Parameters:

  • name (String)

    the name of the hook to be removed

Returns:

  • (nil)

    returns nil



55
56
57
58
# File 'lib/common/upstreamhook.rb', line 55

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, passing the client string through each.

Examples:

processed_string = UpstreamHook.run("input string")

Parameters:

  • client_string (String)

    the client string to be processed

Returns:

  • (String, nil)

    the processed client string or nil if any hook returns nil

Raises:

  • (StandardError)

    raises an error if any hook fails during execution



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/common/upstreamhook.rb', line 35

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

.sourcesnil

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

Examples:

UpstreamHook.sources

Returns:

  • (nil)

    returns nil



74
75
76
77
78
79
# File 'lib/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