pubnub 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pubnub might be problematic. Click here for more details.

data/README CHANGED
@@ -19,7 +19,7 @@ pubnub = Pubnub.new(
19
19
  "demo", ## PUBLISH_KEY
20
20
  "demo", ## SUBSCRIBE_KEY
21
21
  "demo", ## SECRET_KEY
22
- "demo", ## CIPHER_KEY
22
+ "", ## CIPHER_KEY (Cipher key is Optional)
23
23
  false ## SSL_ON?
24
24
  )
25
25
 
@@ -6,11 +6,11 @@
6
6
  require 'rubygems'
7
7
  require 'pubnub'
8
8
 
9
- ## declaring publish_key, subscribe_key, secret_key, cipher_key and ssl flag
9
+ ## declaring publish_key, subscribe_key, secret_key, cipher_key and ssl flag (Cipher key is Optional)
10
10
  publish_key = 'demo'
11
11
  subscribe_key = 'demo'
12
12
  secret_key = 'demo'
13
- cipher_key = 'demo'
13
+ cipher_key = '' # (Cipher key is Optional)
14
14
  ssl_on = false
15
15
 
16
16
  ## Print usage if missing info.
@@ -31,8 +31,11 @@ pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on)
31
31
 
32
32
  ## Request Past Publishes (HISTORY)
33
33
  puts('Requesting History with history() Function')
