Class: CJSONCI::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cjsonci/client.rb', line 6

def initialize(cmd)
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(cmd)
  @queue = Queue.new

  # consider something like https://github.com/kontena/kontena/blob/edb1d6c40e1ceb1b5aae88501d35cda525b64339/cli/lib/kontena/cli/helpers/exec_helper.rb#L38-L51
  Thread.new do
    @stdout.each_line do |line|
      @queue.push JSON.parse(line)
    end
  end
end

Instance Method Details

#closeObject



36
37
38
# File 'lib/cjsonci/client.rb', line 36

def close
  @stdin.close
end

#eval(code) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cjsonci/client.rb', line 18

def eval(code)
  input = {
    eval: code,
  }
  @stdin.puts input.to_json
  result = nil
  begin
    result = @queue.pop
  rescue Exception => ex
    @stderr.each_line do |line|
      puts line
    end
    exit 1
  end

  result
end