pubnub 3.3.0.1 → 3.3.0.2

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/lib/pubnub.rb CHANGED
@@ -15,9 +15,10 @@ require 'uri'
15
15
 
16
16
  require 'pubnub_crypto'
17
17
  require 'pubnub_request'
18
- require 'pubnub_deferrable'
19
18
 
20
19
  require 'eventmachine'
20
+ require 'em-http-request'
21
+ require 'yajl'
21
22
  require 'uuid'
22
23
  require 'active_support/core_ext/hash/indifferent_access'
23
24
  require 'active_support/core_ext/string/inflections'
@@ -95,7 +96,7 @@ class Pubnub
95
96
 
96
97
  publish_request.format_url!
97
98
 
98
- _request(publish_request)
99
+ check_for_em publish_request
99
100
  end
100
101
 
101
102
  def subscribe(options)
@@ -117,7 +118,7 @@ class Pubnub
117
118
  format_url_options = options[:override_timetoken].present? ? options[:override_timetoken] : nil
118
119
  subscribe_request.format_url!(format_url_options)
119
120
 
120
- _request(subscribe_request)
121
+ check_for_em subscribe_request
121
122
 
122
123
  end
123
124
 
@@ -159,7 +160,7 @@ class Pubnub
159
160
  here_now_request.set_subscribe_key(options, self.subscribe_key)
160
161
 
161
162
  here_now_request.format_url!
162
- _request(here_now_request)
163
+ check_for_em here_now_request
163
164
 
164
165
  end
165
166
 
@@ -195,7 +196,7 @@ class Pubnub
195
196
  detailed_history_request.history_reverse = options[:reverse]
196
197
 
197
198
  detailed_history_request.format_url!
198
- _request(detailed_history_request)
199
+ check_for_em detailed_history_request
199
200
 
200
201
  end
201
202
 
@@ -227,7 +228,7 @@ class Pubnub
227
228
  history_request.history_limit = options[:limit]
228
229
 
229
230
  history_request.format_url!
230
- _request(history_request)
231
+ check_for_em history_request
231
232
 
232
233
  end
233
234
 
@@ -239,7 +240,7 @@ class Pubnub
239
240
  time_request.set_callback(options)
240
241
 
241
242
  time_request.format_url!
242
- _request(time_request)
243
+ check_for_em time_request
243
244
  end
244
245
 
245
246
  def my_callback(x, quiet = false)
@@ -255,101 +256,73 @@ class Pubnub
255
256
  UUID.new.generate
256
257
  end
257
258
 
258
- def _request(request)
259
-
260
- if (defined?(Rails) && Rails.present? && Rails.env.present?) && Rails.env.test?
261
-
262
- open(request.url, 'r', :read_timeout => 300) do |response|
263
- request.package_response!(response.read)
264
- request.callback.call(request.response)
265
- request.response
266
- end
267
-
259
+ def check_for_em request
260
+ if EM.reactor_running?
261
+ _request(request, true)
268
262
  else
263
+ EM.run do
264
+ _request(request)
265
+ end
266
+ end
267
+ end
269
268
 
270
- request.format_url!
271
- #puts("- Fetching #{request.url}")
272
-
269
+ private
270
+
271
+ def _request(request, is_reactor_running = false)
272
+ request.format_url!
273
+ #puts("- Fetching #{request.url}")
274
+ Thread.new{
273
275
  begin
274
276
 
275
- EM.run do
276
-
277
- conn = PubnubDeferrable.connect request.host, request.port
278
- conn.pubnub_request = request
279
- req = conn.get(request.query)
280
-
281
- timeout_timer = EM.add_periodic_timer(290) do
282
- #puts("#{Time.now}: Reconnecting from timeout.")
283
- reconnect_and_query(conn, request)
284
- end
277
+ conn = EM::HttpRequest.new(request.url, :inactivity_timeout => 310)#client times out in 310s unless the server returns or timeout first
278
+ req = conn.get()
285
279
 
286
- error_timer = EM.add_periodic_timer(5) do
287
- #puts("#{Time.now}: Checking for errors.")
288
- if conn.error?
280
+ req.errback{
281
+ if req.response.blank?
282
+ puts("#{Time.now}: Reconnecting from timeout.")
289
283
 
290
- error_message = "Intermittent Error: #{response.status}, extended info: #{response.internal_error}"
291
- puts(error_message)
292
- request.callback.call([0, error_message])
293
-
294
- reconnect_and_query(conn, request)
284
+ EM::Timer.new(1) do
285
+ _request(request, is_reactor_running)
295
286
  end
296
- end
297
-
298
- req.errback do |response|
299
- conn.close_connection
300
- error_message = "Unknown Error: #{response.to_s}"
301
-
287
+ else
288
+ error_message = "Unknown Error: #{req.response.to_s}"
302
289
  puts(error_message)
303
290
  request.callback.call([0, error_message])
304
291
 
305
- reconnect_and_query(conn, request)
292
+ EM.stop unless is_reactor_running
306
293
  end
294
+ }
307
295
 
308
- req.callback do |response|
309
-
310
- if response.status != 200
311
- error_message = "Server Error, status: #{response.status}, extended info: #{response.internal_error}"
312
-
313
- puts(error_message)
314
- request.callback.call([0, error_message])
315
-
316
- conn.reconnect request.host, request.port
317
- end
318
-
319
- request.package_response!(response.content)
320
- request.callback.call(request.response)
321
-
322
- EM.next_tick do
323
- if %w(subscribe presence).include?(request.operation)
324
- conn.close_connection
296
+ req.callback {
297
+ request.package_response!(req.response)
298
+ cycle = request.callback.call(request.response)
325
299
 
326
- timeout_timer.cancel
327
- error_timer.cancel
300
+ only_success_status_is_acceptable = 200
328
301
 
329
- _request(request)
330
- else
331
- conn.close_connection
332
- return request.response
302
+ if (req.response_header.http_status.to_i != only_success_status_is_acceptable)
333
303
 
334
- end
304
+ error_message = "Server Error, status: #{req.response_header.http_status}, extended info: #{req.response}"
305
+ puts(error_message)
306
+ EM.stop unless is_reactor_running
307
+ else
335
308
 
309
+ if %w(subscribe presence).include?(request.operation) && (cycle != false || request.first_request?)
310
+ _request(request, is_reactor_running)
311
+ else
312
+ EM.stop unless is_reactor_running
336
313
  end
314
+
337
315
  end
338
- end
316
+ }
339
317
 
340
- rescue EventMachine::ConnectionError => e
318
+ rescue EventMachine::ConnectionError, RuntimeError => e # RuntimeError for catching "EventMachine not initialized"
341
319
  error_message = "Network Error: #{e.message}"
342
320
  puts(error_message)
343
321
  return [0, error_message]
344
322
  end
345
-
346
- end
323
+ }
347
324
  end
