Class: Lightning::Plugin
- Inherits:
-
Object
- Object
- Lightning::Plugin
- Defined in:
- lib/lightning/plugin.rb
Instance Attribute Summary collapse
-
#lightning_dir ⇒ Object
Returns the value of attribute lightning_dir.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rpc ⇒ Object
Returns the value of attribute rpc.
-
#rpc_filename ⇒ Object
Returns the value of attribute rpc_filename.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.define_rpc(name, lambda) ⇒ Object
Define RPC method.
-
.desc(usage, desc, long_desc = nil) ⇒ Object
Define the definition of RPC method.
-
.hook(event, lambda) ⇒ Object
Define hook handler.
-
.methods ⇒ Hash
get RPM methods.
-
.subscribe(event, lambda) ⇒ Object
Define Event notification handler.
-
.subscriptions ⇒ Hash
get subscriptions.
Instance Method Summary collapse
-
#getmanifest ⇒ Hash
get manifest information.
- #init(options, configuration) ⇒ Object
-
#initialize ⇒ Plugin
constructor
A new instance of Plugin.
-
#run ⇒ Object
run plugin.
-
#shutdown ⇒ Object
shutdown this plugin.
Constructor Details
#initialize ⇒ Plugin
Returns a new instance of Plugin.
111 112 113 114 115 116 117 118 |
# File 'lib/lightning/plugin.rb', line 111 def initialize methods[:init] = Method.new(:init) = {} @stdout = STDOUT @stdin = STDIN methods[:getmanifest] = Method.new(:getmanifest) @log = Lightning::Logger.create(:plugin) end |
Instance Attribute Details
#lightning_dir ⇒ Object
Returns the value of attribute lightning_dir.
107 108 109 |
# File 'lib/lightning/plugin.rb', line 107 def lightning_dir @lightning_dir end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
106 107 108 |
# File 'lib/lightning/plugin.rb', line 106 def log @log end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
103 104 105 |
# File 'lib/lightning/plugin.rb', line 103 def end |
#rpc ⇒ Object
Returns the value of attribute rpc.
109 110 111 |
# File 'lib/lightning/plugin.rb', line 109 def rpc @rpc end |
#rpc_filename ⇒ Object
Returns the value of attribute rpc_filename.
108 109 110 |
# File 'lib/lightning/plugin.rb', line 108 def rpc_filename @rpc_filename end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
105 106 107 |
# File 'lib/lightning/plugin.rb', line 105 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
104 105 106 |
# File 'lib/lightning/plugin.rb', line 104 def stdout @stdout end |
Class Method Details
.define_rpc(name, lambda) ⇒ Object
Define RPC method.
69 70 71 72 73 74 75 76 77 |
# File 'lib/lightning/plugin.rb', line 69 def define_rpc(name, lambda) m = name.to_sym raise ArgumentError, 'method must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "#{m} was already defined." if methods[m] raise ArgumentError, "usage for #{m} dose not defined." unless @usage raise ArgumentError, "description for #{m} dose not defined." unless @desc define_method(m, lambda) methods[m] = Method.new(m, @usage, @desc, long_desc: @long_desc) end |
.desc(usage, desc, long_desc = nil) ⇒ Object
Define the definition of RPC method
60 61 62 63 64 |
# File 'lib/lightning/plugin.rb', line 60 def desc(usage, desc, long_desc = nil) @usage = usage @desc = desc @long_desc = long_desc end |
.hook(event, lambda) ⇒ Object
Define hook handler.
93 94 95 96 97 98 99 |
# File 'lib/lightning/plugin.rb', line 93 def hook(event, lambda) e = event.to_sym raise ArgumentError, 'handler must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "Hook #{e} was already registered." if methods[e] define_method(e, lambda) methods[e] = Method.new(e, type: Method::TYPE[:hook]) end |
.methods ⇒ Hash
get RPM methods.
46 47 48 |
# File 'lib/lightning/plugin.rb', line 46 def methods @methods ||= {} end |
.subscribe(event, lambda) ⇒ Object
Define Event notification handler.
82 83 84 85 86 87 88 |
# File 'lib/lightning/plugin.rb', line 82 def subscribe(event, lambda) e = event.to_sym raise ArgumentError, 'handler must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "Topic #{e} already has a handler." if subscriptions.include?(e) define_method(e, lambda) subscriptions << e end |
.subscriptions ⇒ Hash
get subscriptions
52 53 54 |
# File 'lib/lightning/plugin.rb', line 52 def subscriptions @subscriptions ||= [] end |
Instance Method Details
#getmanifest ⇒ Hash
get manifest information.
132 133 134 135 136 137 138 139 140 |
# File 'lib/lightning/plugin.rb', line 132 def getmanifest log.info("getmanifest") { options: .values, rpcmethods: rpc_methods.map(&:to_h), subscriptions: subscriptions, hooks: hook_methods.map(&:name), } end |
#init(options, configuration) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/lightning/plugin.rb', line 120 def init(, configuration) log.info("init") @lightning_dir = configuration['lightning-dir'] @rpc_filename = configuration['rpc-file'] socket_path = (Pathname.new(lightning_dir) + rpc_filename).to_path @rpc = Lightning::RPC.new(socket_path, log) .merge!() nil end |
#run ⇒ Object
run plugin.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/lightning/plugin.rb', line 148 def run log.info("Plugin run.") begin partial = '' stdin.each_line do |l| partial << l msgs = partial.split("\n\n", -1) next if msgs.size < 2 partial = multi_dispatch(msgs) end rescue Exception => e if e.is_a?(Interrupt) shutdown else log.error e throw e end end log.info("Plugin end.") end |
#shutdown ⇒ Object
shutdown this plugin.
143 144 145 |
# File 'lib/lightning/plugin.rb', line 143 def shutdown log.info "Plugin shutdown" end |