pubnub-ruby 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/lib/pubnub-ruby.rb +1 -1
  2. data/lib/pubnub.rb +50 -40
  3. metadata +33 -44
  4. data/LICENSE.PUBNUB +0 -27
  5. data/README +0 -2
  6. data/README.ROOT +0 -39
@@ -1 +1 @@
1
- require "pubnub"
1
+ require "./pubnub.rb"
@@ -1,5 +1,5 @@
1
- ## www.pubnub.com - PubNub realtime push service in the cloud.
2
- ## http://www.pubnub.com/blog/ruby-push-api - Ruby Push API Blog
1
+ ## www.pubnub.com - PubNub realtime push service in the cloud.
2
+ ## http://www.pubnub.com/blog/ruby-push-api - Ruby Push API Blog
3
3
 
4
4
  ## PubNub Real Time Push APIs and Notifications Framework
5
5
  ## Copyright (c) 2010 Stephen Blum
@@ -12,10 +12,13 @@
12
12
  require 'digest/md5'
13
13
  require 'open-uri'
14
14
  require 'uri'
15
+ require 'net/http'
15
16
  require 'json'
16
17
  require 'pp'
17
18
 
18
19
  class Pubnub
20
+ MAX_RETRIES = 3
21
+
19
22
  #**
20
23
  #* Pubnub
21
24
  #*
@@ -39,6 +42,9 @@ class Pubnub
39
42
  else
40
43
  @origin = 'http://' + @origin
41
44
  end
45
+
46
+ uri = URI.parse(@origin)
47
+ @connection = Net::HTTP.start(uri.host, uri.port)
42
48
  end
43
49
 
44
50
  #**
@@ -100,7 +106,6 @@ class Pubnub
100
106
  ## Capture User Input
101
107
  channel = args['channel']
102
108
  callback = args['callback']
103
- timetoken = args['timetoken'] ? args['timetoken'] : 0
104
109
 
105
110
  ## Fail if missing channel
106
111
  if !channel
@@ -114,39 +119,36 @@ class Pubnub
114
119
  return false
115
120
  end
116
121
 
117
- ## Begin Recusive Subscribe
118
- begin
119
- ## Wait for Message
120
- response = self._request([
121
- 'subscribe',
122
- @subscribe_key,
123
- channel,
124
- '0',
125
- timetoken.to_s
126
- ])
127
-
128
- messages = response[0]
129
- args['timetoken'] = response[1]
130
-
131
- ## If it was a timeout
132
- if !messages.length
133
- return self.subscribe(args)
134
- end
135
-
136
- ## Run user Callback and Reconnect if user permits.
137
- messages.each do |message|
138
- if !callback.call(message)
139
- return
122
+ ## Begin Subscribe
123
+ loop do
124
+ begin
125
+ timetoken = args['timetoken'] ? args['timetoken'] : 0
126
+
127
+ ## Wait for Message
128
+ response = self._request([
129
+ 'subscribe',
130
+ @subscribe_key,
131
+ channel,
132
+ '0',
133
+ timetoken.to_s
134
+ ])
135
+
136
+ messages = response[0]
137
+ args['timetoken'] = response[1]
138
+
139
+ ## If it was a timeout
140
+ next if !messages.length
141
+
142
+ ## Run user Callback and Reconnect if user permits.
143
+ messages.each do |message|
144
+ if !callback.call(message)
145
+ return
146
+ end
140
147
  end
148
+ rescue Timeout::Error
149
+ rescue
150
+ sleep(1)
141
151
  end
142
-
143
- ## Keep Listening.
144
- return self.subscribe(args)
145
- rescue Timeout::Error
146
- return self.subscribe(args)
147
- rescue
148
- sleep(1)
149
- return self.subscribe(args)
150
152
  end
151
153
  end
152
154
 
@@ -201,17 +203,25 @@ class Pubnub
201
203
  #*
202
204
  def _request(request)
203
205
  ## Construct Request URL
