Sha256: 6b97698a026e6b8643ec1631bb57608ae8b070442d5cec3ac13a95059d5df75e

Contents?: true

Size: 983 Bytes

Versions: 1

Compression:

Stored size: 983 Bytes

Contents

require "json"

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

    private

    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

    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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cjsonci-0.1.0 lib/cjsonci/servant.rb