wamp_client 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +34 -32
  3. data/lib/wamp/client/auth.rb +0 -27
  4. data/lib/wamp/client/check.rb +0 -27
  5. data/lib/wamp/client/connection.rb +40 -113
  6. data/lib/wamp/client/event.rb +78 -0
  7. data/lib/wamp/client/manager/base.rb +39 -0
  8. data/lib/wamp/client/manager/base_multiple.rb +37 -0
  9. data/lib/wamp/client/manager/establish.rb +168 -0
  10. data/lib/wamp/client/manager/registration.rb +183 -0
  11. data/lib/wamp/client/manager/require.rb +3 -0
  12. data/lib/wamp/client/manager/subscription.rb +55 -0
  13. data/lib/wamp/client/request/base.rb +125 -0
  14. data/lib/wamp/client/request/call.rb +111 -0
  15. data/lib/wamp/client/request/publish.rb +72 -0
  16. data/lib/wamp/client/request/register.rb +79 -0
  17. data/lib/wamp/client/request/require.rb +6 -0
  18. data/lib/wamp/client/request/subscribe.rb +78 -0
  19. data/lib/wamp/client/request/unregister.rb +71 -0
  20. data/lib/wamp/client/request/unsubscribe.rb +72 -0
  21. data/lib/wamp/client/response.rb +136 -0
  22. data/lib/wamp/client/serializer.rb +0 -29
  23. data/lib/wamp/client/session.rb +172 -839
  24. data/lib/wamp/client/transport/base.rb +4 -77
  25. data/lib/wamp/client/transport/event_machine_base.rb +0 -27
  26. data/lib/wamp/client/transport/faye_web_socket.rb +4 -31
  27. data/lib/wamp/client/transport/web_socket_event_machine.rb +3 -30
  28. data/lib/wamp/client/version.rb +1 -28
  29. data/lib/wamp/client.rb +1 -28
  30. data/spec/spec_helper.rb +3 -137
  31. data/spec/support/faye_web_socket_client_stub.rb +43 -0
  32. data/spec/support/test_transport.rb +50 -0
  33. data/spec/support/web_socket_event_machine_client_stub.rb +39 -0
  34. data/spec/wamp/client/connection_spec.rb +4 -4
  35. data/spec/wamp/client/session_spec.rb +135 -135
  36. data/spec/wamp/client/transport_spec.rb +2 -2
  37. data/wamp_client.gemspec +10 -9
  38. metadata +59 -38
  39. data/lib/wamp/client/defer.rb +0 -70
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35fec0c02c367a33b50ea31953fbd8dc6c27e59e
4
- data.tar.gz: c8d09d7b4b8982268f4f761b8b308d43aecca67b
3
+ metadata.gz: 581eccd047b7af79793d5fa979acf389b3f9b6b3
4
+ data.tar.gz: c08465e7c7ff8c4d631e8213af9f4bc940b35731
5
5
  SHA512:
6
- metadata.gz: b7a400d331ea4fcf219543f6652be2c571a8aa11ec2dcd460283b5b4b2a8ff4b577a08d9968260a17e9be38d634f61f92de8dc4c4a2ad5379af6557d0245b5e7
7
- data.tar.gz: 040ced48fc89779307a76a277bb491d993955b6b04d18054229806b05a8fecc2a5cc09032b75cbb8dd0c5b6c21d0967b56850d785add51d013db7e4a0ab10d6b
6
+ metadata.gz: c24b0896d73a539ede06d8e369f9003bc3e3c5e3fe4b22d3e4726509aaea7ec7bff767718e6ea504fd07af0bc7b3e844448831feeb19480f1797dea6dd073840
7
+ data.tar.gz: fea059c5cd3b4221b57dd0e3f90a7501c511cbf67168acfd9d3e467a8dd314c5604bfea33ea26fd211eb760ca3c61d65c65913160f7f69a71ab48ec6c398747b
data/README.md CHANGED
@@ -6,10 +6,21 @@
6
6
 
