Class: StdinResponder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merge_stderr: false, prompt_delay_threshold: 1.0, timeout: 120, verbose: true, debug: false) ⇒ StdinResponder

Returns a new instance of StdinResponder.



46
47
48
49
50
51
52
53
54
55
# File 'lib/stdin_responder.rb', line 46

def initialize(merge_stderr: false, prompt_delay_threshold: 1.0, timeout: 120, verbose: true, debug: false)
  @rules = []
  @stdout_buffer = ""
  @stderr_buffer = merge_stderr ? @stdout_buffer : ""
  @stdin_buffer = ""
  @prompt_delay_threshold = prompt_delay_threshold
  @timeout = timeout
  @verbose = verbose
  @debug = debug
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



44
45
46
# File 'lib/stdin_responder.rb', line 44

def rules
  @rules
end

Instance Method Details

#add_rule(rule) ⇒ Object



57
58
59
# File 'lib/stdin_responder.rb', line 57

def add_rule(rule)
  @rules << {default: "", repeat: 0}.merge(rule)
end

#run(command) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/stdin_responder.rb', line 61

def run(command)
  @session_rules = @rules.dup
  pid, stdin, stdout, stderr = Open4.popen4(command)

  out_buffer = ""
  prompt_threshold = 1

  @stdout_thread = Thread.start do
    monitor_stdout(stdout)
  end

  @stderr_thread = Thread.start do
    monitor_stderr(stderr)
  end

  @stdin_thread = Thread.start do
    generate_responses(stdin)
  end

  master_thread = Thread.start do
    monitor_threads
  end

  @stdout_thread.abort_on_exception = true
  @stderr_thread.abort_on_exception = true
  @stdin_thread.abort_on_exception = true
  master_thread.abort_on_exception = true

  @stdout_thread.join
  @stderr_thread.join
  stdout.close
  stderr.close

  @stdin_thread.join
  stdin.close
end