348
325
 
349
- def reconnect_and_query(conn, request)
350
- conn.reconnect request.host, request.port
351
- conn.pubnub_request = request
352
- conn.get(request.query)
353
- end
326
+
354
327
 
355
328
  end
@@ -1,6 +1,6 @@
1
1
  class PubnubRequest
2
2
  attr_accessor :cipher_key, :host, :query, :response, :timetoken, :url, :operation, :callback, :publish_key, :subscribe_key, :secret_key, :channel, :jsonp, :message, :ssl, :port
3
- attr_accessor :history_limit, :history_count, :history_start, :history_end, :history_reverse, :session_uuid
3
+ attr_accessor :history_limit, :history_count, :history_start, :history_end, :history_reverse, :session_uuid, :last_timetoken
4
4
 
5
5
  class RequestError < RuntimeError;
6
6
  end
@@ -142,6 +142,7 @@ class PubnubRequest
142
142
 
143
143
  def package_response!(response_data)
144
144
  self.response = response_data.respond_to?(:content) ? Yajl.load(response_data.content) : Yajl.load(response_data)
145
+ self.last_timetoken = self.timetoken
145
146
  self.timetoken = self.response[1] unless self.operation == "time"
146
147
 
147
148
  if self.cipher_key.present? && %w(subscribe history detailed_history).include?(self.operation)
@@ -284,4 +285,8 @@ class PubnubRequest
284
285
  return url
285
286
  end
286
287
 
288
+ def first_request?
289
+ @last_timetoken == "0"
290
+ end
291
+
287
292
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubnub
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0.1
4
+ version: 3.3.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-18 00:00:00.000000000 Z
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -32,17 +32,33 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.12.9
37
+ version: '0'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 0.12.9
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: em-http-request
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: uuid
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -83,7 +99,6 @@ extra_rdoc_files: []
83
99
  files:
84
100
  - lib/pubnub.rb
85
101
  - lib/pubnub_crypto.rb
86
- - lib/pubnub_deferrable.rb
87
102
  - lib/pubnub_request.rb
88
103
  homepage: http://github.com/pubnub/pubnub-api
89
104
  licenses:
@@ -1,37 +0,0 @@
1
- require 'eventmachine'
2
-
3
- class PubnubDeferrable < EM::Protocols::HttpClient2
4
-
5
- include EM::Deferrable
6
-
7
- attr_accessor :start_time, :end_time, :elapsed_time, :pubnub_request
8
-
9
- def initialize
10
- super
11
- end
12
-
13
- def post_init
14
- #puts("deferrable says my query is: #{pubnub_request.query}")
15
- super
16
- end
17
-
18
- def connection_completed
19
- @start_time = Time.now
20
- #puts("\n--- #{@start_time}: Connected with string: #{self.pubnub_request.query}")
21
- super
22
- end
23
-
24
- def unbind
25
- @end_time = Time.now
26
-
27
- if @start_time == nil
28
- @start_time = Time.now
29
- end
30
-
31
- #puts("-- #{@end_time}: Disconnected.")
32
- #puts("--- Elapsed connection time: #{@end_time - @start_time}s")
33
- super
34
- end
35
-
36
-
37
- end