204
- url = @origin + '/' + request.map{ |bit| bit.split('').map{ |ch|
206
+ url = '/' + request.map{ |bit| bit.split('').map{ |ch|
205
207
  ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
206
208
  '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
207
209
  }.join('') }.join('/')
208
210
 
209
- response = ''
210
- open(url) do |f|
211
- response = f.read
212
- end
211
+ response = send_with_retries(url, MAX_RETRIES)
212
+ JSON.parse(response)
213
+ end
214
+
215
+ private
213
216
 
214
- return JSON.parse(response)
217
+ def send_with_retries(url, retries)
218
+ tries = 0
219
+ begin
220
+ @connection.get(url).body
221
+ rescue
222
+ tries += 1
223
+ tries < retries ? retry : raise
224
+ end
215
225
  end
216
226
  end
217
227
 
metadata CHANGED
@@ -1,74 +1,63 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: pubnub-ruby
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
4
5
  prerelease:
5
- version: 0.0.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Luke Carpenter
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-03 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: json
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &19743640 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :runtime
25
- version_requirements: *id001
26
- description: Simply Pubnub.rb in gem format, :require => "pubnub" - ask @rubynerd for upgrades
27
- email: orlyowlizere@gmail.com
23
+ prerelease: false
24
+ version_requirements: *19743640
25
+ description: Simply Pubnub.rb in gem format, :require => "pubnub" - ask @rubynerd
26
+ for upgrades
27
+ email: x@rubynerd.net
28
28
  executables: []
29
-
30
29
  extensions: []
31
-
32
- extra_rdoc_files:
33
- - LICENSE.PUBNUB
34
- - README
35
- - README.ROOT
36
- files:
30
+ extra_rdoc_files: []
31
+ files:
37
32
  - examples/history-example.rb
38
33
  - examples/publish-example.rb
39
34
  - examples/subscribe-example.rb
40
35
  - lib/pubnub-ruby.rb
41
36
  - lib/pubnub.rb
42
37
  - tests/unit-test.rb
43
- - LICENSE.PUBNUB
44
- - README
45
- - README.ROOT
46
- homepage: http://github.com/rubynerd/pubnub
47
- licenses:
38
+ homepage: http://github.com/pubnub/pubnub-api/tree/master/ruby
39
+ licenses:
48
40
  - MIT
49
41
  post_install_message:
50
42
  rdoc_options: []
51
-
52
- require_paths:
43
+ require_paths:
53
44
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
45
+ required_ruby_version: !ruby/object:Gem::Requirement
55
46
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
52
  none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- version: "0"
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
66
57
  requirements: []
67
-
68
58
  rubyforge_project:
69
- rubygems_version: 1.7.2
59
+ rubygems_version: 1.8.11
70
60
  signing_key:
71
61
  specification_version: 3
72
62
  summary: PubNub unofficial gem
73
63
  test_files: []
74
-
@@ -1,27 +0,0 @@
1
- PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
2
- Copyright (c) 2011 TopMambo Inc.
3
- http://www.pubnub.com/
4
- http://www.pubnub.com/terms
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in
14
- all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
- THE SOFTWARE.
23
-
24
- PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
25
- Copyright (c) 2011 TopMambo Inc.
26
- http://www.pubnub.com/
27
- http://www.pubnub.com/terms
data/README DELETED
@@ -1,2 +0,0 @@
1
- Pubnub - http://github/pubnub/pubnub-api
2
- @rubynerd on Twitter
@@ -1,39 +0,0 @@
1
- ## ---------------------------------------------------
2
- ##
3
- ## YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
4
- ## http://www.pubnub.com/account
5
- ##
6
- ## ----------------------------------------------------
7
-
8
- ## -----------------------------------
9
- ## PubNub 3.0 Real-time Cloud Push API
10
- ## -----------------------------------
11
- ##
12
- ## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
13
- ## http://www.pubnub.com/tutorial/javascript-push-api
14
- ##
15
- ## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
16
- ## This is a cloud-based service for broadcasting Real-time messages
17
- ## to thousands of web and mobile clients simultaneously.
18
-
19
- JavaScript Tutorial: http://www.pubnub.com/tutorial/javascript-push-api
20
-
21
- Website: http://www.pubnub.com
22
- Videos: http://www.pubnub.com/#videos
23
- Docs: http://www.pubnub.com/tutorial
24
- Blog: http://www.pubnub.com/blog
25
- Podcast Interview: http://techzinglive.com/?p=227
26
-
27
-
28
- ===============================================================================
29
- PubNub Client APIs!
30
- ===============================================================================
31
- Visit the PubNub website for more details at http://www.pubnub.com/
32
- PubNub is a client-to-client push service in the cloud.
33
-
34
- This public repository hosts client APIs for PubNub.
35
- PHP, Ruby, JavaScript and more.
36
-
37
- The API is SO FAST that iPhone, Android and Blackberry phones zip!
38
- The JavaScript Payload is less than 3KB.
39
- It's a breeze for mobile phones.