Sha256: 6a896a7f234b94cbf1990f90d06287e64ba962c09956a331aeeca7c65bbd7707

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

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

      case eval_result.class.name
      when "SignalException"
        case eval_result.message
        when "SIGTERM"
          exit 0
        end
      when "SystemExit"
        exit 0
      end

      eval_result
    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.2.0 lib/cjsonci/servant.rb