class Judge0::Submission

Attributes

compile_out[R]
cpu_extra_time[RW]
cpu_time_limit[RW]
enable_per_process_and_thread_memory_limit[RW]
enable_per_process_and_thread_time_limit[RW]
expected_output[RW]
language_id[RW]
max_file_size[RW]
max_processes_and_or_threads[RW]
memory[R]
memory_limit[RW]
number_of_runs[RW]
source_code[RW]
stack_limit[RW]
status_description[R]
status_id[R]
stderr[R]
stdin[RW]
stdout[R]
time[R]
token[RW]
wall_time_limit[RW]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/submission.rb, line 14
def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Public Instance Methods

get_token() click to toggle source
# File lib/submission.rb, line 90
def get_token
  resp = Faraday.post(Judge0.url('/submissions/?base64_encoded=false&wait=false'), to_hash)
  @token = JSON.parse(resp.body)['token']
end
output() click to toggle source
# File lib/submission.rb, line 61
def output
  msg = ''
  msg = @stdout if @stdout
  msg +="ERROR:\n#{@stderr}\n" if @stderr
  msg += "MESSAGE:\n#{@message}\n" if @message
  msg
end
result() click to toggle source
# File lib/submission.rb, line 46
def result
  {
    stdout: @stdout,
    time: @time,
    memory: @memory,
    stderr: @stderr,
    compile_out: @compile_out,
    message: @message,
    status: {
      id: @status_id,
      description: @status_description
    }
  }
end
run() click to toggle source
# File lib/submission.rb, line 21
def run
  get_token

  wait_response!
end
tests_battery(tests) click to toggle source
# File lib/submission.rb, line 27
def tests_battery (tests)
  tests.map! do |test|
    if test.respond_to?(:input) && test.respond_to?(:output)
      @stdin = test.input
      @expected_output = test.output
    elsif test.include?(:input) && test.respond_to?(:output)
      @stdin = test[:input]
      @expected_output = test[:output]
    else
      @stdin = test[0]
      @expected_output = test[1]
    end
    get_token
  end
  tests.map do |test|
    wait_response
  end
end
to_hash() click to toggle source
# File lib/submission.rb, line 69
def to_hash
  Hash[
    instance_variables.map do |name|
      [name[1..-1].to_sym, instance_variable_get(name)]
    end
  ]
end
to_submission(response) click to toggle source
# File lib/submission.rb, line 77
def to_submission(response)
  @stdout = response['stdout']
  @time = response['time'].to_f
  @memory = response['memory']
  @stderr = response['stderr']
  @compile_out = response['compile_out']
  @message = response['message']
  @status_id = response['status']['id']
  @status_description = response['status']['description']

  result
end
wait_response() click to toggle source
# File lib/submission.rb, line 95
def wait_response
  begin
    resp = Faraday.get(Judge0.url("/submissions/#{@token}"))
    body = JSON.parse(resp.body)
    puts "waiting: #{token} - #{body['status']['description']}" unless ENV['RAILS_ENV'] == 'test'
  end while body['status']['id'] <= 2
  body
end
wait_response!() click to toggle source
# File lib/submission.rb, line 104
def wait_response!
  to_submission(wait_response)
end