Sha256: ced5b20caf1f8088180ac77f0f38bd2f388647a4bfe93d08aaf66ff9bd7967bb

Contents?: true

Size: 814 Bytes

Versions: 2

Compression:

Stored size: 814 Bytes

Contents

require "json"
require "open3"

module CJSONCI
  class Client
    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

    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

    def close
      @stdin.close
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cjsonci-0.2.0 lib/cjsonci/client.rb
cjsonci-0.1.0 lib/cjsonci/client.rb