Module: CJSONCI::Servant

Defined in:
lib/cjsonci/servant.rb

Class Method Summary collapse

Class Method Details

.eval_input(obj) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/cjsonci/servant.rb', line 34

def self.eval_input(obj)
  code = obj["eval"]
  eval_result = nil
  begin
    eval_result = TOPLEVEL_BINDING.eval code.to_s
  rescue Exception => ex
    eval_result = ex
  end
end

.format_message(result) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cjsonci/servant.rb', line 44

def self.format_message(result)
  if result.is_a? Exception
    {
      type: "error",
      class: result.class,
      message: result,
    }
  else
    {
      type: "ok",
      result: result,
    }
  end
end

.read_inputObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cjsonci/servant.rb', line 15

def self.read_input
  input = ""
  obj = nil
  loop do
    line = STDIN.gets
    exit 0 unless line
    input << line

    begin
      obj = JSON.parse(input)
    rescue
    end

    break if obj
  end

  obj
end

.run!Object



5
6
7
8
9
10
11
# File 'lib/cjsonci/servant.rb', line 5

def self.run!
  loop do
    obj = read_input
    result = eval_input(obj)
    puts format_message(result).to_json
  end
end