-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_in.rb
86 lines (70 loc) · 1.81 KB
/
eval_in.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# encoding: utf-8
require 'cinch'
require 'httparty'
require 'nokogiri'
class Cinch::Plugins::EvalIn
include Cinch::Plugin
set :prefix, /^>>/
match(/(\S*)? (.*)/)
def execute(m, lang, code)
lang = lang.length > 0 ? lang.to_sym : :ruby
res = API.new.evaluate(lang, code)
m.reply "#{res.url} >> #{res.output}"
rescue => e
m.reply "eval error >> #{e.message}"
end
class NokogiriParser < HTTParty::Parser
SupportedFormats.merge!('text/html' => :html)
def html
Nokogiri::HTML(body)
end
end
class Result
attr_reader :output, :url
def initialize(response)
@output = response.css('.highlighttable').
first.
next_element.
next_element.
text.chomp("\n")
@url = response.request.last_uri
end
def to_s
output
end
end
class UnknownLangError < StandardError; end
class API
include HTTParty
base_uri 'https://eval.in'
parser NokogiriParser
OPTIONS = {
:body => {
:lang => 'ruby/mri-2.0.0',
:execute => "on",
:input => "",
:utf8 => "λ"
},
:headers => {
'User-Agent' => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"
}
}
LANGS = {
ruby: "ruby/mri-2.0.0",
ruby19: "ruby/mri-1.9.3",
javascript: "javascript/node-0.8.8",
php: "php/php-5.4.6",
perl: "perl/perl-5.16.1",
python: "python/cpython-2.7.3",
python3: "python/cpython-3.2.3",
}
def evaluate(lang, code)
LANGS.has_key?(lang) or \
raise UnknownLangError, "Unknown language #{lang}"
options = OPTIONS.dup
options[:body][:lang] = LANGS.fetch lang
options[:body][:code] = code
Result.new self.class.post('/', options)
end
end
end