class Ruboty::Handlers::Ideone_codeiq
Constants
- LANGUAGES
基本的にruboty-ideoneの古いバージョンから拾ってくれば大丈夫。念のためソース開いて照合を。
Public Class Methods
new(*__reserved__)
click to toggle source
Calls superclass method
# File lib/ruboty/handlers/ideone-codeiq.rb, line 83 def initialize(*__reserved__) super @input=nil @languages=LANGUAGES end
Public Instance Methods
languages(message)
click to toggle source
# File lib/ruboty/handlers/ideone-codeiq.rb, line 98 def languages(message) message.reply @languages.map{|e|"#{'%4d:'%e[:key]} #{e[:value]}\n"}.join end
read_uri(uri)
click to toggle source
# File lib/ruboty/handlers/ideone-codeiq.rb, line 88 def read_uri(uri) return nil if !uri||uri.empty? Kernel.open(uri){|f| return f.read } end
setinput(message)
click to toggle source
# File lib/ruboty/handlers/ideone-codeiq.rb, line 101 def setinput(message) #input_uri: 入力ファイル(空文字列ならクリア) if !message[:input_uri]||message[:input_uri].empty? @input=nil message.reply 'Input cleared.' else @input=read_uri(message[:input_uri]) message.reply 'Input set.' end end
submit(message)
click to toggle source
# File lib/ruboty/handlers/ideone-codeiq.rb, line 111 def submit(message) #language: ID(数値)または言語名(文字列)。言語名の場合、記号類を除いて最大先頭一致のものを使用する。 #source_uri: ソースファイル #input_uri: 入力ファイル(空文字列ならsetinputの内容を使用) input=message[:input_uri]&&!message[:input_uri].empty? ? read_uri(message[:input_uri]) : @input #guess lang lang=message[:language] if lang.to_i>0 lang=lang.to_i else lang=lang.downcase.gsub(/[\s\(\)\.]/,'') lang=@languages.max_by{|e| _e=e[:value].downcase.gsub(/[\s\(\)\.]/,'') lang.size.downto(1).find{|i|_e.start_with?(lang[0,i])}||-1 }[:key].to_i end uri=URI.parse('https://codeiq.jp/tools/connect.php') https=Net::HTTP.new(uri.host,uri.port) https.use_ssl=true https.start{ resp=https.post(uri.path,'lang='+lang.to_s+'&source='+CGI.escape(read_uri(message[:source_uri]))+'&input='+CGI.escape(input),{ 'Content-Type'=>'application/x-www-form-urlencoded; charset=UTF-8', }) result=JSON.parse(resp.body) if result['status'].to_i<0 message.reply '[Ruboty::Ideone::CodeIQ] waiting for compilation' elsif result['status'].to_i==1 message.reply '[Ruboty::Ideone::CodeIQ] compiling' elsif result['status'].to_i==3 message.reply '[Ruboty::Ideone::CodeIQ] running' elsif result['error']!='OK' message.reply '[Ruboty::Ideone::CodeIQ] something wrong happened in execution' elsif result['result'].to_i==15 message.reply result['output'] else message.reply('[Ruboty::Ideone::CodeIQ] '+{ 0=>'not running', 11=>'compilation error', 12=>'runtime error', 13=>'time limit exceeded', 17=>'memory limit exceeded', 19=>'illegal system call', 20=>'internal error', }[result['result'].to_i]) end } end