wunderbar 0.14.2 → 0.14.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -78,7 +78,33 @@ module Wunderbar
78
78
  end
79
79
  end
80
80
 
81
- class XmlMarkup
81
+ class BuilderBase
82
+ def set_variables_from_params(locals={})
83
+ @_scope.params.merge(locals).each do |key,value|
84
+ value = value.first if Array === value
85
+ value.gsub! "\r\n", "\n" if String === value
86
+ if key =~ /^[a-z]\w+$/
87
+ instance_variable_set "@#{key.dup.untaint}", value
88
+ end
89
+ end
90
+ end
91
+
92
+ def get_binding
93
+ binding
94
+ end
95
+ end
96
+
97
+ class BuilderClass < BuilderBase
98
+ def websocket(*args, &block)
99
+ if Hash === args.last
100
+ args.last[:locals] = Hash[instance_variables.
101
+ map { |name| [name.sub('@',''), instance_variable_get(name)] } ]
102
+ end
103
+ Wunderbar.websocket(*args, &block)
104
+ end
105
+ end
106
+
107
+ class XmlMarkup < BuilderClass
82
108
  def initialize(args)
83
109
  @_scope = args.delete(:scope)
84
110
  @_builder = SpacedMarkup.new(args)
@@ -222,24 +248,8 @@ module Wunderbar
222
248
  end
223
249
  end
224
250
 
225
- class BuilderBase
226
- def set_variables_from_params(locals={})
227
- @_scope.params.merge(locals).each do |key,value|
228
- value = value.first if Array === value
229
- value.gsub! "\r\n", "\n" if String === value
230
- if key =~ /^[a-z]\w+$/
231
- instance_variable_set "@#{key.dup.untaint}", value
232
- end
233
- end
234
- end
235
-
236
- def get_binding
237
- binding
238
- end
239
- end
240
-
241
251
  require 'stringio'
242
- class TextBuilder < BuilderBase
252
+ class TextBuilder < BuilderClass
243
253
  def initialize(scope)
244
254
  @_target = StringIO.new
245
255
  @_scope = scope
@@ -290,7 +300,7 @@ module Wunderbar
290
300
  end
291
301
  end
292
302
 
293
- class JsonBuilder < BuilderBase
303
+ class JsonBuilder < BuilderClass
294
304
  def initialize(scope)
295
305
  @_scope = scope
296
306
  @_target = {}
@@ -2,7 +2,7 @@ module Wunderbar
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 14
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -9,16 +9,18 @@ rescue LoadError
9
9
  end
10
10
 
11
11
  module Wunderbar
12
- class Channel
12
+ class Channel < BuilderBase
13
13
  attr_reader :port, :connected, :complete
14
14
 
15
- def initialize(port, limit)
15
+ def initialize(port, limit, locals=nil)
16
16
  # verify that the port is available
17
17
  TCPServer.new('0.0.0.0', port).close
18
18
 
19
19
  super()
20
20
  @port = port
21
21
  @connected = @complete = false
22
+ @onopen = @onmessage = @onerror = @onclose = Proc.new {}
23
+ @_scope = Struct.new(:params).new({})
22
24
  @channel1 = EM::Channel.new
23
25
  @channel2 = EM::Channel.new
24
26
  @memory = []
@@ -26,6 +28,13 @@ module Wunderbar
26
28
  @memory << msg.chomp unless Symbol === msg
27
29
  @memory.shift while @connected and limit and @memory.length > limit
28
30
  end
31
+
32
+ if locals
33
+ @_scope = locals['_scope'] || @_scope
34
+ set_variables_from_params(locals)
35
+ _ :type => 'stdout', :line => locals['_scope'].methods.inspect
36
+ end
37
+
29
38
  websocket.run
30
39
  end
31
40
 
@@ -38,6 +47,7 @@ module Wunderbar
38
47
  connection = EventMachine::WebSocket::Connection
39
48
  EM.start_server('0.0.0.0', @port, connection, {}) do |ws|
