Module: Ideone

Defined in:
lib/ideone.rb

Defined Under Namespace

Classes: IdeoneError

Constant Summary collapse

LANGUAGES =
{
  :bash       => 28,
  :c          => 11,
  'c#'        => 27,
  :csharp     => 27,
  'c++'       => 1,
  :cpp        => 1,
  :cplusplus  => 1,
  :clojure    => 111,
  :commonlisp => 32,
  :clisp      => 32,
  :erlang     => 36,
  :go         => 114,
  :haskell    => 21,
  :java       => 10,
  :javascript => 112,
  :js         => 112,
  :perl       => 3,
  :php        => 29,
  :python     => 4,
  :ruby       => 17,
}
TIMEOUT =

iterations

4

Class Method Summary collapse

Class Method Details

.run(id, input, timeout = TIMEOUT) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ideone.rb', line 47

def self.run( id, input, timeout = TIMEOUT )
  res = JSON.load(
    Net::HTTP.post_form(
      URI.parse( "http://ideone.com/submit/parent/#{id}" ),
      { 'input' => input }
    ).body
  )
  if res['status'] != 'ok'
    raise IdeoneError, "Posting of input failed."
  end

  loc = res['link']
  i = 0

  begin
    sleep 3  if i > 0
    res = JSON.load(
      Net::HTTP.post_form(
        URI.parse("http://ideone.com/ideone/Index/view/id/#{loc}/ajax/1"),
        {}
      ).body
    )
    i += 1
  end while res['status'] != '0' && i < timeout

  case res[ 'result' ]
    when '0', '15'
      # success
    when '11'
      raise IdeoneError, "Compilation error"
    when '12'
      raise IdeoneError, "Runtime error"
    when '13'
      raise IdeoneError, "Execution timed out"
    when '17'
      raise IdeoneError, "Memory limit exceeded"
    when '19'
      raise IdeoneError, "Illegal system call"
    when '20'
      raise IdeoneError, "Internal error occurred at ideone.com"
    else
      raise IdeoneError, "Unknown result: " + res[ 'result' ]
  end

  if i == timeout
    raise IdeoneError, "Timed out while waiting for code result."
  end

  err = res['inouterr'].match(/<label>stderr:<\/label>.*?<pre.*?>(.*?)<\/pre>/m)
  if err
    err[1]
  else
    out = res['inouterr'].match(/<label>output:<\/label>.*?<pre.*?>(.*?)<\/pre>/m)
    if out
      out[1]
    end
  end
end

.submit(lang_, code) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ideone.rb', line 34

def self.submit( lang_, code )
  lang = LANGUAGES[ lang_ ] || lang_

  Net::HTTP.post_form(
    URI.parse("http://ideone.com/ideone/Index/submit/"),
    {
      'lang' => lang,
      'run'  => 0,
      'file' => code
    }
  ).header['location'][1..-1]
end