Permalink
Browse files

Initial commit. Functional example.

  • Loading branch information...
0 parents commit 6bed26890f6e5b2c42f20bdbd1e90126ddde95ef Pistos committed Aug 2, 2010
Showing with 119 additions and 0 deletions.
  1. +21 −0 LICENCE
  2. +1 −0 LICENSE
  3. +25 −0 README.md
  4. +9 −0 examples/hello-world.rb
  5. +63 −0 lib/ideone.rb
@@ -0,0 +1,21 @@
+The MIT Licence
+
+Copyright (c) 2008-2009 Pistos
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,25 @@
+# ideone.com gem
+
+A Ruby interface to the ideone.com API.
+http://github.com/Pistos/ideone-gem
+
+Base code taken from http://github.com/staii/tgoa . See References, below.
+
+## Examples
+
+See examples/ dir of repository or gem.
+
+## Contact
+
+Pistos in #mathetes on irc.freenode.net
+
+## Licence
+
+MIT. See LICENCE file.
+
+## References
+
+http://ideone.com/api
+http://ideone.com/api/1/service.wsdl
+http://ideone.com/files/ideone-api.pdf
+http://github.com/staii/tgoa/blob/7490b8564977f9a0b0dde4840e1c00f6a47cdcba/safe_code.rb
@@ -0,0 +1,9 @@
+require 'ideone'
+
+code = %{
+ puts "Hello, World!"
+}
+
+paste_id = Ideone.submit( :ruby, code )
+p Ideone.run( paste_id, nil )
+
@@ -0,0 +1,63 @@
+require 'json'
+require 'net/http'
+require 'cgi'
+
+
+module Ideone
+
+ class IdeoneError < StandardError; end
+
+ LANGUAGES = {
+ :ruby => 17, :python => 4, :c => 11
+ }
+
+ def self.submit( lang, code )
+ if ! LANGUAGES.keys.include?( lang )
+ raise "Invalid language: #{lang}"
+ end
+
+ Net::HTTP.post_form(
+ URI.parse("http://ideone.com/ideone/Index/submit/"),
+ {
+ 'lang' => LANGUAGES[lang],
+ 'run' => 0,
+ 'file' => code
+ }
+ ).header['location'][1..-1]
+ end
+
+ def self.run( id, input )
+ 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 1 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['result'] != "15" and i < 4
+
+ if i == 4
+ 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)
+ raise IdeoneError, "Error parsing output." unless out
+ out[1]
+ end
+ end
+
+end

0 comments on commit 6bed268

Please sign in to comment.