40
49
  ws.onopen do
50
+ @onopen.call(ws)
41
51
  @memory.each {|msg| ws.send msg }
42
52
  @connected = true
43
53
  ws.close_websocket if complete
@@ -51,9 +61,11 @@ module Wunderbar
51
61
  end
52
62
  end
53
63
 
54
- ws.onmessage {|msg| @channel2.push msg}
64
+ ws.onmessage {|msg| @onmessage.call(msg); @channel2.push msg}
65
+
66
+ ws.onerror {|e| @onerror.call(e)}
55
67
 
56
- ws.onclose {@channel1.unsubscribe sid}
68
+ ws.onclose {@onclose.call(ws); @channel1.unsubscribe sid}
57
69
  end
58
70
  EM.add_timer(0.1) {ready = true}
59
71
  end
@@ -86,6 +98,30 @@ module Wunderbar
86
98
  @channel2.pop(*args)
87
99
  end
88
100
 
101
+ def onopen(&block)
102
+ @onopen = block
103
+ end
104
+
105
+ def onmessage(&block)
106
+ @onmessage = block
107
+ end
108
+
109
+ def onerror(&block)
110
+ @onerror = block
111
+ end
112
+
113
+ def onclose(&block)
114
+ @onclose = block
115
+ end
116
+
117
+ def method_missing(method, *args, &block)
118
+ if @_scope and @_scope.respond_to? :method
119
+ @_scope.__send__ method, *args, &block
120
+ else
121
+ super
122
+ end
123
+ end
124
+
89
125
  def _(*args, &block)
90
126
  if block or args.length > 1
91
127
  begin
@@ -151,7 +187,7 @@ module Wunderbar
151
187
 
152
188
  proc = Proc.new do
153
189
  begin
154
- channel = Wunderbar::Channel.new(port, buffer)
190
+ channel = Wunderbar::Channel.new(port, buffer, opts[:locals])
155
191
  if sock1
156
192
  sock1.send('x',0)
157
193
  sock1.close
@@ -167,7 +203,7 @@ module Wunderbar
167
203
  if channel
168
204
  channel.complete = true
169
205
  sleep 5
170
- sleep 60 unless channel.connected
206
+ sleep 60 unless channel.connected or opts[:sync]
171
207
  channel.close
172
208
  end
173
209
  end
data/wunderbar.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "wunderbar"
5
- s.version = "0.14.2"
5
+ s.version = "0.14.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Sam Ruby"]
9
- s.date = "2012-04-30"
9
+ s.date = "2012-05-01"
10
10
  s.description = " Wunderbar makes it easy to produce valid HTML5, wellformed XHTML, Unicode\n (utf-8), consistently indented, readable applications. This includes\n output that conforms to the Polyglot specification and the emerging\n results from the XML Error Recovery Community Group.\n"
11
11
  s.email = "rubys@intertwingly.net"
12
12
  s.files = ["wunderbar.gemspec", "README.md", "COPYING", "lib/wunderbar.rb", "lib/wunderbar", "lib/wunderbar/installation.rb", "lib/wunderbar/html-methods.rb", "lib/wunderbar/job-control.rb", "lib/wunderbar/server.rb", "lib/wunderbar/logger.rb", "lib/wunderbar/rack.rb", "lib/wunderbar/builder.rb", "lib/wunderbar/websocket.rb", "lib/wunderbar/sinatra.rb", "lib/wunderbar/environment.rb", "lib/wunderbar/rails.rb", "lib/wunderbar/cgi-methods.rb", "lib/wunderbar/cssproxy.rb", "lib/wunderbar/version.rb"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wunderbar
3
3
  version: !ruby/object:Gem::Version
4
- hash: 35
4
+ hash: 33
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 14
9
- - 2
10
- version: 0.14.2
9
+ - 3
10
+ version: 0.14.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Ruby
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-30 00:00:00 Z
18
+ date: 2012-05-01 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: builder