web-repl 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16603912aa2b99c9c68ced749d4a3f6ef3a2ead1
4
- data.tar.gz: 4b3974407e3c741068f24fc2ea152acf5da8ee1c
3
+ metadata.gz: 0e6f59ae36066ce6cff2f0826db2749ff3929a57
4
+ data.tar.gz: 4eb7847fd7444a0846d5ae2dbfe40bd339e4a4e6
5
5
  SHA512:
6
- metadata.gz: 3a3e4c9ab844ffd58a8a174b6fad56f1e625b1d236b217268cda519dcd367c213a3cf0e69af0890f3d32570aec1db1506c22b4880e52983cd3b536558cdf280c
7
- data.tar.gz: 9c6781d65d49a485ef741ed1bd1d4e13587108c22eed7906152b043a58110188aab06b1a927a5596ae3c057929846dd698eef9d5b43ed75a57134b73d165ab66
6
+ metadata.gz: 657a2e5bba0fb94fe7c2162dfd30d2a436b918c6fe926948cb9fdb245f3cb834901df10c7a04069481954e28628221f815ef574a694e6d16c0e8dfcb024c6e42
7
+ data.tar.gz: bedcf0013f25f8e86d6d354fc0e6a84f37c45d97d764f06f5dcd27f51bd9dca1d60d3c5a1a3e90adef151b8b341f49802e5199eab2608f3b37639ca114eaee3d
@@ -0,0 +1 @@
1
+ function ReplConnection(e,t,n){n=typeof n==="undefined"?{}:n;this.debug=!!n.debug;this.reconnect=!!n.reconnect;this.retryTime=n.retryTime||1e3;this.evalFunction=n.evalFunction;this.host=e;this.port=t;this.active=false;this.socket;this.supported="WebSocket"in window;if(this.supported){console.log("REPL: socket ok")}else{console.log("REPL: socket not supported")}}ReplConnection.prototype.eval=function(statement){options=typeof options==="undefined"?{}:options;var response={};try{if(typeof this.evalFunction==="function"){response.value=this.evalFunction(statement)}else{response.value=eval(statement)}}catch(err){response.error=err.message}return response};ReplConnection.prototype.initSocket=function(e){if(this.socket!==undefined&&this.socket!==null){this.socket.close()}var t="ws://"+this.host+":"+this.port+"/echo";this.socket=new WebSocket(t);e()};ReplConnection.prototype.handleSocketOpen=function(){this.active=true;console.log("REPL: socket ready")};ReplConnection.prototype.tryConnection=function(){console.log("REPL: waiting for connection");if(this.reconnect){var e=this;window.setTimeout(function(){e.start()},this.retryTime)}};ReplConnection.prototype.handleSocketClose=function(e){if(!this.active){this.tryConnection()}else{console.log("REPL: socket closed");this.active=false;if(this.reconnect){this.tryConnection()}}};ReplConnection.prototype.convertTimestamp=function(e){var t=e.timestamp;e.timestamp=new Date(t);return e.timestamp};ReplConnection.prototype.handleMessageReceived=function(e){if(this.debug){console.log("REPL: message received");console.log(e)}var t=JSON.parse(e.data);this.convertTimestamp(t);var n=this.eval(t.statement);return this.send(n)};ReplConnection.prototype.send=function(e){var t=this.prepareJSON(e);if(this.debug){console.log("REPL: sending");console.log(t)}return this.socket.send(t)};ReplConnection.prototype.prepareJSON=function(e){var t;try{e.timestamp=(new Date).getTime();t=JSON.stringify(e)}catch(n){t=this.handleJsonError(e,n)}return t};ReplConnection.prototype.handleJsonError=function(e,t){e.value=null;e.error=err.message;return this.prepareJSONResponse(e)};ReplConnection.prototype.initEventHandling=function(){var e=this;this.socket.onopen=function(){e.handleSocketOpen()};this.socket.onclose=function(t){e.handleSocketClose(t)};this.socket.onmessage=function(t){e.handleMessageReceived(t)}};ReplConnection.prototype.start=function(e){if(this.supported){var t=this;this.initSocket(function(){t.initEventHandling();if(e!==undefined){e(t)}})}}
@@ -21,7 +21,7 @@ function ReplConnection(host, port, options) {
21
21
  // statement: the statement to evaluate
22
22
  ReplConnection.prototype.eval = function(statement) {
23
23
  options = (typeof options === "undefined") ? {} : options;
24
- response = {};
24
+ var response = {};
25
25
  try {
26
26
  if (typeof this.evalFunction === "function") {
27
27
  response.value = this.evalFunction(statement);
@@ -74,6 +74,13 @@ ReplConnection.prototype.handleSocketClose = function(event) {
74
74
  }
75
75
  }
76
76
 
77
+ // turn the timestamp from the rec'd message into a real date
78
+ ReplConnection.prototype.convertTimestamp = function(message) {
79
+ var timestamp = message.timestamp;
80
+ message.timestamp = new Date(timestamp);
81
+ return message.timestamp;
82
+ }
83
+
77
84
  // To be run when the Websocket registers an event over the connection.
78
85
  ReplConnection.prototype.handleMessageReceived = function(event) {
79
86
  if (this.debug) {
@@ -81,33 +88,41 @@ ReplConnection.prototype.handleMessageReceived = function(event) {
81
88
  console.log(event);
82
89
  }
83
90
  var message = JSON.parse(event.data);
84
- // turn the timestamp from the rec'd message into a real date
85
- var timestamp = message.timestamp;
86
- message.timestamp = new Date(timestamp);
87
- //
91
+ this.convertTimestamp(message);
88
92
  var response = this.eval(message.statement); // evaluate the statement
89
- var json = this.prepareJSONResponse(response)
93
+ return this.send(response);
94
+ }
95
+
96
+ // Send a message
97
+ ReplConnection.prototype.send = function(message) {
98
+ var json = this.prepareJSON(message);
90
99
  if (this.debug) {
91
- console.log("REPL: replying ");
100
+ console.log("REPL: sending");
92
101
  console.log(json);
93
102
  }
94
- this.socket.send(json);
103
+ return this.socket.send(json);
95
104
  }
96
105
 
97
- ReplConnection.prototype.prepareJSONResponse = function(response) {
106
+ // Convert a message to JSON for sending
107
+ ReplConnection.prototype.prepareJSON = function(message) {
98
108
  // prepare the response
99
109
  var json;
100
110
  try {
101
- response.timestamp = new Date().getTime(); // timestamp for the returned message
102
- json = JSON.stringify(response);
103
- } catch(err) {
104
- response.value = null;
105
- response.error = err.message;
106
- json = this.prepareJSONResponse(response)
111
+ message.timestamp = new Date().getTime(); // timestamp for the returned message
112
+ json = JSON.stringify(message);
113
+ } catch(error) {
114
+ json = this.handleJsonError(message, error);
107
115
  }
108
116
  return json;
109
117
  }
110
118
 
119
+ // Handle an error when converting the message to JSON
120
+ ReplConnection.prototype.handleJsonError = function(message, error) {
121
+ message.value = null;
122
+ message.error = err.message;
123
+ return this.prepareJSONResponse(message);
124
+ }
125
+
111
126
  // Initialize the Websocket event handling actions
112
127
  ReplConnection.prototype.initEventHandling = function() {
113
128
  var connection = this;
@@ -13,7 +13,7 @@ require "web-repl/repl"
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.4"
16
+ VERSION = "0.5"
17
17
 
18
18
  # Shortcut to REPL.start
19
19
  def self.start(*a)
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'
4
+ version: '0.5'
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-12 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -94,6 +94,7 @@ files:
94
94
  - js/replConnection-0.1.min.js
95
95
  - js/replConnection-0.3.min.js
96
96
  - js/replConnection-0.4.min.js
97
+ - js/replConnection-0.5.min.js
97
98
  - js/replConnection.js
98
99
  - lib/web-repl.rb
99
100
  - lib/web-repl/messager.rb