Class: Evalso::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/evalso.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Request

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.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/evalso.rb', line 46

def initialize(hash)
  self.class.base_uri Evalso.base_uri

  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])

  @hash = hash
end

Instance Method Details

#handle_input_files(files) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/evalso.rb', line 85

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

#responseObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/evalso.rb', line 65

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

  resp = self.class.post('/evaluate', opts)
  ret = JSON.parse(resp.body)

  if !resp.response.is_a?(Net::HTTPSuccess)
    error = ret['error']
    error ||= "#{resp.response.code} #{resp.response.msg.inspect}"
    raise Evalso::HTTPError, ret['error']
  end

  @response = Response.new(@hash[:code], ret)
end