ruby-eval-in 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/eval-in.rb +15 -14
- data/lib/eval-in/exceptions.rb +23 -21
- data/lib/eval-in/result.rb +67 -74
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73f75c21a90cfa0edbce574ce269414b66684172
|
4
|
+
data.tar.gz: 3003813f791edcf17e8d07916b4b9a4693ef76b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db4cdb798b4281821df3c2e2a44173b2b732876a966c9cb1c116a364f538c79449d8a2d7d096122e7dbe0a54852a1696339226fa7dc9c77bbbcfd85b6952eeaa
|
7
|
+
data.tar.gz: 8d607751ea5bc26659587e0c3729b3423a94b16116de5316e93fbf5a47b67600774d18bd5d6154b806708674f6655666c4f03c0026935a055849d2d2713fff92
|
data/lib/eval-in.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
#
|
2
|
-
# Author: William Woodruff
|
3
|
-
# A Ruby interface to the eval.in (https://eval.in) service.
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
require_relative "eval-in/result"
|
4
|
+
require_relative "eval-in/exceptions"
|
7
5
|
|
8
6
|
# The primary namespace.
|
9
7
|
# @author William Woodruff
|
10
8
|
# @since 0.0.1
|
11
9
|
module EvalIn
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
# The library's current version.
|
11
|
+
VERSION = "0.0.5"
|
12
|
+
|
13
|
+
# Evaluate some code in a given language.
|
14
|
+
# @example
|
15
|
+
# EvalIn.eval(:ruby, 'puts "Hello, World"') # => #<EvalIn::Result:0xNN>
|
16
|
+
# @param [Symbol, String] lang the language to execute in
|
17
|
+
# @param [String] code the program code to execute
|
18
|
+
# @return [EvalIn::Result] the execution results
|
19
|
+
def self.eval(lang, code)
|
20
|
+
EvalIn::Result.new(lang.to_s.downcase, code)
|
21
|
+
end
|
21
22
|
end
|
data/lib/eval-in/exceptions.rb
CHANGED
@@ -1,26 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module EvalIn
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
# A parent Error class for all eval-in errors.
|
5
|
+
class EvalInError < RuntimeError
|
6
|
+
end
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
# Raised when an error occurs during connection.
|
9
|
+
class ConnectionError < EvalInError
|
10
|
+
def initialize(error)
|
11
|
+
super "Connection error: #{error}"
|
12
|
+
end
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
# Raised when EvalIn.{EvalIn.eval} is given a bad input language.
|
16
|
+
class BadLanguageError < EvalInError
|
17
|
+
def initialize(lang)
|
18
|
+
super "No such language: #{lang}"
|
19
|
+
end
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
# Raised when EvalIn.{EvalIn.eval} is given blank or whitespace-only code.
|
23
|
+
class EmptyCodeError < EvalInError
|
24
|
+
def initialize
|
25
|
+
super "The code may not be empty"
|
26
|
+
end
|
27
|
+
end
|
26
28
|
end
|
data/lib/eval-in/result.rb
CHANGED
@@ -1,89 +1,82 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "nokogiri"
|
4
|
+
require "net/http"
|
5
|
+
require "uri"
|
4
6
|
|
5
7
|
module EvalIn
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
# A representation of a result produced by an eval.in query.
|
9
|
+
class Result
|
10
|
+
# @private
|
11
|
+
URL = URI("https://eval.in")
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
13
|
+
# The languages supported by eval.in.
|
14
|
+
# Any of these keys or values will work for the lang parameter in
|
15
|
+
# {EvalIn.eval}.
|
16
|
+
LANGS = {
|
17
|
+
"c" => "c/gcc-4.9.1",
|
18
|
+
"c++" => "c++/gcc-4.9.1",
|
19
|
+
"coffeescript" => "coffeescript/node-0.10.29-coffee-1.7.1",
|
20
|
+
"fortran" => "fortran/f95-4.4.3",
|
21
|
+
"haskell" => "haskell/hugs98-sep-2006",
|
22
|
+
"io" => "io/io-20131204",
|
23
|
+
"js" => "javascript/node-0.10.29",
|
24
|
+
"lua" => "lua/lua-5.2.3",
|
25
|
+
"ocaml" => "ocaml/ocaml-4.01.0",
|
26
|
+
"php5" => "php/php-5.6.23",
|
27
|
+
"php7" => "php/php-7.0.8",
|
28
|
+
"pascal" => "pascal/fpc-2.6.4",
|
29
|
+
"perl" => "perl/perl-5.20.0",
|
30
|
+
"python" => "python/cpython-3.4.1",
|
31
|
+
"python2" => "python/cpython-2.7.8",
|
32
|
+
"ruby" => "ruby/mri-2.4",
|
33
|
+
"slash" => "slash/slash-head",
|
34
|
+
"nasm" => "assembly/nasm-2.07",
|
35
|
+
}.freeze
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
# @return [String] the expanded language used in execution
|
38
|
+
# @example
|
39
|
+
# result.language # => "ruby/mri-2.2"
|
40
|
+
attr_reader :language
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
# @return [String] the program code used in execution
|
43
|
+
# @example
|
44
|
+
# result.code # => "puts \"hello\""
|
45
|
+
attr_reader :code
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
# @return [String] any output produced by the program
|
48
|
+
# @example
|
49
|
+
# result.output # => "hello\n"
|
50
|
+
attr_reader :output
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
52
|
+
# @return [String] the program's exit status
|
53
|
+
# @example
|
54
|
+
# result.status # => "OK (0 sec real, 0 sec wall, 8 MB, 16 syscalls)"
|
55
|
+
attr_reader :status
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
# @return [URI] a permalink to the output webpage
|
58
|
+
# @example
|
59
|
+
# result.url # => #<URI::HTTPS https://eval.in/xxxxxx>
|
60
|
+
attr_reader :url
|
58
61
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
# @private
|
63
|
+
def initialize(lang, code)
|
64
|
+
if LANGS.key?(lang)
|
65
|
+
lang = LANGS[lang]
|
66
|
+
elsif !LANGS.value?(lang)
|
67
|
+
raise BadLanguageError, lang
|
68
|
+
end
|
66
69
|
|
67
|
-
|
70
|
+
raise EmptyCodeError if code.strip.empty?
|
68
71
|
|
69
|
-
|
70
|
-
|
72
|
+
@lang = lang
|
73
|
+
@code = code
|
71
74
|
|
72
|
-
|
73
|
-
"execute" => "on",
|
74
|
-
"lang" => lang,
|
75
|
-
"code" => code
|
76
|
-
)
|
75
|
+
result = Net::HTTP.post_form(URL, "execute" => "on", "lang" => lang, "code" => code)
|
77
76
|
|
78
|
-
|
79
|
-
@url = URI(result["location"])
|
77
|
+
raise ConnectionError, result unless result.is_a? Net::HTTPFound
|
80
78
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
else
|
85
|
-
raise ConnectionError.new(result)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
79
|
+
@url = URI(result["location"])
|
80
|
+
end
|
81
|
+
end
|
89
82
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-eval-in
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Woodruff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
60
|
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
61
|
+
rubygems_version: 2.6.13
|
62
62
|
signing_key:
|
63
63
|
specification_version: 4
|
64
64
|
summary: ruby-eval-in - A Ruby interface to https://eval.in/.
|