Class: Lich::Common::UpstreamHook
- Inherits:
-
Object
- Object
- Lich::Common::UpstreamHook
- Defined in:
- lib/common/upstreamhook.rb
Overview
A class that manages upstream hooks for processing client strings.
Class Method Summary collapse
-
.add(name, action) ⇒ Boolean
Adds a new upstream hook with a given name and action.
-
.hook_sources ⇒ Hash<String, String>
Returns a hash of upstream hook sources.
-
.list ⇒ Array<String>
Lists all registered upstream hooks.
-
.remove(name) ⇒ nil
Removes an upstream hook by its name.
-
.run(client_string) ⇒ String?
Runs all registered upstream hooks in order, passing the client string through each.
-
.sources ⇒ nil
Displays the sources of all registered upstream hooks in a table format.
Class Method Details
.add(name, action) ⇒ Boolean
Adds a new upstream hook with a given name and action.
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_sources ⇒ Hash<String, String>
Returns a hash of upstream hook sources.
86 87 88 |
# File 'lib/common/upstreamhook.rb', line 86 def UpstreamHook.hook_sources @@upstream_hook_sources end |
.list ⇒ Array<String>
Lists all registered upstream hooks.
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.
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.
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 |
.sources ⇒ nil
Displays the sources of all registered upstream hooks in a table format.
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 |