<commit_before>require "evalso/version"
require "httparty"
require "json"

# TODO: Handle auth, once eval.so supports it.

module Evalso
  API_VERSION = 1

  def self.run(language, code)
    Request.new(language, code).response
  end

  class Request
    include HTTParty
    base_uri 'http://eval.so/api/'

    attr_accessor :response

    def initialize(language, code)
      hash = {
        :api_version => Evalso::API_VERSION,
        :api_key     => "", # Doesn't actually use API keys yet.
        :language    => language.to_s,
        :code        => code,
      }

      opts = {
        :body    => hash.to_json,
        :headers => {
          "Content-Type" => "text/json"
        }
      }

      ret = JSON.parse(self.class.post('/evaluate', opts).body)
      @response = Response.new(code, ret)
    end
  end

  class Response
    attr_accessor :stdout, :stderr, :wall_time, :remaining_evaluations, :code

    def initialize(code, hash)
      @code   = code
      @stdout = hash["stdout"]
      @stderr = hash["stderr"]
      @wall_time = hash["wallTime"]
      @exit_code = hash["exitCode"]
    end

    def inspect
      "#<#{self.class.name} code=#{code.inspect} stdout=#{stdout.inspect} stderr=#{stderr.inspect} wall_time=#{wall_time.inspect}>"
    end
  end
end

<commit_msg>Updated to use full API.<commit_after>require "evalso/version"
require "httparty"
require "json"
require "base64"

# TODO: Handle auth, once eval.so supports it.

# Current API docs: http://eval.so/api/1

module Evalso
  API_VERSION = 1

  def self.base_uri(uri = nil)
    return @@base_uri unless uri

    @@base_uri = uri
  end

  base_uri 'http://eval.so/api/'

  def self.run(hash)
    Request.new(hash).response
  end

  class Request
    include HTTParty
    base_uri Evalso.base_uri

    attr_accessor :response

    # Usage:
    #   Evalso::Request.new(lang, code, inputFiles: ["filename.txt"])
    # or
    #   Evalso::Request.new(language: lang, code: code, inputFiles: {"filename.txt" => "file contents"})
    #
    # Defaults:
    #   Evalso::Request.new(language: nil, code: nil, inputFiles: {}, compilationOnly: false)
    #
    # If `language` or `code` are not specified, an ArugmentError is raised.
    def initialize(hash)
      hash = {
        :api_version => Evalso::API_VERSION,
        :language    => nil,
        :code        => nil,
        :inputFiles  => {},
        :compilationOnly => false,
      }.merge(hash)

      raise ArgumentError, "no language specified." unless hash[:language]
      raise ArgumentError, "no code specified."     unless hash[:code]

      hash[:inputFiles] = handle_input_files(hash[:inputFiles])

      opts = {
        :body    => hash.to_json,
        :headers => {
          "Content-Type" => "text/json"
        }
      }

      ret = JSON.parse(self.class.post('/evaluate', opts).body)
      @response = Response.new(hash[:code], ret)
    end

    def handle_input_files(files)
      # If files is an array of filenames,
      # make it a hash of filenames => file content.
      if files.is_a?(Array)
        ret = {}
        files.each do |file|
          ret[file] = open(file).read
        end

        files = ret
      end

      files.keys.each do |file|
        files[file] = Base64.encode64(files[file])
      end

      files
    end
  end

  class Response
    attr_accessor :code, :stdout, :stderr, :wall_time, :remaining_evaluations,
                  :output_files, :compilation_result

    def initialize(code, hash)
      @code   = code
      @stdout = hash["stdout"]
      @stderr = hash["stderr"]
      @wall_time = hash["wallTime"]
      @exit_code = hash["exitCode"]
      @output_files = handle_output_files(hash["outputFiles"] || {})
      @compilation_result = hash["compilationResult"]
    end

    def handle_output_files(files)
      files.keys.each do |file|
        files[file] = Base64.decode64(files[file])
      end
    end

    def inspect
      "#<#{self.class.name} code=#{code.inspect} stdout=#{stdout.inspect} stderr=#{stderr.inspect} wall_time=#{wall_time.inspect}>"
    end
  end
end


{'fraction_non_alphanumeric': 0.12910338069573737, 'fraction_numerical': 0.0031847133757961785, 'mean_word_length': 2.483788395904437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1621570', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1295, 'n_words': 305}