celluloid_pubsub 0.0.18 → 0.0.19

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: f20d9bbd812fed2a19482a5d4f3451bc3f2e4260
4
- data.tar.gz: da7fb4d8a9829821b93925d8cb8dc9187ee027a7
3
+ metadata.gz: 269b50c5760b451657e2203852797199925b3d98
4
+ data.tar.gz: aa6318fa2134008fa6750690223d3fa9f7ff07e9
5
5
  SHA512:
6
- metadata.gz: b968c4769ab416975950a50958e5e8de65789207b497a0b36d28946cc9f44c37bcb1d7f38f92fe4c796e070858e79eb7da7c2ffd62a5167bb5c6ea18b9aa2443
7
- data.tar.gz: 5f8ade42f08eb58f5c6d73acf53f723f017d271565f977a1810764f2a102a5e013a9a65c0642d8a756ebe0a506b43ac87a9ba971091d04a6337a556e1929f569
6
+ metadata.gz: 71114ae804ba8d00224a9fe1a831775c85ab9c6ac9a1af050f0dbb360c1e652cabcf6252f0c4eb32481d802b0c39efae22a8adfda43ee1ce7aa24b40efa7b38b
7
+ data.tar.gz: 4eacd46bd37575138ab7a8dff3272c8ebe25728aa9f48ed4bb379c9e0914f48c6b4fe2cbf53b1dbf1b5a9ed287db0e7c876b79593d7d2cdd85f2e3ade6184a26
@@ -114,9 +114,7 @@ module CelluloidPubsub
114
114
  #
115
115
  # @api public
116
116
  def publish(channel, data)
117
- publishing_data = { 'client_action' => 'publish', 'channel' => channel, 'data' => data }
118
- debug(" #{self.class} publishl to #{channel} message: #{publishing_data}") if debug_enabled?
119
- async.chat(publishing_data)
117
+ send_action('publish', channel, data)
120
118
  end
121
119
 
122
120
  # unsubscribes current client from a channel
@@ -127,9 +125,7 @@ module CelluloidPubsub
127
125
  #
128
126
  # @api public
129
127
  def unsubscribe(channel)
130
- publishing_data = { 'client_action' => 'unsubscribe', 'channel' => channel }
131
- debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
132
- async.chat(publishing_data)
128
+ send_action('unsubscribe', channel)
133
129
  end
134
130
 
135
131
  # unsubscribes all clients subscribed to a channel
@@ -140,9 +136,7 @@ module CelluloidPubsub
140
136
  #
141
137
  # @api public
142
138
  def unsubscribe_clients(channel)
143
- publishing_data = { 'client_action' => 'unsubscribe_clients', 'channel' => channel }
144
- debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
145
- async.chat(publishing_data)
139
+ send_action('unsubscribe_clients', channel)
146
140
  end
147
141
 
148
142
  # unsubscribes all clients from all channels
@@ -151,9 +145,7 @@ module CelluloidPubsub
151
145
  #
152
146
  # @api public
153
147
  def unsubscribe_all
154
- publishing_data = { 'client_action' => 'unsubscribe_all' }
155
- debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
156
- async.chat(publishing_data)
148
+ send_action('unsubscribe_all')
157
149
  end
158
150
  # callback executes after connection is opened and delegates action to actor
159
151
  #
@@ -199,6 +191,23 @@ module CelluloidPubsub
199
191
 
200
192
  private
201
193
 
194
+ # method used to send an action to the webserver reactor , to a chanel and with data
195
+ #
196
+ # @param [String] action
197
+ # @param [String] channel
198
+ # @param [Hash] data
199
+ #
200
+ # @return [void]
201
+ #
202
+ # @api private
203
+ def send_action(action, channel = nil, data = {})
204
+ publishing_data = { 'client_action' => action }
205
+ publishing_data = publishing_data.merge('channel' => channel) if channel.present?
206
+ publishing_data = publishing_data.merge('data' => data) if data.present?
207
+ debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
208
+ async.chat(publishing_data)
209
+ end
210
+
202
211
  # method used to send messages to the webserver
203
212
  # checks too see if the message is a hash and if it is it will transform it to JSON and send it to the webser
204
213
  # otherwise will construct a JSON object that will have the key action with the value 'message" and the key message witth the parameter's value
@@ -62,8 +62,8 @@ module CelluloidPubsub
62
62
  message = nil
63
63
  begin
64
64
  message = @websocket.read
65
- rescue => e
66
- debug(e) if @server.debug_enabled?
65
+ rescue => e
66
+ debug(e) if @server.debug_enabled?
67
67
  end
68
68
  message
69
69
  end
@@ -208,10 +208,7 @@ module CelluloidPubsub
208
208
  # @api public
209
209
  def unsubscribe_clients(channel)
210
210
  return if channel.blank? || @server.subscribers[channel].blank?
211
- @server.subscribers[channel].each do |hash|
212
- hash[:reactor].websocket.close
213
- Celluloid::Actor.kill(hash[:reactor])
214
- end
211
+ unsubscribe_from_channel(channel)
215
212
  @server.subscribers[channel] = []
216
213
  end
217
214
 
@@ -266,15 +263,25 @@ module CelluloidPubsub
266
263
  # @api public
267
264
  def unsubscribe_all
268
265
  CelluloidPubsub::Registry.channels.map do |channel|
269
- @server.subscribers[channel].each do |hash|
270
- hash[:reactor].websocket.close
271
- Celluloid::Actor.kill(hash[:reactor])
272
- end
266
+ unsubscribe_from_channel(channel)
273
267
  @server.subscribers[channel] = []
274
268
  end
275
269
 
276
270
  info 'clearing connections' if @server.debug_enabled?
277
271
  shutdown
278
272
  end
273
+
274
+ # unsubscribes all actors from the specified chanel
275
+ #
276
+ # @param [String] channel
277
+ # @return [void]
278
+ #
279
+ # @api public
280
+ def unsubscribe_from_channel(channel)
281
+ @server.subscribers[channel].each do |hash|
282
+ hash[:reactor].websocket.close
283
+ Celluloid::Actor.kill(hash[:reactor])
284
+ end
285
+ end
279
286
  end
280
287
  end
@@ -17,7 +17,7 @@ module CelluloidPubsub
17
17
  # minor release version
18
18
  MINOR = 0
19
19
  # tiny release version
20
- TINY = 18
20
+ TINY = 19
21
21
  # prelease version ( set this only if it is a prelease)
22
22
  PRE = nil
23
23
 
@@ -16,6 +16,8 @@
16
16
  subject.stubs(:inspect).returns(subject)
17
17
  subject.stubs(:run)
18
18
  subject.work(websocket, server)
19
+ subject.stubs(:unsubscribe_from_channel).returns(true)
20
+ Celluloid::Actor.stubs(:kill).returns(true)
19
21
  end
20
22
 
21
23
  describe '#work' do
@@ -204,11 +206,8 @@
204
206
  let(:message) { { a: 'b' } }
205
207
 
206
208
  it 'adds subscribed' do
207
- Celluloid::Actor.stubs(:kill).returns(true)
208
209
  CelluloidPubsub::Registry.stubs(:channels).returns([channel])
209
- server.subscribers.stubs(:[]).with(channel).returns([{ reactor: subject, message: message }])
210
- subject.websocket.expects(:close)
211
- subject.expects(:shutdown)
210
+ subject.expects(:unsubscribe_from_channel).with(channel)
212
211
  subject.unsubscribe_all
213
212
  end
214
213
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: celluloid_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - bogdanRada