Class: Judge0::Submission

Inherits:
Object
  • Object
show all
Defined in:
lib/submission.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Submission

Returns a new instance of Submission.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
# 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

Instance Attribute Details

#compile_outObject (readonly)

Returns the value of attribute compile_out.



11
12
13
# File 'lib/submission.rb', line 11

def compile_out
  @compile_out
end

#cpu_extra_timeObject

Returns the value of attribute cpu_extra_time.



3
4
5
# File 'lib/submission.rb', line 3

def cpu_extra_time
  @cpu_extra_time
end

#cpu_time_limitObject

Returns the value of attribute cpu_time_limit.



3
4
5
# File 'lib/submission.rb', line 3

def cpu_time_limit
  @cpu_time_limit
end

#enable_per_process_and_thread_memory_limitObject

Returns the value of attribute enable_per_process_and_thread_memory_limit.



3
4
5
# File 'lib/submission.rb', line 3

def enable_per_process_and_thread_memory_limit
  @enable_per_process_and_thread_memory_limit
end

#enable_per_process_and_thread_time_limitObject

Returns the value of attribute enable_per_process_and_thread_time_limit.



3
4
5
# File 'lib/submission.rb', line 3

def enable_per_process_and_thread_time_limit
  @enable_per_process_and_thread_time_limit
end

#expected_outputObject

Returns the value of attribute expected_output.



3
4
5
# File 'lib/submission.rb', line 3

def expected_output
  @expected_output
end

#language_idObject

Returns the value of attribute language_id.



3
4
5
# File 'lib/submission.rb', line 3

def language_id
  @language_id
end

#max_file_sizeObject

Returns the value of attribute max_file_size.



3
4
5
# File 'lib/submission.rb', line 3

def max_file_size
  @max_file_size
end

#max_processes_and_or_threadsObject

Returns the value of attribute max_processes_and_or_threads.



3
4
5
# File 'lib/submission.rb', line 3

def max_processes_and_or_threads
  @max_processes_and_or_threads
end

#memoryObject (readonly)

Returns the value of attribute memory.



11
12
13
# File 'lib/submission.rb', line 11

def memory
  @memory
end

#memory_limitObject

Returns the value of attribute memory_limit.



3
4
5
# File 'lib/submission.rb', line 3

def memory_limit
  @memory_limit
end

#number_of_runsObject

Returns the value of attribute number_of_runs.



3
4
5
# File 'lib/submission.rb', line 3

def number_of_runs
  @number_of_runs
end

#source_codeObject

Returns the value of attribute source_code.



3
4
5
# File 'lib/submission.rb', line 3

def source_code
  @source_code
end

#stack_limitObject

Returns the value of attribute stack_limit.



3
4
5
# File 'lib/submission.rb', line 3

def stack_limit
  @stack_limit
end

#status_descriptionObject (readonly)

Returns the value of attribute status_description.



11
12
13
# File 'lib/submission.rb', line 11

def status_description
  @status_description
end

#status_idObject (readonly)

Returns the value of attribute status_id.



11
12
13
# File 'lib/submission.rb', line 11

def status_id
  @status_id
end

#stderrObject (readonly)

Returns the value of attribute stderr.



11
12
13
# File 'lib/submission.rb', line 11

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



3
4
5
# File 'lib/submission.rb', line 3

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



11
12
13
# File 'lib/submission.rb', line 11

def stdout
  @stdout
end

#timeObject (readonly)

Returns the value of attribute time.



11
12
13
# File 'lib/submission.rb', line 11

def time
  @time
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/submission.rb', line 3

def token
  @token
end

#wall_time_limitObject

Returns the value of attribute wall_time_limit.



3
4
5
# File 'lib/submission.rb', line 3

def wall_time_limit
  @wall_time_limit
end

Instance Method Details

#get_tokenObject



90
91
92
93
# 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

#outputObject



61
62
63
64
65
66
67
# 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

#resultObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

#runObject



21
22
23
24
25
# File 'lib/submission.rb', line 21

def run
  get_token

  wait_response!
end

#tests_battery(tests) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_hashObject



69
70
71
72
73
74
75
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# 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_responseObject



95
96
97
98
99
100
101
102
# 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!Object



104
105
106
# File 'lib/submission.rb', line 104

def wait_response!
  to_submission(wait_response)
end