websocket-gui 0.0.1 → 0.0.2

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: 0f9b2ada3cef153c8ebd85ff255e143068f21dcd
4
- data.tar.gz: 0d95a12c9c9fdf239b5f9f4ca26d598766a600cd
3
+ metadata.gz: 810ab8cd2d4babe30b454e23331a26ca3157098a
4
+ data.tar.gz: 7c52fdad5960f6f53b9edd756e1f8619d890371e
5
5
  SHA512:
6
- metadata.gz: b1ba9084cbfcd657ea4e3cda19fbf071ce5497925d2537ca5d219b30b7038eb4c225dfbe4efbb1a7fe753df9721953b8d110a4843fb2a5776e4aed2efe289fc8
7
- data.tar.gz: 7bf16018e44381dded3b25ea882ed0155d9339b6bdc631ff832ec8c8078ae9e927ab04270d47150dfbbe2f8e0d031225c075e71be51a49207d4b8a7c5ccc05f5
6
+ metadata.gz: 8580c9917dc555e4a3e04debbe011a9dfcd51051705f2ad10ae52d2fde2f905c3db2fecf4af7c04b6b1088a7ce64b43d10739ad6c3fe2946404b2048a279a9ea
7
+ data.tar.gz: 695ae7d48e95d493b0d7feffea44e66b03227b6b05beba145f5a2efddf09728b68ba0afd95c11c44fb5ce47537b38c8a1b25c4282f32e65a4731d880e05228ad
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Websocket Gui
2
- This gem uses Sinatra, EventMachine, and Web Sockets to make it easy to use the browser as a GUI for Ruby apps that
2
+ This gem uses Sinatra, EventMachine, and WebSockets to make it easy to use the browser as a GUI for Ruby apps that
3
3
  would otherwise be stuck in the console.
4
4
 
5
5
 
@@ -9,12 +9,12 @@ To use this gem, make a class that extends WebsocketGui::Base and a HTML/JS file
9
9
 
10
10
  ### Configuration
11
11
  The following configuration options are available:
12
- :socket_port (8080)
13
- :socket_host (127.0.0.1)
14
- :http_port (3000)
15
- :http_host (127.0.0.1)
16
- :view (:index)
17
- :tick_interval (nil)
12
+ * :socket\_port (8080)
13
+ * :socket\_host (127.0.0.1)
14
+ * :http\_port (3000)
15
+ * :http\_host (127.0.0.1)
16
+ * :view (:index)
17
+ * :tick\_interval (nil)
18
18
 
19
19
  The options can be set in several ways, each level overriding the previous:
20
20
  1 In declaring your subclass of WebsocketGui::Base (like tick\_interval below)
@@ -22,7 +22,7 @@ The options can be set in several ways, each level overriding the previous:
22
22
  1 By passing an options hash to the run! method
23
23
 
24
24
 
25
- ### project-root/app.rb
25
+ ### Sample Code: project-root/app.rb
26
26
  require 'websocket-gui'
27
27
 
28
28
  class App < WebsocketGui::Base
@@ -57,6 +57,16 @@ The options can be set in several ways, each level overriding the previous:
57
57
  on_socket_close do
58
58
  puts "Socket closed."
59
59
  end
60
+
61
+ # custom handler
62
+ # trigger by sending JSON like this from the client:
63
+ # { event: 'custom_buttom_click', params: { ...} }
64
+ on_custom_button_click do |params|
65
+ socket_send "Custom event triggered! Params provided were:"
66
+ params.each do |k,v|
67
+ socket_send "#{k} = #{v}"
68
+ end
69
+ end
60
70
  end
61
71
 
62
72
  # specify options in the constructor and/or the run method.
@@ -65,7 +75,7 @@ The options can be set in several ways, each level overriding the previous:
65
75
 
66
76
 
67
77
 
68
- ### project-root/index.erb
78
+ ### Sample Code: project-root/index.erb
69
79
 
70
80
  <!doctype html>
71
81
  <html>
@@ -93,6 +103,12 @@ The options can be set in several ways, each level overriding the previous:
93
103
  var writeMessage = function (msg) {
94
104
  $("<li></li>").text (msg).appendTo ($("#output"));
95
105
  };
106
+ var sendEvent = function (event, params) {
107
+ socket.send (JSON.stringify ({
108
+ event: event,
109
+ params: params
110
+ }));
111
+ };
96
112
  socket.onmessage = function (evt) {
97
113
  writeMessage (evt.data);
98
114
  };
@@ -115,6 +131,13 @@ The options can be set in several ways, each level overriding the previous:
115
131
  input.val ('');
116
132
  }
117
133
  });
134
+ $("#custom").on ('click', function () {
135
+ sendEvent('custom_button_click', {
136
+ a: 1,
137
+ b: "Two",
138
+ c: "III"
139
+ });
140
+ });
118
141
  }
119
142
  $("#connect").on ('click', function () { connect (); } );
120
143
  $("#disconnect").on ('click', function () { socket.close (); });
@@ -137,7 +160,3 @@ The options can be set in several ways, each level overriding the previous:
137
160
  This will start the socket server and sinatra, and launch a browser with your view.
138
161
 
139
162
 
140
- ## todo
141
- * Create a small JS library that wraps the native websocket and makes it easy to post events to the server.
142
- * Expand event handlers so you can write events like `on_start do |params|` instead of having to do parse raw strings and do everything in on\_socket\_recv
143
-
data/examples/example.rb CHANGED
@@ -22,6 +22,14 @@ class ExampleWebSocketApp < WebsocketGui::Base
22
22
  on_socket_close do