34
- message = pubnub.history({
34
+ pubnub.history({
35
35
  'channel' => 'hello_world',
36
- 'limit' => 5
36
+ 'limit' => 5,
37
+ 'callback' => lambda do |message|
38
+ puts(message)
39
+ end
37
40
  })
38
- puts(message)
41
+ #puts(message)
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  ## -----------------------------------
3
2
  ## PubNub Ruby API Publish Example
4
3
  ## -----------------------------------
@@ -7,11 +6,11 @@
7
6
  require 'rubygems'
8
7
  require 'pubnub'
9
8
 
10
- ## declaring publish_key, subscribe_key, secret_key, cipher_key, channel, ssl flag, messages
9
+ ## declaring publish_key, subscribe_key, secret_key, cipher_key, channel, ssl flag, messages (Cipher key is Optional)
11
10
  publish_key = 'demo'
12
11
  subscribe_key = 'demo'
13
12
  secret_key = 'demo'
14
- cipher_key = ''
13
+ cipher_key = '' # (Cipher key is Optional)
15
14
  ssl_on = false
16
15
  channel = 'hello_world'
17
16
 
@@ -40,22 +39,31 @@ pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on)
40
39
  puts("\nSending message in String format with publish() Function")
41
40
  info = pubnub.publish({
42
41
  'channel' => channel,
43
- 'message' => strMessage
42
+ 'message' => strMessage,
43
+ 'callback' => lambda do |message|
44
+ puts(message)
45
+ end
44
46
  })
45
47
  puts(info)
46
48
 
47
49
  ## Send Message (PUBLISH) -- Array
48
50
  puts("\nSending message in Array format with publish() Function")
49
- info = pubnub.publish({
51
+ pubnub.publish({
50
52
  'channel' => channel,
51
- 'message' => arrMessage
53
+ 'message' => arrMessage,
54
+ 'callback' => lambda do |message|
55
+ puts(message)
56
+ end
52
57
  })
53
- puts(info)
58
+ #puts(info)
54
59
 
55
60
  ## Send Message (PUBLISH) -- Object (Dictionary)
56
61
  puts("\nSending message in Dictionary Object format with publish() Function")
57
- info = pubnub.publish({
62
+ pubnub.publish({
58
63
  'channel' => channel,
59
- 'message' => objMessage
64
+ 'message' => objMessage,
65
+ 'callback' => lambda do |message|
66
+ puts(message)
67
+ end
60
68
  })
61
- puts(info)
69
+ #puts(info)
@@ -6,11 +6,11 @@
6
6
  require 'rubygems'
7
7
  require 'pubnub'
8
8
 
9
- ## declaring publish_key, subscribe_key, secret_key, cipher_key, ssl flag
9
+ ## declaring publish_key, subscribe_key, secret_key, cipher_key, ssl flag (Cipher key is Optional)
10
10
  publish_key = 'demo'
11
11
  subscribe_key = 'demo'
12
12
  secret_key = 'demo'
13
- cipher_key = 'demo'
13
+ cipher_key = '' # (Cipher key is Optional)
14
14
  ssl_on = false
15
15
  channel = 'hello_world'
16
16
 
data/lib/pubnub.rb CHANGED
@@ -112,16 +112,17 @@ class Pubnub
112
112
  #*
113
113
  def publish(args)
114
114
  ## Fail if bad input.
115
- if !(args['channel'] && args['message'])
116
- puts('Missing Channel or Message')
115
+ if !(args['channel'] && args['message'] && args['callback'])
116
+ puts('Missing Channel or Message or Callback')
117
117
  return false
118
118
  end
119
119
 
120
120
  ## Capture User Input
121
121
  channel = args['channel']
122
122
  message = args['message']
123
+ callback = args['callback']
123
124
 
124
- #encryption of message
125
+ # Encryption of message
125
126
  if @cipher_key.length > 0
126
127
  pc=PubnubCrypto.new(@cipher_key)
127
128
  if message.is_a? Array
@@ -144,15 +145,10 @@ class Pubnub
144
145
  end
145
146
 
146
147
  ## Send Message
147
- return _request([
148
- 'publish',
149
- @publish_key,
150
- @subscribe_key,
151
- signature,
152
- channel,
153
- '0',
154
- message
155
- ])
148
+ request = [ 'publish', @publish_key, @subscribe_key, signature, channel, '0', message ]
149
+ args['request'] = request
150
+ args['callback'] = callback
151
+ _request(args)
156
152
  end
157
153
 
158
154
  #**
@@ -181,51 +177,12 @@ class Pubnub
181
177
  return false
182
178
  end
183
179
 
184
- ## Begin Subscribe
185
- loop do
186
- begin
187
- timetoken = args['timetoken'] ? args['timetoken'] : 0
188
-
189
- ## Wait for Message
190
- response = _request([
191
- 'subscribe',
192
- @subscribe_key,
193
- channel,
194
- '0',
195
- timetoken.to_s
196
- ])
197
-
198
- messages = response[0]
199
- args['timetoken'] = response[1]
200
-
201
- ## If it was a timeout
202
- next if !messages.length
203
-
204
- ## Run user Callback and Reconnect if user permits.
205
- ## Capture the message and encrypt it
206
- if @cipher_key.length > 0
207
- pc = PubnubCrypto.new(@cipher_key)
208
- messages.each do |message|
209
- if message.is_a? Array
210
- message=pc.decryptArray(message)
211
- else
212
- message=pc.decryptObject(message)
213
- end
214
- if !callback.call(message)
215
- return
216
- end
217
- end
218
- else
219
- messages.each do |message|
220
- if !callback.call(message)
221
- return
222
- end
223
- end
224
- end
225
- rescue Timeout::Error
226
- rescue
227
- sleep(1)
228
- end
180
+ ## EventMachine loop
181
+ EventMachine.run do
182
+ timetoken = 0
183
+ request = [ 'subscribe', @subscribe_key, channel, '0', timetoken.to_s ]
184
+ args['request'] = request
185
+ _subscribe(args)
229
186
  end
230
187
  end
231
188
 
@@ -241,30 +198,22 @@ class Pubnub
241
198
  ## Capture User Input
242
199
  limit = +args['limit'] ? +args['limit'] : 5
243
200
  channel = args['channel']
201
+ callback = args['callback']
244
202
 
245
203
  ## Fail if bad input.
246
204
  if (!channel)
247
205
  puts 'Missing Channel.'
248
206
  return false
249
207
  end
208
+ if (!callback)
209
+ puts 'Missing Callback.'
210
+ return false
211
+ end
250
212
 
251
213
  ## Get History
252
- response = _request([ 'history', @subscribe_key, channel, '0', limit.to_s])
253
- if @cipher_key.length > 0
254
- myarr=Array.new()
255
- response.each do |message|
256
- pc=PubnubCrypto.new(@cipher_key)
257
- if message.is_a? Array
258
- message=pc.decryptArray(message)
259
- else
260
- message=pc.decryptObject(message)
261
- end
262
- myarr.push(message)
263
- end
264
- return myarr
265
- else
266
- return response
267
- end
214
+ request = [ 'history', @subscribe_key, channel, '0', limit.to_s ]
215
+ args['request'] = request
216
+ _request(args)
268
217
  end
269
218
 
270
219
  #**
@@ -274,11 +223,10 @@ class Pubnub
274
223
  #*
275
224
  #* @return int timestamp.
276
225
  #*
277
- def time()
278
- return _request([
279
- 'time',
280
- '0'
281
- ])[0]
226
+ def time(args)
227
+ request = [ 'time', '0' ]
228
+ args['request'] = request
229
+ _request(args)
282
230
  end
283
231
 
284
232
  #**
@@ -295,53 +243,114 @@ class Pubnub
295
243
  private
296
244
 
297
245
  #**
298
- #* Request URL
246
+ #* Request URL for subscribe
299
247
  #*
300
248
  #* @param array request of url directories.
301
249
  #* @return array from JSON response.
302
250
  #*
303
- def _request(request)
304
- ## Construct Request URL
305
- url = '/' + request.map{ |bit| bit.split('').map{ |ch|
306
- ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
307
- '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
308
- }.join('') }.join('/')
251
+ def _subscribe(args)
252
+ channel = args['channel']
253
+ callback = args['callback']
254
+ request = args['request']
309
255
 
256
+ # Construct Request
257
+ url = encode_URL(request);
310
258
  url = @origin + url
311
- http_response = ''
312
-
313
- EventMachine.run do
314
- Fiber.new{
315
- http = async_fetch(url)
316
- http_response = http.response
317
- EventMachine.stop
318
- }.resume
319
- end
320
- if(http_response != '')
321
- JSON.parse(http_response)
322
- end
323
- end
324
259
 
325
- ## Non-blocking IO using EventMachine
326
- def async_fetch(url)
327
- f = Fiber.current
260
+ # Execute Request
261
+ http = EventMachine::HttpRequest.new(url, :connect_timeout => 310, :inactivity_timeout => 0).get
262
+ http.callback {
263
+ http_response = JSON.parse(http.response)
264
+ messages = http_response[0]
265
+ timetoken = http_response[1]
266
+
267
+ next if !messages.length
268
+
269
+ ## Run user Callback and Reconnect if user permits.
270
+ ## Capture the message and encrypt it
271
+ if @cipher_key.length > 0
272
+ pc = PubnubCrypto.new(@cipher_key)
273
+ messages.each do |message|
274
+ if message.is_a? Array
275
+ message=pc.decryptArray(message)
276
+ else
277
+ message=pc.decryptObject(message)
278
+ end
279
+ if !callback.call(message)
280
+ return
281
+ end
282
+ end
283
+ else
284
+ messages.each do |message|
285
+ if !callback.call(message)
286
+ return
287
+ end
288
+ end
289
+ end
328
290
 
329
- request_options = {
330
- :timeout => 310, # set request timeout
331
- :query => {'V' => '3.1', 'User-Agent' => 'Ruby', 'Accept-Encoding' => 'gzip'}, # set request headers
291
+ request = [ 'subscribe', @subscribe_key, channel, '0', timetoken.to_s ]
292
+ args['request'] = request
293
+ # Recusive call to _subscribe
294
+ _subscribe(args)
332
295
  }
296
+ http.errback {
297
+ http.error
298
+ # TODO: check for time function and reconnect
299
+ }
300
+ end
333
301
 
334
- http = EventMachine::HttpRequest.new(url).get request_options
335
- http.callback { f.resume(http) }
336
- http.errback { f.resume(http) }
337
-
338
- Fiber.yield
339
-
340
- if http.error
341
- p [:HTTP_ERROR, http.error]
342
- end
343
-
344
- http
302
+ #**
303
+ #* Request URL
304
+ #*
305
+ #* @param array request of url directories.
306
+ #* @return array from JSON response.
307
+ #*
308
+ def _request(args)
309
+ request = args['request']
310
+ callback = args['callback']
311
+ url = encode_URL(request)
312
+ url = @origin + url
313
+ EventMachine.run {
314
+ # Execute Request
315
+ http = EventMachine::HttpRequest.new(url, :connect_timeout => 310, :inactivity_timeout => 0).get
316
+ http.callback {
317
+ if request[0] == 'history'
318
+ response = JSON.parse(http.response)
319
+ if @cipher_key.length > 0
320
+ myarr=Array.new()
321
+ response.each do |message|
322
+ pc=PubnubCrypto.new(@cipher_key)
323
+ if message.is_a? Array
324
+ message=pc.decryptArray(message)
325
+ else
326
+ message=pc.decryptObject(message)
327
+ end
328
+ myarr.push(message)
329
+ end
330
+ callback.call(myarr)
331
+ else
332
+ callback.call(reponse)
333
+ end
334
+ elsif request[0] == 'publish'
335
+ callback.call(JSON.parse(http.response))
336
+ else
337
+ callback.call(JSON.parse(http.response))
338
+ end
339
+ EventMachine.stop
340
+ }
341
+ http.errback {
342
+ puts http.error
343
+ EventMachine.stop
344
+ }
345
+ }
345
346
  end
346
347
 
348
+ def encode_URL(request)
349
+ ## Construct Request URL
350
+ url = '/' + request.map{ |bit| bit.split('').map{ |ch|
351
+ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
352
+ '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
353
+ }.join('') }.join('/')
354
+ return url
355
+ end
347
356
  end
data/tests/unit_test.rb CHANGED
@@ -13,11 +13,11 @@
13
13
  require 'rubygems'
14
14
  require 'pubnub'
15
15
 
16
- ## declaring publish_key, subscribe_key, secret_key, cipher_key, message, ssl_on
16
+ ## declaring publish_key, subscribe_key, secret_key, cipher_key, message, ssl_on (Cipher key is Optional)
17
17
  publish_key = ARGV[0] || 'demo'
18
18
  subscribe_key = ARGV[1] || 'demo'
19
19
  secret_key = ARGV[2] || 'demo'
20
- cipher_key = ARGV[3] || 'demo'
20
+ cipher_key = ARGV[3] || '' # (Cipher key is Optional)
21
21
  ssl_on = false
22
22
  channel = 'hello_world'
23
23
  message = 'Hi. (顶顅Ȓ)'
@@ -38,33 +38,41 @@ end
38
38
  ## ---------------------------------------------------------------------------
39
39
  ## UUID generation
40
40
  ## ---------------------------------------------------------------------------
41
- uuid=pubnub.UUID()
41
+ uuid = pubnub.UUID()
42
42
  test( uuid.length > 0, 'PubNub Client API UUID: ' + uuid )
43
43
 
44
44
  ## ---------------------------------------------------------------------------
45
45
  ## PubNub Server Time
46
46
  ## ---------------------------------------------------------------------------
47
- timestamp = pubnub.time()
48
- test( timestamp > 0, 'PubNub Server Time: ' + timestamp.to_s )
47
+ pubnub.time({'callback' => lambda do |message|
48
+ print 'PubNub Server Time: ', message ## print message
49
+ end
50
+ })
49
51
 
50
52
  ## ---------------------------------------------------------------------------
51
53
  ## PUBLISH
52
54
  ## ---------------------------------------------------------------------------
53
- pubish_success = pubnub.publish({
55
+ pubnub.publish({
54
56
  'channel' => channel,
55
- 'message' => message
57
+ 'message' => message,
58
+ 'callback' => lambda do |message|
59
+ puts(message)
60
+ end
56
61
  })
57
- test( pubish_success[0] == 1, 'Publish First Message Success' )
62
+ #test( pubish_success[0] == 1, 'Publish First Message Success' )
58
63
 
59
64
  ## ---------------------------------------------------------------------------
60
65
  ## HISTORY
61
66
  ## ---------------------------------------------------------------------------
62
- history = pubnub.history({
67
+ pubnub.history({
63
68
  'channel' => channel,
64
- 'limit' => 5
69
+ 'limit' => 5,
70
+ 'callback' => lambda do |message|
71
+ puts(message)
72
+ end
65
73
  })
66
- puts(history)
67
- test( history.length >= 1, 'Display History' )
74
+ #puts(history)
75
+ #test( history.length >= 1, 'Display History' )
68
76
 
69
77
  ## ---------------------------------------------------------------------------
70
78
  ## Subscribe
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 10
9
- version: 0.1.10
8
+ - 11
9
+ version: 0.1.11
10
10
  platform: ruby
11
11
  authors:
12
12
  - Luke Carpenter / PubNub.com