eval_in 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5a172a314e520fe820972f9dbf6ba76c16a49ab
4
- data.tar.gz: a7a6a571f986ca1296dd389f7476bfce7ac92bee
3
+ metadata.gz: 8115bfc81c8a3463fefbffc45ec42c3332f97207
4
+ data.tar.gz: a95be59582154a4f6c3c513cc5d00d66fe76deca
5
5
  SHA512:
6
- metadata.gz: e2bbd5233868060a9d66f85e7ce75f8879276c8527d2d74587736eff267e78d5e31a5ce7a4c457076a54a5dfce1156b8c22a8cb5fddbdd596d1a44d1ea2763be
7
- data.tar.gz: 3c688f0b063b24c5f938ae0ba06c4cfd7f7fda897be7f1842782e9757c2b0b7597ec536aa49a88e484061d3af8aa5cf1bf53c69802191aa4bf645ad887eb9120
6
+ metadata.gz: 065c36bb0cd8d3636629364ccbbaeba014199399ca115f036e817b1e13e1031926a232d4caa846e490a5b6ffc13897a9d7bd092da26f834e2db6ed5a0c727a01
7
+ data.tar.gz: cac7e40b378bf637481e5d5be154f885a15fb8c4d7a1e22c61ca9e89816f80bde1389b94b63185be6163ff985ccec49fc7d7b512e2e912a01dcefa026d8ba29b
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  *.gem
2
+ .yardoc
3
+ doc
data/Readme.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://secure.travis-ci.org/JoshCheek/seeing_eval_in.png?branch=master)](http://travis-ci.org/JoshCheek/eval_in)
1
+ [![Build Status](https://secure.travis-ci.org/JoshCheek/eval_in.png?branch=master)](http://travis-ci.org/JoshCheek/eval_in)
2
2
 
3
3
  EvalIn
4
4
  ======
@@ -17,10 +17,11 @@ result = EvalIn.call 'puts "hello, #{gets}"', stdin: 'world', language: "ruby/mr
17
17
 
18
18
  result.output # => "hello, world\n"
19
19
  result.exitstatus # => 0
20
+ result.url # => "https://eval.in/182711.json"
20
21
  result.language # => "ruby/mri-2.1"
21
22
  result.language_friendly # => "Ruby — MRI 2.1"
22
23
  result.code # => "puts \"hello, \#{gets}\""
23
- result.status # => "OK (0.052 sec real, 0.059 sec wall, 9 MB, 21 syscalls)"
24
+ result.status # => "OK (0.064 sec real, 0.073 sec wall, 9 MB, 21 syscalls)"
24
25
  ```
25
26
 
26
27
 
@@ -1,3 +1,3 @@
1
1
  module EvalIn
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
data/lib/eval_in.rb CHANGED
@@ -10,7 +10,8 @@ module EvalIn
10
10
  RequestError = Class.new EvalInError
11
11
  ResultNotFound = Class.new EvalInError
12
12
 
13
- # curl https://eval.in | ruby -rnokogiri -e 'puts Nokogiri::HTML($stdin.read).css("option").map { |o| o["value"] }'
13
+ # @example Generated with
14
+ # curl https://eval.in | ruby -rnokogiri -e 'puts Nokogiri::HTML($stdin.read).css("option").map { |o| o["value"] }'
14
15
  KNOWN_LANGUAGES = %w[
15
16
  c/gcc-4.4.3
16
17
  c/gcc-4.9.1
@@ -39,8 +40,10 @@ module EvalIn
39
40
  assembly/nasm-2.07
40
41
  ]
41
42
 
43
+ # The data structure containing the final result
44
+ # its attributes default to null-objects for their given type
42
45
  class Result
43
- attr_accessor :exitstatus, :language, :language_friendly, :code, :output, :status
46
+ attr_accessor :exitstatus, :language, :language_friendly, :code, :output, :status, :url
44
47
 
45
48
  def initialize(attributes={})
46
49
  attributes = attributes.dup
@@ -50,26 +53,37 @@ module EvalIn
50
53
  self.code = attributes.delete(:code) || ""
51
54
  self.output = attributes.delete(:output) || ""
52
55
  self.status = attributes.delete(:status) || ""
56
+ self.url = attributes.delete(:url) || ""
53
57
  $stderr.puts "Unexpected attributes! #{attributes.keys.inspect}" if attributes.any?
54
58
  end
55
59
  end
56
60
 
61
+ # The primary way to use this library.
62
+ #
63
+ # @param code [String] the code to evaluate.
64
+ # @option options [String] :language Mandatory, a language recognized by eval.in, such as any value in {KNOWN_LANGUAGES}.
65
+ # @option options [String] :url Override the url to post the code to
66
+ # @option options [String] :stdin Will be passed as standard input to the script
67
+ # @option options [String] :context Will be included in the user agent
68
+ # @return [Result] the relevant data from the evaluated code.
57
69
  def self.call(code, options={})
58
70
  build_result get_code post_code(code, options)
59
71
  end
60
72
 
61
73
  def self.post_code(code, options)
62
- uri = URI(options.fetch(:url, "https://eval.in/"))
63
- input = options.fetch(:stdin, "")
64
- language = options.fetch(:language)
65
- path = uri.path
66
- path = '/' if path.empty?
74
+ uri = URI(options.fetch(:url, "https://eval.in/"))
75
+ input = options.fetch(:stdin, "")
76
+ language = options.fetch(:language)
77
+ user_agent = 'http://rubygems.org/gems/eval_in'
78
+ user_agent << " (#{options[:context]})" if options[:context]
79
+ path = uri.path
80
+ path = '/' if path.empty?
67
81
 
68
- # stole this out of implementation for post_form https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L503
82
+ # stole this out of implementation for post_form https://github.com/ruby/ruby/blob/2afed6eceff2951b949db7ded8167a75b431bad6/lib/net/http.rb#L503
69
83
  request = Net::HTTP::Post.new(path)
70
84
  request.form_data = {"utf8" => "√", "code" => code, "execute" => "on", "lang" => language, "input" => input}
71
- request['User-Agent'] = 'http://rubygems.org/gems/eval_in'
72
- req.basic_auth uri.user, uri.password if uri.user
85
+ request['User-Agent'] = user_agent
86
+ request.basic_auth uri.user, uri.password if uri.user
73
87
  net = Net::HTTP.new(uri.hostname, uri.port)
74
88
  # net.set_debug_output $stdout
75
89
  result = Net::HTTP.start(uri.hostname, uri.port, use_ssl: (uri.scheme == 'https')) { |http|
@@ -90,7 +104,7 @@ module EvalIn
90
104
 
91
105
  def self.get_code(location)
92
106
  if body = Net::HTTP.get(URI location)
93
- JSON.parse body
107
+ JSON.parse(body).merge('url' => location)
94
108
  else
95
109
  raise ResultNotFound, "No json at #{location.inspect}"
96
110
  end
@@ -109,6 +123,7 @@ module EvalIn
109
123
  language_friendly: response_json['lang_friendly'],
110
124
  code: response_json['code'],
111
125
  output: response_json['output'],
112
- status: response_json['status']
126
+ status: response_json['status'],
127
+ url: response_json['url']
113
128
  end
114
129
  end
data/spec/eval_in_spec.rb CHANGED
@@ -28,13 +28,14 @@ RSpec.describe EvalIn, integration: true do
28
28
  end
29
29
 
30
30
  it 'evaluates Ruby code through eval.in' do
31
- result = EvalIn.call 'print "hello, #{gets}"', stdin: "world", language: "ruby/mri-1.9.3"
31
+ result = EvalIn.call 'print "hello, #{gets}"', stdin: "world", language: "ruby/mri-1.9.3", context: 'eval_in integration test'
32
32
  expect(result.exitstatus ).to eq 0
33
33
  expect(result.language ).to eq "ruby/mri-1.9.3"
34
34
  expect(result.language_friendly).to eq "Ruby — MRI 1.9.3"
35
35
  expect(result.code ).to eq 'print "hello, #{gets}"'
36
36
  expect(result.output ).to eq "hello, world"
37
37
  expect(result.status ).to match /OK \([\d.]+ sec real, [\d.]+ sec wall, \d MB, \d+ syscalls\)/
38
+ expect(result.url ).to match %r(https://eval.in/\d+.json)
38
39
  end
39
40
  end
40
41
 
@@ -62,18 +63,21 @@ RSpec.describe EvalIn::Result do
62
63
  language_friendly: '',
63
64
  code: '',
64
65
  output: '',
65
- status: ''
66
+ status: '',
67
+ url: ''
66
68
  assert_result EvalIn::Result.new(language: nil,
67
69
  language_friendly: nil,
68
70
  code: nil,
69
71
  output: nil,
70
- status: nil),
72
+ status: nil,
73
+ url: nil),
71
74
  exitstatus: -1,
72
75
  language: '',
73
76
  language_friendly: '',
74
77
  code: '',
75
78
  output: '',
76
- status: ''
79
+ status: '',
80
+ url: ''
77
81
  end
78
82
 
79
83
  it 'doesn\'t mutate the input attributes' do
@@ -125,13 +129,20 @@ RSpec.describe 'post_code' do
125
129
  EvalIn.post_code code, stdin: stdin, language: language
126
130
  end
127
131
 
128
- it 'sets the user agent to its gem homepage' do
132
+ it 'defaults the user agent to its gem homepage' do
129
133
  stub_request(:post, url)
130
134
  .with(headers: {'User-Agent' => 'http://rubygems.org/gems/eval_in'})
131
135
  .to_return(status: 302, headers: {'Location' => result_location})
132
136
  EvalIn.post_code code, language: language
133
137
  end
134
138
 
139
+ it 'can add a context to the user agent' do
140
+ stub_request(:post, url)
141
+ .with(headers: {'User-Agent' => 'http://rubygems.org/gems/eval_in (some context)'})
142
+ .to_return(status: 302, headers: {'Location' => result_location})
143
+ EvalIn.post_code code, language: language, context: 'some context'
144
+ end
145
+
135
146
  it 'returns the redirect location jsonified' do
136
147
  stub_eval_in expected_data
137
148
  result = EvalIn.post_code code, stdin: stdin, language: language
@@ -160,6 +171,12 @@ RSpec.describe 'post_code' do
160
171
  EvalIn.post_code code, url: 'http://example.com', stdin: stdin, language: language
161
172
  end
162
173
 
174
+ it 'supports basic http auth' do
175
+ url.replace "http://user:pass@example.com"
176
+ stub_eval_in
177
+ EvalIn.post_code code, url: 'http://user:pass@example.com', stdin: stdin, language: language
178
+ end
179
+
163
180
  context 'when it gets a non-redirect' do
164
181
  it 'informs user of language provided and languages known if language is unknown' do
165
182
  stub_request(:post, "https://eval.in/").to_return(status: 406)
@@ -192,24 +209,31 @@ RSpec.describe 'get_code' do
192
209
  it 'queries the location, and inflates the json' do
193
210
  stub_eval_in(url: "http://example.com/some-result.json")
194
211
  result = EvalIn.get_code "http://example.com/some-result.json"
195
- expect(result).to eq ruby_result
212
+ expect(result).to match hash_including(ruby_result)
196
213
  end
197
214
 
198
215
  it 'raises an error when it gets a non-200' do
199
- stub_eval_in json_result: '', url: 'http://whatever.com'
200
- expect { EvalIn.get_code "http://whatever.com" }.to \
201
- raise_error EvalIn::ResultNotFound, %r(http://whatever.com)
216
+ stub_eval_in json_result: '', url: 'http://example.com'
217
+ expect { EvalIn.get_code "http://example.com" }.to \
218
+ raise_error EvalIn::ResultNotFound, %r(http://example.com)
219
+ end
220
+
221
+ it 'adds the url to the result' do
222
+ stub_eval_in url: 'http://example.com'
223
+ result = EvalIn.get_code 'http://example.com'
224
+ expect(result['url']).to eq 'http://example.com'
202
225
  end
203
226
  end
204
227
 
205
228
 
206
229
  RSpec.describe 'build_result' do
207
- let(:language) { 'some lang' }
230
+ let(:language) { 'some lang' }
208
231
  let(:language_friendly) { 'some lang friendly' }
209
- let(:code) { 'some code' }
210
- let(:output) { 'some output' }
211
- let(:status) { 'some status' }
212
- let(:response_json) { {'lang' => language, 'lang_friendly' => language_friendly, 'code' => code, 'output' => output, 'status' => status} }
232
+ let(:code) { 'some code' }
233
+ let(:output) { 'some output' }
234
+ let(:status) { 'some status' }
235
+ let(:url) { 'some url' }
236
+ let(:response_json) { {'lang' => language, 'lang_friendly' => language_friendly, 'code' => code, 'output' => output, 'status' => status, 'url' => 'some url'} }
213
237
 
214
238
  it 'returns a response for the given response json' do
215
239
  result = EvalIn.build_result response_json
@@ -219,7 +243,8 @@ RSpec.describe 'build_result' do
219
243
  language_friendly: language_friendly,
220
244
  code: code,
221
245
  output: output,
222
- status: status
246
+ status: status,
247
+ url: url
223
248
  end
224
249
 
225
250
  # exit: https://eval.in/182586.json
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eval_in
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Cheek
@@ -11,28 +11,28 @@ cert_chain: []
11
11
  date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ name: rspec
14
15
  requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - "~>"
17
18
  - !ruby/object:Gem::Version
18
19
  version: '3.0'
19
- prerelease: false
20
- name: rspec
21
20
  type: :development
21
+ prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
+ name: webmock
28
29
  requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - "~>"
31
32
  - !ruby/object:Gem::Version
32
33
  version: '1.18'
33
- prerelease: false
34
- name: webmock
35
34
  type: :development
35
+ prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
@@ -77,7 +77,6 @@ files:
77
77
  - ".gitignore"
78
78
  - ".travis.yml"
79
79
  - Gemfile
80
- - Gemfile.lock
81
80
  - Readme.md
82
81
  - eval_in.gemspec
83
82
  - lib/eval_in.rb
data/Gemfile.lock DELETED
@@ -1,37 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- eval_in (0.1.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- addressable (2.3.6)
10
- crack (0.4.2)
11
- safe_yaml (~> 1.0.0)
12
- diff-lcs (1.2.5)
13
- rspec (3.0.0)
14
- rspec-core (~> 3.0.0)
15
- rspec-expectations (~> 3.0.0)
16
- rspec-mocks (~> 3.0.0)
17
- rspec-core (3.0.4)
18
- rspec-support (~> 3.0.0)
19
- rspec-expectations (3.0.4)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.0.0)
22
- rspec-mocks (3.0.4)
23
- rspec-support (~> 3.0.0)
24
- rspec-support (3.0.4)
25
- safe_yaml (1.0.3)
26
- webmock (1.18.0)
27
- addressable (>= 2.3.6)
28
- crack (>= 0.3.2)
29
-
30
- PLATFORMS
31
- java
32
- ruby
33
-
34
- DEPENDENCIES
35
- eval_in!
36
- rspec (~> 3.0)
37
- webmock (~> 1.18)