Class: Ruboty::Handlers::Paizaio

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/paizaio.rb

Constant Summary collapse

LANGUAGES =
[
  "c",
  "cpp",
  "objective-c",
  "java",
  "scala",
				  "swift",
  "csharp",
  "go",
  "haskell",
  "erlang",
  "perl",
  "python",
  "python3",
  "ruby",
  "php",
  "bash",
  "r",
  "javascript",
  "coffeescript",
  "vb",
  "cobol",
  "fsharp",
  "d",
  "clojure",
  "mysql"
]

Instance Method Summary collapse

Constructor Details

#initialize(*__reserved__) ⇒ Paizaio

Returns a new instance of Paizaio.



39
40
41
42
43
44
45
# File 'lib/ruboty/handlers/paizaio.rb', line 39

def initialize(*__reserved__)
	super
	@input=nil
	@current_submission=nil

	@languages=LANGUAGES
end

Instance Method Details

#languages(message) ⇒ Object



57
58
59
# File 'lib/ruboty/handlers/paizaio.rb', line 57

def languages(message)
	message.reply @languages.map{|e|e+"\n"}.join
end

#read_uri(uri) ⇒ Object



46
47
48
49
50
51
# File 'lib/ruboty/handlers/paizaio.rb', line 46

def read_uri(uri)
	return nil if !uri||uri.empty?
	Kernel.open(uri){|f|
		return f.read
	}
end

#setinput(message) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ruboty/handlers/paizaio.rb', line 60

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) ⇒ Object



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
# File 'lib/ruboty/handlers/paizaio.rb', line 70

def submit(message)
	#language: 言語名(文字列)記号類を除いて最大先頭一致のものを使用する。
	#source_uri: ソースファイル
	#input_uri: 入力ファイル(空文字列ならsetinputの内容を使用)
	input=message[:input_uri]&&!message[:input_uri].empty? ? read_uri(message[:input_uri]) : @input
	#guess lang
	lang=message[:language].downcase.gsub(/[\s\(\)\.]/,'')
	lang=@languages.max_by{|e|
		_e=e.downcase.gsub(/[\s\(\)\.]/,'')
		lang.size.downto(1).find{|i|_e.start_with?(lang[0,i])}||-1
	}

	json={
		language: lang,
		source_code: read_uri(message[:source_uri]),
		input: input,
		longpoll: true,
		longpoll_timeout: 10.0,
	}
	uri=URI.parse('http://api.paiza.io/runners/create')
	Net::HTTP.start(uri.host,uri.port){|http|
		resp=http.post(uri.path,JSON.generate(json),{
			'Content-Type'=>'application/json',
		})
		json=JSON.parse(resp.body)
		#p json
		@current_submission=json['id']
		message.reply 'http://api.paiza.io/runners/get_details?id='+@current_submission
	}
end

#view(message) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruboty/handlers/paizaio.rb', line 100

def view(message)
	#id: paiza.io ID(空文字列なら直前のsubmitで返されたIDを使用)
	#なお、api.paiza.ioのエントリはpaiza.ioのエントリとは異なる模様。コード一覧にも出てこない。
	submission=message[:id]&&!message[:id].empty? ? message[:id] : @current_submission
	resp=JSON.parse Net::HTTP.get URI.parse 'http://api.paiza.io/runners/get_details?id='+submission
	if resp['status']=='running'
		message.reply '[Ruboty::Paizaio] running'
	elsif resp['build_exit_code']!=0
		message.reply '[Ruboty::Paizaio] compile error'
	else
		message.reply resp['stdout']
	end
end