cinch-eval-in 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +33 -0
  4. data/lib/cinch/plugins/eval_in.rb +102 -0
  5. metadata +61 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 396f1d7eb94096958f1efd233d3461f45e890c2a
4
+ data.tar.gz: edc3f105f659152248d95e689cb97b59848c5b6f
5
+ SHA512:
6
+ metadata.gz: 9a1104e561a684f7075864cdbb01c46e9a16ca3a10f2ecc5459c4821ac00750d5b60b6c152813984bf72f3bc65c1dd8d4a88fe8d9ebb901a562528e253ea0c72
7
+ data.tar.gz: 5866b1ce0986fdd10c158cf246641db02a7bfbc1d5f8a1773eb9d731541c95f36f7362bcae5057ad866eed401c926e6beba465686d504867d1f3ef1b70dfd7ad
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ cinch-eval-in - Copyright (c) 2014 - Mon ouïe
2
+
3
+ This software is provided 'as-is', without any express or
4
+ implied warranty. In no event will the authors be held
5
+ liable for any damages arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute
9
+ it freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented;
12
+ you must not claim that you wrote the original software.
13
+ If you use this software in a product, an acknowledgment
14
+ in the product documentation would be appreciated but
15
+ is not required.
16
+
17
+ 2. Altered source versions must be plainly marked as such,
18
+ and must not be misrepresented as being the original software.
19
+
20
+ 3. This notice may not be removed or altered from any
21
+ source distribution.
@@ -0,0 +1,33 @@
1
+ Eval.in plugin
2
+ ==============
3
+
4
+ This is a plugin that allows users to evaluate Ruby code on
5
+ <https://eval.in/>. It can be sent queries as follow:
6
+
7
+ >> "Hello world"'
8
+ 10>> TRUE.type # Ruby 1.0 # MRI 1.0
9
+ 18>> true.respond_to? :type # MRI 1.8.7
10
+ 19>> true.type # MRI 1.9.3
11
+ 20>> false.class # MRI 2.0.0
12
+ 21>> 'strange'.length # MRI 2.1
13
+
14
+ Installation
15
+ ------------
16
+
17
+ To install this, run the following command:
18
+
19
+ gem install cinch-eval-in
20
+
21
+ Here's an example of how to use it:
22
+
23
+ require "cinch"
24
+ require "cinch/plugins/eval_in"
25
+
26
+ bot = Cinch::Bot.new do
27
+ configure do |c|
28
+ # You can of course add more plugins and other options here.
29
+ c.plugins.plugins = [Cinch::Plugins::EvalIn]
30
+ end
31
+ end
32
+
33
+ bot.start
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cinch'
3
+
4
+ require 'uri'
5
+ require 'net/http'
6
+ require 'nokogiri'
7
+
8
+ module Cinch::Plugins
9
+ class EvalIn
10
+ include Cinch::Plugin
11
+ match(/\A(\d*)>>(.*)\z/, :prefix => "")
12
+
13
+ class FormatError < StandardError; end
14
+ class CommunicationError < StandardError; end
15
+
16
+ ServiceURI = URI("https://eval.in/")
17
+
18
+ KnownVersions = {
19
+ "10" => "ruby/mri-1.0",
20
+ "18" => "ruby/mri-1.8.7",
21
+ "19" => "ruby/mri-1.9.3",
22
+ "20" => "ruby/mri-2.0.0",
23
+ "21" => "ruby/mri-2.1",
24
+ }
25
+
26
+ DefaultVersion = "21"
27
+
28
+ MaxLength = 80
29
+
30
+ def execute(m, version, code)
31
+ version = DefaultVersion if version.empty?
32
+
33
+ if !KnownVersions.has_key?(version)
34
+ m.reply("Unknown version number --- #{version}", true)
35
+ else
36
+ m.reply("=> #{evaluate(KnownVersions[version], code)}", true)
37
+ end
38
+ rescue
39
+ m.reply("Something went wrong when trying to evaluate your code.", true)
40
+ raise $! # Cinch takes care of logging exceptions
41
+ end
42
+
43
+ def evaluate(version, code)
44
+ templated_code = if version == "ruby/mri-1.0"
45
+ old_template(code)
46
+ else
47
+ new_template(code)
48
+ end
49
+
50
+ result = Net::HTTP.post_form(ServiceURI,
51
+ "utf8" => "λ",
52
+ "code" => templated_code,
53
+ "execute" => "on",
54
+ "lang" => version,
55
+ "input" => "")
56
+
57
+ if result.is_a? Net::HTTPFound
58
+ location = URI(result['location'])
59
+ location.scheme = "https"
60
+ location.port = 443
61
+
62
+ body = Nokogiri(Net::HTTP.get(location))
63
+
64
+ if output_title = body.at_xpath("*//h2[text()='Program Output']")
65
+ output = output_title.next_element.text
66
+ first_line = output.each_line.first.chomp
67
+ needs_ellipsis = output.each_line.count > 1 ||
68
+ first_line.length > MaxLength
69
+
70
+ "#{first_line[0, MaxLength]}#{'...' if needs_ellipsis} "\
71
+ "(#{location})"
72
+ else
73
+ raise FormatError, "couldn't find program output"
74
+ end
75
+ else
76
+ raise CommunicationError, result
77
+ end
78
+ end
79
+
80
+ def old_template(code)
81
+ <<eot
82
+ begin
83
+ $stdout.puts((#{code}).inspect)
84
+ rescue Exception
85
+ $stderr.puts "\#{$!.type}: \#{$!}"
86
+ end
87
+ eot
88
+ end
89
+
90
+ def new_template(code)
91
+ <<eot
92
+ begin
93
+ puts eval(DATA.read).inspect
94
+ rescue Exception => e
95
+ $stderr.puts "\#{e.class}: \#{e}"
96
+ end
97
+ __END__
98
+ #{code}
99
+ eot
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-eval-in
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mon ouie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cinch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ description: A Cinch plug-in to evaluate Ruby code on https://eval.in.
28
+ email: mon.ouie@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/cinch/plugins/eval_in.rb
36
+ homepage: http://github.com/Mon-Ouie/cinch-eval-in
37
+ licenses:
38
+ - zlib
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A Cinch plug-in to evaluate Ruby code on https://eval.in.
60
+ test_files: []
61
+ has_rdoc: