Class: Cinch::Plugins::EvalSo

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin, HTTParty
Defined in:
lib/cinch/plugins/evalso.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ EvalSo

Initializes the class and caches a list of languages from teh eval.so API



17
18
19
20
# File 'lib/cinch/plugins/evalso.rb', line 17

def initialize(*args)
    super
    @langs = self.class.get('/api/languages')
end

Instance Method Details

#eval(m, lang, code) ⇒ Object

Evaluate code using the eval.so API Params:

m

Cinch::Message object

lang

The language for code to be evaluated with

code

The code to be evaluated



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cinch/plugins/evalso.rb', line 34

def eval(m, lang, code)
    options = { :body => {:language => lang, :code => code }.to_json, :headers => { 'Content-Type' => 'application/json' }}
    res = self.class.post('/api/evaluate', options)
    # Eval.so brought back an error, print it out and return

    if res.has_key? 'error'
        m.reply 'Error: ' + res['error'], true
        return
    end

    # Print stderr if stdout is empty

    if res['stdout'].empty?
       output = res['stderr']
    else
       output = res['stdout']
    end

    # According to RFC 2812, the maximum line length on IRC is 510 characters, minus the carriage return

    # In order to not spam the channel, if the output is greater than one line, convert it to a gist

    if output.length > 510
        m.reply Gist.gist(output, filename: 'result', description: code)['html_url'], true
    else
        m.reply output, true
    end
end

#langs(m) ⇒ Object

Print out a list of languages Params:

m

Cinch::Message object



25
26
27
# File 'lib/cinch/plugins/evalso.rb', line 25

def langs(m)
    m.reply 'Available languages: ' + @langs.keys.join(', ')
end