23
23
  puts "Socket closed."
24
24
  end
25
+
26
+ # custom handler
27
+ on_custom_button_click do |params|
28
+ socket_send "Custom event triggered! Params provided were:"
29
+ params.each do |k,v|
30
+ socket_send "#{k} = #{v}"
31
+ end
32
+ end
25
33
  end
26
34
 
27
35
  if __FILE__ == $0
data/examples/index.erb CHANGED
@@ -13,11 +13,19 @@
13
13
 
14
14
  <body>
15
15
  <div id="connectedContainer" style="display:none;">
16
- <textarea id="input"></textarea>
17
- <input type="button" id="send" value="Send" />
16
+ <textarea id="input" rows="4" cols="40"></textarea>
17
+ <p>
18
+ <input type="button" id="send" value="Send" />
19
+ </p>
20
+ <p>
21
+ <input type="button" id="custom" value="Send Custom Event" />
22
+ </p>
18
23
 
19
24
  <ul id="output"></ul>
20
- <input type="button" id="disconnect" value="Disconnect" />
25
+
26
+ <p>
27
+ <input type="button" id="disconnect" value="Disconnect" />
28
+ </p>
21
29
  </div>
22
30
  <div id="notConnectedContainer">
23
31
  <input type="button" id="connect" value="Connect" />
@@ -30,6 +38,12 @@
30
38
  var writeMessage = function (msg) {
31
39
  $("<li></li>").text (msg).appendTo ($("#output"));
32
40
  };
41
+ var sendEvent = function (event, params) {
42
+ socket.send (JSON.stringify ({
43
+ event: event,
44
+ params: params
45
+ }));
46
+ };
33
47
  socket.onmessage = function (evt) {
34
48
  writeMessage (evt.data);
35
49
  };
@@ -52,6 +66,13 @@
52
66
  input.val ('');
53
67
  }
54
68
  });
69
+ $("#custom").on ('click', function () {
70
+ sendEvent('custom_button_click', {
71
+ a: 1,
72
+ b: "Two",
73
+ c: "III"
74
+ });
75
+ });
55
76
  }
56
77
 
57
78
  $("#connect").on ('click', function () { connect (); } );
@@ -45,7 +45,7 @@ module WebsocketGui
45
45
  end
46
46
 
47
47
  socket.onmessage do |msg|
48
- socket_trigger(:on_socket_recv, msg)
48
+ process_message(msg)
49
49
  end
50
50
 
51
51
  socket.onclose do
@@ -72,8 +72,31 @@ module WebsocketGui
72
72
 
73
73
  private
74
74
 
75
+ def handler_exists?(method)
76
+ @config[method].kind_of? Proc
77
+ end
78
+
75
79
  def socket_trigger(method, *args)
76
- self.instance_exec(*args, &@config[method]) if @config[method].kind_of? Proc
80
+ self.instance_exec(*args, &@config[method]) if handler_exists? method
81
+ end
82
+
83
+ def process_message(msg)
84
+ begin
85
+ data = JSON.parse(msg)
86
+
87
+ if data && data['event'] && data['params']
88
+ event_method = ('on_' + data['event']).to_sym
89
+ if handler_exists? event_method
90
+ socket_trigger(event_method, data['params'])
91
+ return
92
+ end
93
+ end
94
+
95
+ rescue JSON::ParserError
96
+ # fall through to trigger :on_socket_recv
97
+ end
98
+
99
+ socket_trigger(:on_socket_recv, msg)
77
100
  end
78
101
  end
79
102
  end
@@ -1,3 +1,3 @@
1
1
  module WebsocketGui
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/websocket-gui.rb CHANGED
@@ -4,6 +4,7 @@ require "em-websocket"
4
4
  require "sinatra"
5
5
  require "thin"
6
6
  require "launchy"
7
+ require "json"
7
8
 
8
9
  require "websocket-gui/base"
9
10
  require "websocket-gui/sinatra_wrapper"
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.summary = %q{Use the browser as a GUI for local Ruby apps.}
15
15
  spec.homepage = ""
16
16
  spec.license = "MIT"
17
+ spec.homepage = "https://github.com/grossvogel/websocket-gui"
17
18
 
18
19
  spec.files = `git ls-files`.split($/)
19
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -28,4 +29,5 @@ Gem::Specification.new do |spec|
28
29
  spec.add_runtime_dependency 'sinatra', '~> 1.4.3'
29
30
  spec.add_runtime_dependency 'thin', '>= 1.5.1'
30
31
  spec.add_runtime_dependency 'launchy', '~> 2.3.0'
32
+ spec.add_runtime_dependency 'json', '>= 1.7.7'
31
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-gui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Pollentier
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ~>
109
109
  - !ruby/object:Gem::Version
110
110
  version: 2.3.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: json
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.7.7
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.7.7
111
125
  description: |
112
126
  Use Sinatra, Websockets, and EventMachine to create
113
127
  an event-based GUI for Ruby projects that would otherwise be stuck in the console.
@@ -132,7 +146,7 @@ files:
132
146
  - spec/base_spec.rb
133
147
  - spec/spec_helper.rb
134
148
  - websocket-gui.gemspec
135
- homepage: ''
149
+ homepage: https://github.com/grossvogel/websocket-gui
136
150
  licenses:
137
151
  - MIT
138
152
  metadata: {}