Class: Huck::Handlers::ExecHandler

Inherits:
Huck::Handler show all
Defined in:
lib/huck/handlers/exec.rb

Overview

A handler to execute arbitrary scripts, passing in the generated text as input on stdin.

Instance Attribute Summary

Attributes inherited from Huck::Handler

#config

Instance Method Summary collapse

Methods inherited from Huck::Handler

factory

Constructor Details

#initializeExecHandler

Includes all required modules



10
11
12
# File 'lib/huck/handlers/exec.rb', line 10

def initialize
  require 'open3'
end

Instance Method Details

#handle(msg) ⇒ Object

Handle an individual message by running an executable, passing in the gathered data via stdin.



26
27
28
29
30
31
32
# File 'lib/huck/handlers/exec.rb', line 26

def handle msg
  verify_config

  Open3.popen2 @config['exec']['command'] do |stdin, stdout, thread|
    stdin.print msg
  end
end

#verify_configObject

Ensures that configuration is set properly before executing



15
16
17
18
19
20
21
22
# File 'lib/huck/handlers/exec.rb', line 15

def verify_config
  if !@config.has_key? 'exec'
    raise RuntimeError, 'missing exec config'
  end
  if !@config['exec'].has_key? 'command'
    raise RuntimeError, 'missing exec config: command'
  end
end