7
7
  Client for talking to a WAMP Router. This is defined [here](https://tools.ietf.org/html/draft-oberstet-hybi-tavendo-wamp-02)
8
8
 
9
- Please use [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails) to integrate this GEM in to RAILS.
9
+ Note: [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails) has been deprecated in favor of
10
+ [wamp-worker](https://github.com/ericchapman/ruby_wamp_worker) which allows this library to be run
11
+ in a RAILS worker. It will also allow integration with Sidekiq to support allowing handlers to
12
+ execute in the background.
10
13
 
11
14
  ## Revision History
12
15
 
16
+ - v0.2.0:
17
+ - Breaking changes to the API including
18
+ - Moving CallResult, CallError, CallDefer, and ProgressiveCallDefer to common module called "Response"
19
+ - Results to calls return a Hash object instead of a CallResult object
20
+ - Catches "StandardError" instead of "Exception"
21
+ - Session object has been split into *request* and *manager* objects
22
+ - Callback subscriptions are now all "on(event)"
23
+ - Lots of code cleanup and combining handling of methods
13
24
  - v0.1.4:
14
25
  - Wrapped defer logic inside of yield method for cleanliness
15
26
  - v0.1.3:
@@ -79,11 +90,7 @@ A connection can be created as follows
79
90
  ```ruby
80
91
  require 'wamp/client'
81
92
 
82
- options = {
83
- uri: 'ws://127.0.0.1:8080/ws',
84
- realm: 'realm1',
85
- }
86
- connection = Wamp::Client::Connection.new(options)
93
+ connection = Wamp::Client::Connection.new(uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1')
87
94
 
88
95
  connection.on(:join) do |session, details|
89
96
  puts "Session Open"
@@ -97,7 +104,7 @@ connection.on(:join) do |session, details|
97
104
  # Call It
98
105
  session.call('com.example.procedure', [3,4]) do |result, error, details|
99
106
  if result
100
- puts result.args[0] # => 7
107
+ puts result[:args][0] # => 7
101
108
  end
102
109
  end
103
110
 
@@ -163,12 +170,7 @@ method 'add_tick_loop'
163
170
  ```ruby
164
171
  require 'wamp/client'
165
172
 
166
- options = {
167
- uri: 'ws://127.0.0.1:8080/ws',
168
- realm: 'realm1',
169
- }
170
-
171
- connection = Wamp::Client::Connection.new(options)
173
+ connection = Wamp::Client::Connection.new(uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1')
172
174
 
173
175
  connection.transport_class.add_tick_loop do
174
176
  # Do something periodic
@@ -516,8 +518,8 @@ To call, do the following
516
518
  ```ruby
517
519
  session.call('com.example.procedure', [15], {param: value}, {}) do |result, error, details|
518
520
  # TODO: Do something
519
- args = result.args
520
- kwargs = result.kwargs
521
+ args = result[:args]
522
+ kwargs = result[:kwargs]
521
523
  end
522
524
  ```
523
525
 
@@ -532,11 +534,11 @@ Errors can either be raised OR returned as shown below
532
534
 
533
535
  ```ruby
534
536
  handler = lambda do |args, kwargs, details|
535
- raise 'error'
537
+ raise RuntimeError,'error'
536
538
  # OR
537
- raise Wamp::Client::CallError.new('wamp.error', ['some error'], {details: true})
539
+ raise Wamp::Client::Response::CallError.new('wamp.error', ['some error'], {details: true})
538
540
  # OR
539
- Wamp::Client::CallError.new('wamp.error', ['some error'], {details: true})
541
+ Wamp::Client::Response::CallError.new('wamp.error', ['some error'], {details: true})
540
542
  end
541
543
  session.register('com.example.procedure', handler)
542
544
  ```
@@ -549,7 +551,7 @@ caller. This is shown below
549
551
 
550
552
  ```ruby
551
553
  def add(args, kwargs, details)
552
- defer = Wamp::Client::Defer::CallDefer.new
554
+ defer = Wamp::Client::Response::CallDefer.new
553
555
  EM.add_timer(2) { # Something Async
554
556
  defer.succeed(args[0]+args[1])
555
557
  }
@@ -562,9 +564,9 @@ Errors are returned as follows
562
564
 
563
565
  ```ruby
564
566
  def add(args, kwargs, details)
565
- defer = Wamp::Client::Defer::CallDefer.new
567
+ defer = Wamp::Client::Response::CallDefer.new
566
568
  EM.add_timer(2) { # Something Async
567
- defer.fail(Wamp::Client::CallError.new('test.error'))
569
+ defer.fail(Wamp::Client::Response::CallError.new('test.error'))
568
570
  }
569
571
  defer
570
572
  end
@@ -579,7 +581,7 @@ Progressive calls are ones that return the result in pieces rather than all at o
579
581
  ```ruby
580
582
  results = []
581
583
  session.call('com.example.procedure', [], {}, {receive_progress: true}) do |result, error, details|
582
- results = results + result.args
584
+ results = results + result[:args]
583
585
  unless details[:progress]
584
586
  puts results # => [1,2,3,4,5,6]
585
587
  end
@@ -590,15 +592,15 @@ end
590
592
 
591
593
  ```ruby
592
594
  def add(args, kwargs, details)
593
- defer = Wamp::Client::Defer::ProgressiveCallDefer.new
595
+ defer = Wamp::Client::Response::ProgressiveCallDefer.new
594
596
  EM.add_timer(2) { # Something Async
595
- defer.progress(Wamp::Client::CallResult.new([1,2,3]))
597
+ defer.progress(Wamp::Client::Response::CallResult.new([1,2,3]))
596
598
  }
597
599
  EM.add_timer(4) { # Something Async
598
- defer.progress(Wamp::Client::CallResult.new([4,5,6]))
600
+ defer.progress(Wamp::Client::Response::CallResult.new([4,5,6]))
599
601
  }
600
602
  EM.add_timer(6) { # Something Async
601
- defer.succeed(Wamp::Client::CallResult.new)
603
+ defer.succeed(Wamp::Client::Response::CallResult.new)
602
604
  }
603
605
  defer
604
606
  end
@@ -613,8 +615,8 @@ A cancelled call will tell a callee who implements a progressive call to cancel
613
615
  ```ruby
614
616
  call = session.call('com.example.procedure', [15], {param: value}, {}) do |result, error, details|
615
617
  # TODO: Do something
616
- args = result.args
617
- kwargs = result.kwargs
618
+ args = result[:args]
619
+ kwargs = result[:kwargs]
618
620
  end
619
621
 
620
622
  # At some later time...
@@ -641,20 +643,20 @@ def interrupt_handler(request, mode)
641
643
  end
642
644
 
643
645
  def add(args, kwargs, details)
644
- defer = Wamp::Client::Defer::ProgressiveCallDefer.new
646
+ defer = Wamp::Client::Response::ProgressiveCallDefer.new
645
647
  EM.add_timer(2) { # Something Async
646
648
  if @interrupts[defer.request].nil?
647
- defer.progress(Wamp::Client::CallResult.new([1,2,3]))
649
+ defer.progress(Wamp::Client::Response::CallResult.new([1,2,3]))
648
650
  end
649
651
  }
650
652
  EM.add_timer(4) { # Something Async
651
653
  if @interrupts[defer.request].nil?
652
- defer.progress(Wamp::Client::CallResult.new([4,5,6]))
654
+ defer.progress(Wamp::Client::Response::CallResult.new([4,5,6]))
653
655
  end
654
656
  }
655
657
  EM.add_timer(6) { # Something Async
656
658
  if @interrupts[defer.request].nil?
657
- defer.succeed(Wamp::Client::CallResult.new)
659
+ defer.succeed(Wamp::Client::Response::CallResult.new)
658
660
  end
659
661
  @interrupts.delete(request)
660
662
  }
@@ -1,30 +1,3 @@
1
- =begin
2
-
3
- Copyright (c) 2018 Eric Chapman
4
-
5
- MIT License
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining
8
- a copy of this software and associated documentation files (the
9
- "Software"), to deal in the Software without restriction, including
10
- without limitation the rights to use, copy, modify, merge, publish,
11
- distribute, sublicense, and/or sell copies of the Software, and to
12
- permit persons to whom the Software is furnished to do so, subject to
13
- the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be
16
- included in all copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
-
26
- =end
27
-
28
1
  require 'openssl'
29
2
  require 'base64'
30
3
 
@@ -1,30 +1,3 @@
1
- =begin
2
-
3
- Copyright (c) 2018 Eric Chapman
4
-
5
- MIT License
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining
8
- a copy of this software and associated documentation files (the
9
- "Software"), to deal in the Software without restriction, including
10
- without limitation the rights to use, copy, modify, merge, publish,
11
- distribute, sublicense, and/or sell copies of the Software, and to
12
- permit persons to whom the Software is furnished to do so, subject to
13
- the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be
16
- included in all copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
-
26
- =end
27
-
28
1
  module Wamp
29
2
  module Client
30
3
  module Check
@@ -1,100 +1,16 @@
1
- =begin
2
-
3
- Copyright (c) 2018 Eric Chapman
4
-
5
- MIT License
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining
8
- a copy of this software and associated documentation files (the
9
- "Software"), to deal in the Software without restriction, including
10
- without limitation the rights to use, copy, modify, merge, publish,
11
- distribute, sublicense, and/or sell copies of the Software, and to
12
- permit persons to whom the Software is furnished to do so, subject to
13
- the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be
16
- included in all copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
-
26
- =end
27
-
28
1
  require 'wamp/client/session'
2
+ require 'wamp/client/event'
29
3
  require 'wamp/client/transport/web_socket_event_machine'
30
4
  require 'wamp/client/transport/faye_web_socket'
31
5
 
32
6
  module Wamp
33
7
  module Client
34
8
  class Connection
35
- attr_accessor :options, :transport_class, :transport, :session
36
-
37
- @reconnect = true
38
-
39
- @open = false
40
- def is_open?
41
- @open
42
- end
43
-
44
- # Called when the connection is established
45
- @on_connect
46
- def on_connect(&on_connect)
47
- @on_connect = on_connect
48
- end
49
-
50
- # Called when the WAMP session is established
51
- # @param session [WampClient::Session]
52
- # @param details [Hash]
53
- @on_join
54
- def on_join(&on_join)
55
- @on_join = on_join
56
- end
57
-
58
- # Called when the WAMP session presents a challenge
59
- # @param authmethod [String]
60
- # @param extra [Hash]
61
- @on_challenge
62
- def on_challenge(&on_challenge)
63
- @on_challenge = on_challenge
64
- end
9
+ include Event
65
10
 
66
- # Called when the WAMP session is terminated
67
- # @param reason [String] The reason the session left the router
68
- # @param details [Hash] Object containing information about the left session
69
- @on_leave
70
- def on_leave(&on_leave)
71
- @on_leave = on_leave
72
- end
11
+ attr_accessor :options, :transport_class, :transport, :session
73
12
 
74
- # Called when the connection is terminated
75
- # @param reason [String] The reason the transport was disconnected
76
- @on_disconnect
77
- def on_disconnect(&on_disconnect)
78
- @on_disconnect = on_disconnect
79
- end
80
-
81
- # Simple setter for callbacks
82
- def on(event, &callback)
83
- case event
84
- when :connect
85
- self.on_connect(&callback)
86
- when :join
87
- self.on_join(&callback)
88
- when :challenge
89
- self.on_challenge(&callback)
90
- when :leave
91
- self.on_leave(&callback)
92
- when :disconnect
93
- self.on_disconnect(&callback)
94
- else
95
- raise RuntimeError, "Unknown on(event) '#{event}'"
96
- end
97
- end
13
+ create_event [:connect, :join, :challenge, :leave, :disconnect]
98
14
 
99
15
  # @param options [Hash] The different options to pass to the connection
100
16
  # @option options [String] :uri The uri of the WAMP router to connect to
@@ -108,12 +24,17 @@ module Wamp
108
24
  def initialize(options)
109
25
  self.transport_class = options.delete(:transport) || Wamp::Client::Transport::WebSocketEventMachine
110
26
  self.options = options || {}
27
+
28
+ @reconnect = true
29
+ @open = false
30
+
31
+ logger.info("#{self.class.name} using version #{Wamp::Client::VERSION}")
111
32
  end
112
33
 
113
34
  # Opens the connection
114
35
  def open
115
36
 
116
- raise RuntimeError, 'The connection is already open' if self.is_open?
37
+ raise RuntimeError, 'connection is already open' if self.is_open?
117
38
 
118
39
  @reconnect = true
119
40
  @retry_timer = 1
@@ -121,7 +42,7 @@ module Wamp
121
42
 
122
43
  self.transport_class.start_event_machine do
123
44
  # Create the transport
124
- self._create_transport
45
+ create_transport
125
46
  end
126
47
 
127
48
  end
@@ -129,7 +50,7 @@ module Wamp
129
50
  # Closes the connection
130
51
  def close
131
52
 
132
- raise RuntimeError, 'The connection is already closed' unless self.is_open?
53
+ raise RuntimeError, 'connection is already closed' unless self.is_open?
133
54
 
134
55
  # Leave the session
135
56
  @reconnect = false
@@ -138,29 +59,37 @@ module Wamp
138
59
 
139
60
  end
140
61
 
141
- def _create_session
62
+ # Returns true if the connection is open
63
+ #
64
+ def is_open?
65
+ @open
66
+ end
67
+
68
+ private
69
+
70
+ def create_session
142
71
  self.session = Wamp::Client::Session.new(self.transport, self.options)
143
72
 
144
73
  # Setup session callbacks
145
74
  self.session.on(:challenge) do |authmethod, extra|
146
- self._finish_retry
147
- @on_challenge.call(authmethod, extra) if @on_challenge
75
+ finish_retry
76
+ trigger :challenge, authmethod, extra
148
77
  end
149
78
 
150
79
  self.session.on(:join) do |details|
151
- self._finish_retry
152
- @on_join.call(self.session, details) if @on_join
80
+ finish_retry
81
+ trigger :join, self.session, details
153
82
  end
154
83
 
155
84
  self.session.on(:leave) do |reason, details|
156
85
 
157
86
  unless @retrying
158
- @on_leave.call(reason, details) if @on_leave
87
+ trigger :leave, reason, details
159
88
  end
160
89
 
161
90
  if @reconnect
162
91
  # Retry
163
- self._retry unless @retrying
92
+ retry_connect unless @retrying
164
93
  else
165
94
  # Close the transport
166
95
  self.transport.disconnect
@@ -170,7 +99,7 @@ module Wamp
170
99
  self.session.join(self.options[:realm])
171
100
  end
172
101
 
173
- def _create_transport
102
+ def create_transport
174
103
 
175
104
  if self.transport
176
105
  self.transport.disconnect
@@ -183,22 +112,22 @@ module Wamp
183
112
  # Setup transport callbacks
184
113
  self.transport.on(:open) do
185
114
 
186
- logger.info("#{self.class.name} TRANSPORT OPEN")
115
+ logger.info("#{self.class.name} transport open")
187
116
 
188
117
  # Call the callback
189
- @on_connect.call if @on_connect
118
+ trigger :connect
190
119
 
191
120
  # Create the session
192
- self._create_session
121
+ create_session
193
122
 
194
123
  end
195
124
 
196
125
  self.transport.on(:close) do |reason|
197
- logger.info("#{self.class.name} TRANSPORT CLOSED: #{reason}")
126
+ logger.info("#{self.class.name} transport closed: #{reason}")
198
127
  @open = false
199
128
 
200
129
  unless @retrying
201
- @on_disconnect.call(reason) if @on_disconnect
130
+ trigger :disconnect, reason
202
131
  end
203
132
 
204
133
  # Nil out the session since the transport closed underneath it
@@ -206,7 +135,7 @@ module Wamp
206
135
 
207
136
  if @reconnect
208
137
  # Retry
209
- self._retry unless @retrying
138
+ retry_connect unless @retrying
210
139
  else
211
140
  # Stop the Event Machine
212
141
  self.transport_class.stop_event_machine
@@ -214,7 +143,7 @@ module Wamp
214
143
  end
215
144
 
216
145
  self.transport.on(:error) do |message|
217
- logger.error("#{self.class.name} TRANSPORT ERROR: #{message}")
146
+ logger.error("#{self.class.name} transport error: #{message}")
218
147
  end
219
148
 
220
149
  @open = true
@@ -223,29 +152,27 @@ module Wamp
223
152
 
224
153
  end
225
154
 
226
- def _finish_retry
155
+ def finish_retry
227
156
  @retry_timer = 1
228
157
  @retrying = false
229
158
  end
230
159
 
231
- def _retry
160
+ def retry_connect
232
161
 
233
162
  if self.session == nil or not self.session.is_open?
234
163
  @retry_timer = 2*@retry_timer unless @retry_timer == 32
235
164
  @retrying = true
236
165
 
237
- self._create_transport
166
+ create_transport
238
167
 
239
- logger.info("#{self.class.name} RECONNECT in #{@retry_timer} seconds")
168
+ logger.info("#{self.class.name} reconnect in #{@retry_timer} seconds")
240
169
  self.transport_class.add_timer(@retry_timer*1000) do
241
- self._retry if @retrying
170
+ retry_connect if @retrying
242
171
  end
243
172
  end
244
173
 
245
174
  end
246
175
 
247
- private
248
-
249
176
  # Returns the logger
250
177
  #
251
178
  def logger
@@ -0,0 +1,78 @@
1
+ module Wamp
2
+ module Client
3
+
4
+ # Module that adds event capabilities to the class.
5
+ #
6
+ # Usage:
7
+ #
8
+ # class MyClass
9
+ # include Event
10
+ #
11
+ # create_event [:open, :close]
12
+ #
13
+ # def do_something
14
+ # trigger :open, 4
15
+ # end
16
+ #
17
+ # end
18
+ #
19
+ # object = MyClass.new
20
+ #
21
+ # object.on(:open) do |value|
22
+ # puts value
23
+ # end
24
+ #
25
+ # object.do_something
26
+ #
27
+ # Prints:
28
+ #
29
+ # 4
30
+ #
31
+ module Event
32
+
33
+ def self.included(base)
34
+ base.extend(ClassMethods)
35
+ end
36
+
37
+ module ClassMethods
38
+ def create_event(events, attribute: nil, setter: nil, trigger: nil)
39
+ attribute ||= :event
40
+ setter ||= :on
41
+ trigger ||= :trigger
42
+
43
+ # Create the attributes
44
+ callback_name = "#{attribute}_callbacks"
45
+ event_list_name = "#{attribute}_list"
46
+
47
+ # Creates the attribute to store the callbacks
48
+ attr_accessor callback_name
49
+
50
+ # Creates the attributes to store the allowed events
51
+ define_method event_list_name do
52
+ events
53
+ end
54
+
55
+ # Creates the setter. Default: "on"
56
+ define_method setter do |event, &handler|
57
+ unless self.send(event_list_name).include?(event)
58
+ raise RuntimeError, "unknown #{setter}(event) '#{event}'"
59
+ end
60
+
61
+ callback = self.send(callback_name) || {}
62
+ callback[event] = handler
63
+ self.send("#{callback_name}=", callback)
64
+ end
65
+
66
+ # Create the trigger. Default: "trigger"
67
+ define_method trigger do |event, *args|
68
+ handler = (self.send(callback_name) || {})[event]
69
+ if handler != nil
70
+ handler.call(*args)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,39 @@
1
+ module Wamp
2
+ module Client
3
+ module Manager
4
+
5
+ class Base
6
+ attr_reader :session, :send_message_callback
7
+
8
+ # Constructor
9
+ #
10
+ # @param session [Wamp::Client::Session] - The session
11
+ # @param success [Block] - A block to run when the request was successful
12
+ def initialize(session, send_message)
13
+ @session = session
14
+ @send_message_callback = send_message
15
+ end
16
+
17
+ private
18
+
19
+ # Returns the logger
20
+ #
21
+ def logger
22
+ Wamp::Client.logger
23
+ end
24
+
25
+ # Sends a message
26
+ #
27
+ def send_message(message)
28
+ self.send_message_callback.call(message) if self.send_message_callback
29
+ end
30
+
31
+ # Triggers an event
32
+ def trigger(event, *args)
33
+ self.session.trigger event, *args
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,37 @@
1
+ require_relative "base"
2
+
3
+ module Wamp
4
+ module Client
5
+ module Manager
6
+
7
+ class BaseMultiple < Base
8
+ attr_reader :objects
9
+
10
+ # Constructor
11
+ #
12
+ # @param session [Wamp::Client::Session] - The session
13
+ # @param success [Block] - A block to run when the request was successful
14
+ def initialize(session, send_message)
15
+ super session, send_message
16
+ @objects = {}
17
+ end
18
+
19
+ # Adds an object to the manager
20
+ #
21
+ # @param id [Int] - The ID of the object
22
+ # @param object [Object] - The object to handle
23
+ def add(id, object)
24
+ self.objects[id] = object
25
+ end
26
+
27
+ # Removes an object
28
+ #
29
+ # @param id [Int] - The ID of the object
30
+ def remove(id)
31
+ self.objects.delete(id)
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end