web-repl 0.6 → 0.7
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/web-repl.rb +2 -2
- data/lib/web-repl/{messager.rb → messenger.rb} +1 -1
- data/lib/web-repl/repl.rb +45 -28
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4e0434060703c3bc75cf67d85d2897d4990da1a
|
4
|
+
data.tar.gz: 0378db709e99f75dc4a9e83cf6c017b6159103ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 814a12b0fb7b606f3d84e4879f570a9f0e9e38c141ef0e58d4bb57120db97e8019181f564d4bba74e7f4e2d27c5d12707b646b97f846dc31703200c10ce1bbc2
|
7
|
+
data.tar.gz: 8fcdf3dd69be65c74111f2acaba1400808a50a1d42fde538125259e12de002c5bbb41b7bbe41516f4dfef6bcf2c67a20e790af1f3800a5d16f0ac5e4348bfa33
|
data/lib/web-repl.rb
CHANGED
@@ -6,14 +6,14 @@ require "readline"
|
|
6
6
|
require "socket"
|
7
7
|
|
8
8
|
# classes
|
9
|
-
require "web-repl/
|
9
|
+
require "web-repl/messenger"
|
10
10
|
require "web-repl/patch"
|
11
11
|
require "web-repl/repl"
|
12
12
|
|
13
13
|
# A Javascript REPL that runs in Ruby. Evaluation is done by a web browser instance.
|
14
14
|
module WebRepl
|
15
15
|
|
16
|
-
VERSION = "0.
|
16
|
+
VERSION = "0.7"
|
17
17
|
|
18
18
|
# Shortcut to REPL.start
|
19
19
|
def self.start(*a)
|
data/lib/web-repl/repl.rb
CHANGED
@@ -21,7 +21,7 @@ module WebRepl
|
|
21
21
|
def initialize(config, options = {})
|
22
22
|
@config = config
|
23
23
|
@socket = nil
|
24
|
-
@
|
24
|
+
@messenger = nil
|
25
25
|
@buffer = []
|
26
26
|
@debug = options[:debug]
|
27
27
|
end
|
@@ -30,28 +30,54 @@ module WebRepl
|
|
30
30
|
# @param [Fixnum, String] statement A Javascript statement to be evaluated
|
31
31
|
# @return [String, nil] The data that was sent to the browser, or nil if sending could not be completed.
|
32
32
|
def evaluate(statement)
|
33
|
-
@
|
33
|
+
@messenger.out({ :statement => statement }) unless @messenger.nil?
|
34
34
|
end
|
35
35
|
|
36
36
|
# Prompt the Ruby user for input and send that input to the browser for evaluation (blocking)
|
37
37
|
# @return [String, nil] The data that was sent to the browser, or nil if sending could not be completed
|
38
|
-
def
|
38
|
+
def input(options = {})
|
39
39
|
line = Readline.readline('> ', true)
|
40
|
-
|
41
|
-
if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
|
40
|
+
if invalid_input?(line)
|
42
41
|
Readline::HISTORY.pop
|
43
|
-
|
44
|
-
statement = line.strip
|
45
|
-
case statement
|
46
|
-
when "exit" then exit
|
42
|
+
input(options)
|
47
43
|
else
|
48
|
-
|
44
|
+
Readline::HISTORY.pop if repeat_input?(line)
|
45
|
+
statement = line.strip
|
46
|
+
case statement
|
47
|
+
when "exit", "quit" then exit
|
48
|
+
else
|
49
|
+
evaluate(statement)
|
50
|
+
wait_for_response(options)
|
51
|
+
end
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|
55
|
+
def repeat_input?(line)
|
56
|
+
line == Readline::HISTORY.to_a[-2]
|
57
|
+
end
|
58
|
+
|
59
|
+
def invalid_input?(line)
|
60
|
+
line.nil? || line =~ /^\s*$/
|
61
|
+
end
|
62
|
+
|
52
63
|
# Wait for a response from the browser
|
53
|
-
def wait_for_response
|
54
|
-
|
64
|
+
def wait_for_response(options)
|
65
|
+
until !@buffer.empty? do
|
66
|
+
end
|
67
|
+
@buffer.each { |resp| puts_message(resp) }
|
68
|
+
@buffer.clear
|
69
|
+
input unless !!options[:background]
|
70
|
+
end
|
71
|
+
|
72
|
+
def puts_message(message)
|
73
|
+
keys = { :error => :red, :value => :white }
|
74
|
+
text = nil
|
75
|
+
keys.each do |k,v|
|
76
|
+
text ||= message[k].to_s.send(v) unless message[k].nil?
|
77
|
+
end
|
78
|
+
text ||= "(void)"
|
79
|
+
puts(text)
|
80
|
+
text
|
55
81
|
end
|
56
82
|
|
57
83
|
# Start the Websocket connection (blocking)
|
@@ -62,25 +88,24 @@ module WebRepl
|
|
62
88
|
EM::WebSocket.run(@config) do |ws|
|
63
89
|
if @socket.nil?
|
64
90
|
@socket = ws
|
65
|
-
@
|
91
|
+
@messenger = Messenger.new(@socket)
|
66
92
|
configure_event_handling(:background => options[:background], &block)
|
67
93
|
end
|
68
94
|
end
|
69
95
|
end
|
96
|
+
Thread.abort_on_exception = true
|
70
97
|
acknowledge_handshake do
|
71
98
|
yield if block_given?
|
72
|
-
|
99
|
+
input(options) unless !!options[:background]
|
73
100
|
end
|
74
|
-
|
101
|
+
#@thread.join unless !!options[:background]
|
75
102
|
end
|
76
103
|
|
77
104
|
# Execute a block when a connection is made
|
78
105
|
# @return [TrueClass]
|
79
106
|
def acknowledge_handshake(&block)
|
80
|
-
|
81
|
-
|
82
|
-
yield
|
83
|
-
end
|
107
|
+
loop until !@handshake.nil?
|
108
|
+
yield
|
84
109
|
end
|
85
110
|
|
86
111
|
# Close the REPL
|
@@ -102,17 +127,9 @@ module WebRepl
|
|
102
127
|
end
|
103
128
|
|
104
129
|
def handle_message_received(raw_message, options = {})
|
105
|
-
@
|
106
|
-
@buffer.clear
|
130
|
+
@messenger.in(raw_message) do |message|
|
107
131
|
@buffer << message
|
108
|
-
keys = { :error => :red, :value => :white }
|
109
|
-
text = nil
|
110
|
-
keys.each do |k,v|
|
111
|
-
text ||= message[k].to_s.send(v) unless message[k].nil?
|
112
|
-
end
|
113
|
-
puts(text)
|
114
132
|
end
|
115
|
-
gets unless !!options[:background]
|
116
133
|
end
|
117
134
|
|
118
135
|
# Configure the Websocket event handling
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web-repl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Russo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rb-readline
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: mocha
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,7 +112,7 @@ files:
|
|
98
112
|
- js/replConnection-0.6.min.js
|
99
113
|
- js/replConnection.js
|
100
114
|
- lib/web-repl.rb
|
101
|
-
- lib/web-repl/
|
115
|
+
- lib/web-repl/messenger.rb
|
102
116
|
- lib/web-repl/patch.rb
|
103
117
|
- lib/web-repl/repl.rb
|
104
118
|
- test/helper.rb
|