pubnub-ruby 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README +55 -0
- data/examples/history-example.rb +37 -36
- data/examples/publish-example.rb +28 -27
- data/examples/subscribe-example.rb +16 -13
- data/lib/pubnub.rb +115 -74
- data/tests/unit-test.rb +75 -79
- metadata +54 -33
data/README
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
Pubnub - http://github/pubnub/pubnub-api
|
2
|
+
@poptartinc on Twitter, @poptart on Github
|
3
|
+
|
4
|
+
## ------------------------------------------
|
5
|
+
## PubNub 3.0 Real-time Cloud Push API - RUBY
|
6
|
+
## ------------------------------------------
|
7
|
+
##
|
8
|
+
## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
|
9
|
+
## http://www.pubnub.com/blog/ruby-push-api
|
10
|
+
##
|
11
|
+
## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
|
12
|
+
## This is a cloud-based service for broadcasting Real-time messages
|
13
|
+
## to thousands of web and mobile clients simultaneously.
|
14
|
+
|
15
|
+
## -------------
|
16
|
+
## Ruby Push API
|
17
|
+
## -------------
|
18
|
+
pubnub = Pubnub.new(
|
19
|
+
"demo", ## PUBLISH_KEY
|
20
|
+
"demo", ## SUBSCRIBE_KEY
|
21
|
+
"", ## SECRET_KEY
|
22
|
+
false ## SSL_ON?
|
23
|
+
)
|
24
|
+
|
25
|
+
# -------
|
26
|
+
# PUBLISH
|
27
|
+
# -------
|
28
|
+
# Send Message
|
29
|
+
info = pubnub.publish({
|
30
|
+
'channel' => 'hello_world',
|
31
|
+
'message' => { 'text' => 'some text data' }
|
32
|
+
})
|
33
|
+
puts(info)
|
34
|
+
|
35
|
+
# ---------
|
36
|
+
# SUBSCRIBE
|
37
|
+
# ---------
|
38
|
+
# Listen for Messages *BLOCKING*
|
39
|
+
pubnub.subscribe({
|
40
|
+
'channel' => 'hello_world',
|
41
|
+
'callback' => lambda do |message|
|
42
|
+
puts(message) ## print message
|
43
|
+
return true ## keep listening?
|
44
|
+
end
|
45
|
+
})
|
46
|
+
|
47
|
+
# -------
|
48
|
+
# HISTORY
|
49
|
+
# -------
|
50
|
+
# Load Previously Published Messages
|
51
|
+
messages = pubnub.history({
|
52
|
+
'channel' => 'hello_world',
|
53
|
+
'limit' => 10
|
54
|
+
})
|
55
|
+
puts(messages)
|
data/examples/history-example.rb
CHANGED
@@ -1,36 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
##
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
1
|
+
##including required libraries
|
2
|
+
require 'rubygems'
|
3
|
+
require './lib/pubnub.rb'
|
4
|
+
|
5
|
+
##declaring publish_key, subscribe_key, secret_key, cipher_key
|
6
|
+
publish_key = 'demo'
|
7
|
+
subscribe_key = 'demo'
|
8
|
+
secret_key =''
|
9
|
+
cipher_key ='demo'
|
10
|
+
ssl_on = !!ARGV[4]
|
11
|
+
|
12
|
+
## Print usage if missing info.
|
13
|
+
if !subscribe_key
|
14
|
+
puts('
|
15
|
+
Get API Keys at http://www.pubnub.com/account
|
16
|
+
==============
|
17
|
+
EXAMPLE USAGE:
|
18
|
+
==============
|
19
|
+
ruby history-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
|
20
|
+
ruby history-example.rb demo demo true
|
21
|
+
')
|
22
|
+
end
|
23
|
+
|
24
|
+
## Create Pubnub Client API (INITIALIZATION)
|
25
|
+
|
26
|
+
puts('Creating new Pubnub Client API')
|
27
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on=false)
|
28
|
+
|
29
|
+
## Request Past Publishes (HISTORY)
|
30
|
+
|
31
|
+
puts('Requesting History with history() Function')
|
32
|
+
message = pubnub.history(
|
33
|
+
{
|
34
|
+
'channel' => 'HelloWorld',
|
35
|
+
'limit' => 20
|
36
|
+
})
|
37
|
+
puts(message)
|
data/examples/publish-example.rb
CHANGED
@@ -1,39 +1,40 @@
|
|
1
|
-
|
1
|
+
##including required libraries
|
2
|
+
require './lib/pubnub.rb'
|
3
|
+
require 'json'
|
2
4
|
|
3
|
-
publish_key
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
##declaring publish_key, subscribe_key, secret_key, cipher_key, message
|
6
|
+
publish_key = 'demo'
|
7
|
+
subscribe_key = 'demo'
|
8
|
+
secret_key = 'demo'
|
9
|
+
cipher_key = 'demo'
|
10
|
+
message = 'HelloRuby'
|
11
|
+
channel = 'HelloWorld'
|
7
12
|
ssl_on = !!ARGV[4]
|
8
13
|
|
9
14
|
## Print usage if missing info.
|
10
15
|
if !message
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
## -----------------------------------------
|
16
|
+
puts('
|
17
|
+
Get API Keys at http://www.pubnub.com/account
|
18
|
+
==============
|
19
|
+
EXAMPLE USAGE:
|
20
|
+
==============
|
21
|
+
ruby publish-example.rb PUB-KEY SUB-KEY SECRET-KEY "message text" SSL-ON
|
22
|
+
ruby publish-example.rb demo demo "" "hey what is up?" true
|
23
|
+
')
|
24
|
+
|
25
|
+
end
|
26
|
+
|
24
27
|
## Create Pubnub Client API (INITIALIZATION)
|
25
|
-
|
28
|
+
|
26
29
|
puts('Creating new Pubnub Client API')
|
27
|
-
pubnub = Pubnub.new(
|
30
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on=false)
|
28
31
|
|
29
|
-
## ----------------------
|
30
32
|
## Send Message (PUBLISH)
|
31
|
-
|
33
|
+
|
32
34
|
puts('Sending a message with publish() Function')
|
33
|
-
info = pubnub.publish({
|
34
|
-
|
35
|
-
'message' => message
|
36
|
-
})
|
35
|
+
info = pubnub.publish({'channel' => channel ,'message' => message})
|
36
|
+
|
37
37
|
|
38
|
-
## Print
|
38
|
+
## Print Info
|
39
39
|
puts(info)
|
40
|
+
|
@@ -1,9 +1,15 @@
|
|
1
|
-
|
1
|
+
##including required libraries
|
2
|
+
require 'rubygems'
|
3
|
+
require './lib/pubnub.rb'
|
2
4
|
|
3
|
-
publish_key
|
4
|
-
|
5
|
+
##declaring publish_key, subscribe_key, secret_key, cipher_key
|
6
|
+
publish_key = 'demo'
|
7
|
+
subscribe_key = 'demo'
|
8
|
+
secret_key = 'demo'
|
9
|
+
cipher_key = 'demo'
|
5
10
|
ssl_on = !!ARGV[2]
|
6
11
|
|
12
|
+
|
7
13
|
## Print usage if missing info.
|
8
14
|
if !subscribe_key
|
9
15
|
puts('
|
@@ -13,24 +19,21 @@ if !subscribe_key
|
|
13
19
|
==============
|
14
20
|
ruby subscribe-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
|
15
21
|
ruby subscribe-example.rb demo demo true
|
16
|
-
|
17
|
-
')
|
18
|
-
exit()
|
22
|
+
')
|
19
23
|
end
|
20
24
|
|
21
|
-
## -----------------------------------------
|
22
25
|
## Create Pubnub Client API (INITIALIZATION)
|
23
|
-
|
26
|
+
|
24
27
|
puts('Creating new Pubnub Client API')
|
25
|
-
pubnub = Pubnub.new(
|
28
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on=false)
|
29
|
+
|
30
|
+
## Listen for Messages (SUBSCRIBE)
|
26
31
|
|
27
|
-
## -------------------------------
|
28
|
-
## Listen for Messages (SUBSCRIBE)
|
29
|
-
## -------------------------------
|
30
32
|
puts('Listening for new messages with subscribe() Function')
|
31
33
|
puts('Press CTRL+C to quit.')
|
34
|
+
|
32
35
|
pubnub.subscribe({
|
33
|
-
'channel' => '
|
36
|
+
'channel' => 'HelloWorld',
|
34
37
|
'callback' => lambda do |message|
|
35
38
|
puts(message) ## print message
|
36
39
|
return true ## keep listening?
|
data/lib/pubnub.rb
CHANGED
@@ -9,17 +9,24 @@
|
|
9
9
|
## PubNub 3.0 Real-time Push Cloud API
|
10
10
|
## -----------------------------------
|
11
11
|
|
12
|
-
|
12
|
+
|
13
|
+
##including required libraries
|
14
|
+
require 'openssl'
|
15
|
+
require 'base64'
|
13
16
|
require 'open-uri'
|
14
17
|
require 'uri'
|
15
18
|
require 'net/http'
|
16
19
|
require 'json'
|
17
20
|
require 'pp'
|
21
|
+
require 'rubygems'
|
22
|
+
require 'securerandom'
|
23
|
+
require 'digest'
|
24
|
+
require './lib/PubnubCrypto.rb'
|
18
25
|
|
19
26
|
class Pubnub
|
20
27
|
MAX_RETRIES = 3
|
21
|
-
|
22
|
-
|
28
|
+
retries=0
|
29
|
+
|
23
30
|
#* Pubnub
|
24
31
|
#*
|
25
32
|
#* Init the Pubnub Client API
|
@@ -27,12 +34,14 @@ class Pubnub
|
|
27
34
|
#* @param string publish_key required key to send messages.
|
28
35
|
#* @param string subscribe_key required key to receive messages.
|
29
36
|
#* @param string secret_key required key to sign messages.
|
37
|
+
#*@param string cipher_key required to encrypt messages.
|
30
38
|
#* @param boolean ssl required for 2048 bit encrypted messages.
|
31
39
|
#*
|
32
|
-
def initialize( publish_key, subscribe_key, secret_key, ssl_on = false)
|
40
|
+
def initialize( publish_key, subscribe_key, secret_key, cipher_key, ssl_on = false)
|
33
41
|
@publish_key = publish_key
|
34
42
|
@subscribe_key = subscribe_key
|
35
43
|
@secret_key = secret_key
|
44
|
+
@cipher_key = cipher_key
|
36
45
|
@ssl = ssl_on
|
37
46
|
@origin = 'pubsub.pubnub.com'
|
38
47
|
@limit = 1800
|
@@ -47,55 +56,84 @@ class Pubnub
|
|
47
56
|
http = Net::HTTP.new(uri.host, uri.port)
|
48
57
|
http.use_ssl = ssl_on
|
49
58
|
@connection = http.start()
|
59
|
+
|
60
|
+
puts('Connection done')
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
#* UUID
|
66
|
+
#*
|
67
|
+
#* Unique identifier generation
|
68
|
+
#* @Return Unique Identifier
|
69
|
+
#*
|
70
|
+
def UUID()
|
71
|
+
uuid=SecureRandom.base64(32).gsub("/","_").gsub(/=+$/,"")
|
50
72
|
end
|
51
73
|
|
52
|
-
|
74
|
+
|
53
75
|
#* Publish
|
54
76
|
#*
|
55
77
|
#* Send a message to a channel.
|
56
78
|
#*
|
57
79
|
#* @param array args with channel and message.
|
58
80
|
#* @return array success information.
|
59
|
-
|
81
|
+
|
60
82
|
def publish(args)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
## Capture User Input
|
68
|
-
channel = args['channel']
|
69
|
-
message = args['message'].to_json
|
83
|
+
|
84
|
+
## Fail if bad input.
|
85
|
+
if !(args['channel'] && args['message'])
|
86
|
+
puts('Missing Channel or message')
|
87
|
+
return false
|
88
|
+
end
|
70
89
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
90
|
+
## Capture User Input
|
91
|
+
channel = args['channel']
|
92
|
+
message = args['message'].to_json
|
93
|
+
puts message
|
94
|
+
|
95
|
+
#encryption of message
|
96
|
+
if @cipher_key.length > 0
|
97
|
+
pubnubcrypto=PubnubCrypto.new('cipher_key')
|
98
|
+
message=message.chop.reverse.chop.reverse()
|
99
|
+
puts('message is->'+message)
|
100
|
+
message=pubnubcrypto.encrypt(message)
|
101
|
+
puts('Encrypted message->'+message)
|
102
|
+
message = message.strip
|
103
|
+
message = '"' +message+ '"'
|
104
|
+
end
|
105
|
+
|
106
|
+
## Sign message using HMAC
|
107
|
+
String signature = '0'
|
79
108
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
109
|
+
if @secret_key.length > 0
|
110
|
+
signature = "{@publish_key,@subscribe_key,@secret_key,channel,message}"
|
111
|
+
digest = OpenSSL::Digest.new("sha256")
|
112
|
+
key = [ @secret_key ]
|
113
|
+
hmac = OpenSSL::HMAC.hexdigest(digest, key.pack("H*"), signature)
|
114
|
+
signature = hmac
|
115
|
+
end
|
116
|
+
puts "signature >> "+signature
|
117
|
+
|
118
|
+
|
119
|
+
##If message length is greater than limit output fails
|
120
|
+
if message.length > @limit
|
121
|
+
puts('message TOO LONG (' + @limit.to_s + ' LIMIT)')
|
122
|
+
return [ 0, 'message Too Long.' ]
|
123
|
+
end
|
85
124
|
|
86
|
-
|
87
|
-
|
125
|
+
## Send message
|
126
|
+
return self._request([
|
88
127
|
'publish',
|
89
128
|
@publish_key,
|
90
129
|
@subscribe_key,
|
91
130
|
signature,
|
92
131
|
channel,
|
93
132
|
'0',
|
94
|
-
message
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
#**
|
133
|
+
message,])
|
134
|
+
end
|
135
|
+
|
136
|
+
|
99
137
|
#* Subscribe
|
100
138
|
#*
|
101
139
|
#* This is BLOCKING.
|
@@ -105,10 +143,10 @@ class Pubnub
|
|
105
143
|
#* @return false on fail, array on success.
|
106
144
|
#*
|
107
145
|
def subscribe(args)
|
108
|
-
|
146
|
+
## Capture User Input
|
109
147
|
channel = args['channel']
|
110
148
|
callback = args['callback']
|
111
|
-
|
149
|
+
|
112
150
|
## Fail if missing channel
|
113
151
|
if !channel
|
114
152
|
puts "Missing Channel."
|
@@ -120,41 +158,42 @@ class Pubnub
|
|
120
158
|
puts "Missing Callback."
|
121
159
|
return false
|
122
160
|
end
|
123
|
-
|
161
|
+
|
124
162
|
## Begin Subscribe
|
125
163
|
loop do
|
126
164
|
begin
|
127
165
|
timetoken = args['timetoken'] ? args['timetoken'] : 0
|
128
166
|
|
129
|
-
## Wait for
|
167
|
+
## Wait for message
|
130
168
|
response = self._request([
|
131
169
|
'subscribe',
|
132
170
|
@subscribe_key,
|
133
171
|
channel,
|
134
172
|
'0',
|
135
|
-
timetoken.to_s
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
args['timetoken'] = response[1]
|
140
|
-
|
173
|
+
timetoken.to_s])
|
174
|
+
messages = response[0]
|
175
|
+
args['timetoken'] = response[1]
|
176
|
+
|
141
177
|
## If it was a timeout
|
142
178
|
next if !messages.length
|
143
179
|
|
144
180
|
## Run user Callback and Reconnect if user permits.
|
145
|
-
|
181
|
+
##Capture the message and encrypt it
|
182
|
+
messages.each do |message|
|
183
|
+
pc = PubnubCrypto.new('cipher_key')
|
184
|
+
message = pc.decrypt(message)
|
146
185
|
if !callback.call(message)
|
147
|
-
return
|
186
|
+
return
|
148
187
|
end
|
149
188
|
end
|
150
|
-
rescue
|
189
|
+
rescue Timeout::Error
|
151
190
|
rescue
|
152
|
-
|
153
|
-
|
191
|
+
sleep(1)
|
192
|
+
end
|
154
193
|
end
|
155
194
|
end
|
156
|
-
|
157
|
-
|
195
|
+
|
196
|
+
|
158
197
|
#* History
|
159
198
|
#*
|
160
199
|
#* Load history from a channel.
|
@@ -163,27 +202,27 @@ class Pubnub
|
|
163
202
|
#* @return mixed false on fail, array on success.
|
164
203
|
#*
|
165
204
|
def history(args)
|
166
|
-
|
167
|
-
limit = +args['limit'] ? +args['limit'] :
|
205
|
+
## Capture User Input
|
206
|
+
limit = +args['limit'] ? +args['limit'] : 15
|
168
207
|
channel = args['channel']
|
169
|
-
|
170
|
-
|
208
|
+
|
209
|
+
## Fail if bad input.
|
171
210
|
if (!channel)
|
172
211
|
puts 'Missing Channel.'
|
173
212
|
return false
|
174
213
|
end
|
175
214
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
215
|
+
## Get History
|
216
|
+
response = self._request([ 'history', @subscribe_key,channel,'0',limit.to_s])
|
217
|
+
myarr=Array.new()
|
218
|
+
response.each do |message|
|
219
|
+
pc = PubnubCrypto.new('cipher_key')
|
220
|
+
message = pc.decrypt(message)
|
221
|
+
myarr.push(message)
|
222
|
+
end
|
223
|
+
return myarr
|
184
224
|
end
|
185
|
-
|
186
|
-
#**
|
225
|
+
|
187
226
|
#* Time
|
188
227
|
#*
|
189
228
|
#* Timestamp from PubNub Cloud.
|
@@ -197,7 +236,6 @@ class Pubnub
|
|
197
236
|
])[0]
|
198
237
|
end
|
199
238
|
|
200
|
-
#**
|
201
239
|
#* Request URL
|
202
240
|
#*
|
203
241
|
#* @param array request of url directories.
|
@@ -209,21 +247,24 @@ class Pubnub
|
|
209
247
|
' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
|
210
248
|
'%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
|
211
249
|
}.join('') }.join('/')
|
212
|
-
|
213
|
-
|
250
|
+
|
251
|
+
puts(' URL==> '+url)
|
252
|
+
|
253
|
+
response = send_with_retries(url,MAX_RETRIES )
|
214
254
|
JSON.parse(response)
|
215
255
|
end
|
216
256
|
|
217
257
|
private
|
218
|
-
|
219
|
-
def send_with_retries(url, retries)
|
258
|
+
def send_with_retries(url, retries)
|
220
259
|
tries = 0
|
260
|
+
|
221
261
|
begin
|
222
262
|
@connection.get(url).body
|
223
|
-
|
263
|
+
rescue
|
264
|
+
|
224
265
|
tries += 1
|
225
266
|
tries < retries ? retry : raise
|
226
267
|
end
|
227
|
-
end
|
228
|
-
end
|
229
268
|
|
269
|
+
end
|
270
|
+
end
|
data/tests/unit-test.rb
CHANGED
@@ -1,79 +1,75 @@
|
|
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
|
-
|
4
|
-
## PubNub Real Time Push APIs and Notifications Framework
|
5
|
-
## Copyright (c) 2010 Stephen Blum
|
6
|
-
## http://www.pubnub.com/
|
7
|
-
|
8
|
-
## -----------------------------------
|
9
|
-
## PubNub 3.0 Real-time Push Cloud API
|
10
|
-
## -----------------------------------
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
ssl_on
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
##
|
27
|
-
##
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
## ---------------------------------------------------------------------------
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
##
|
42
|
-
##
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
pubish_success
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
history
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
##
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
puts(message) ## print message
|
77
|
-
return true ## keep listening?
|
78
|
-
end
|
79
|
-
})
|
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
|
+
|
4
|
+
## PubNub Real Time Push APIs and Notifications Framework
|
5
|
+
## Copyright (c) 2010 Stephen Blum
|
6
|
+
## http://www.pubnub.com/
|
7
|
+
|
8
|
+
## -----------------------------------
|
9
|
+
## PubNub 3.0 Real-time Push Cloud API
|
10
|
+
## -----------------------------------
|
11
|
+
|
12
|
+
##including required libraries
|
13
|
+
require 'rubygems'
|
14
|
+
require './lib/pubnub.rb'
|
15
|
+
|
16
|
+
##declaring publish_key, subscribe_key, secret_key, cipher_key, message, ssl_on
|
17
|
+
publish_key = ARGV[0] || 'demo'
|
18
|
+
subscribe_key = ARGV[1] || 'demo'
|
19
|
+
secret_key = ARGV[2] || 'demo'
|
20
|
+
cipher_key = ARGV[3]||'demo'
|
21
|
+
message ='hellounit'
|
22
|
+
ssl_on = !!ARGV[4]
|
23
|
+
|
24
|
+
|
25
|
+
## ---------------------------------------------------------------------------
|
26
|
+
## Create Pubnub Client API (INITIALIZATION)
|
27
|
+
## ---------------------------------------------------------------------------
|
28
|
+
pubnub = Pubnub.new( publish_key, subscribe_key, secret_key,cipher_key, ssl_on=false )
|
29
|
+
channel = 'HelloWorld' + rand().to_s
|
30
|
+
|
31
|
+
|
32
|
+
## ---------------------------------------------------------------------------
|
33
|
+
## Unit Test Function
|
34
|
+
## ---------------------------------------------------------------------------
|
35
|
+
def test( trial, name )
|
36
|
+
trial ? puts('PASS: ' + name) : puts('FAIL: ' + name)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
## ---------------------------------------------------------------------------
|
41
|
+
## PubNub Server Time
|
42
|
+
## ---------------------------------------------------------------------------
|
43
|
+
timestamp = pubnub.time()
|
44
|
+
test( timestamp > 0, 'PubNub Server Time: ' + timestamp.to_s )
|
45
|
+
|
46
|
+
|
47
|
+
## ---------------------------------------------------------------------------
|
48
|
+
## PUBLISH
|
49
|
+
## ---------------------------------------------------------------------------
|
50
|
+
first_message = 'Hi. (顶顅Ȓ)'
|
51
|
+
pubish_success = pubnub.publish({'channel' => 'HelloWorld' ,'message' => message})
|
52
|
+
test( pubish_success[0] == 1, 'Publish First Message Success' )
|
53
|
+
|
54
|
+
## ---------------------------------------------------------------------------
|
55
|
+
## HISTORY
|
56
|
+
## ---------------------------------------------------------------------------
|
57
|
+
history = pubnub.history({
|
58
|
+
'channel' => 'HelloWorld',
|
59
|
+
'limit' => 20
|
60
|
+
})
|
61
|
+
puts(history)
|
62
|
+
test( history.length >= 1, 'Display History' )
|
63
|
+
|
64
|
+
## ---------------------------------------------------------------------------
|
65
|
+
## Subscribe
|
66
|
+
## ---------------------------------------------------------------------------
|
67
|
+
puts('Listening for new messages with subscribe() Function')
|
68
|
+
puts('Press CTRL+C to quit.')
|
69
|
+
pubnub.subscribe({
|
70
|
+
'channel' => 'HelloWorld',
|
71
|
+
'callback' => lambda do |message|
|
72
|
+
puts(message) ## print message
|
73
|
+
return true ## keep listening?
|
74
|
+
end
|
75
|
+
})
|
metadata
CHANGED
@@ -1,63 +1,84 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: pubnub-ruby
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Luke Carpenter
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2011-05-28 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: json
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
22
32
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
upgrades
|
27
|
-
email: x@rubynerd.net
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Simply Pubnub.rb in gem format, :require => "pubnub" - ask @rubynerd for upgrades
|
35
|
+
email: orlyowlizere@gmail.com
|
28
36
|
executables: []
|
37
|
+
|
29
38
|
extensions: []
|
30
|
-
|
31
|
-
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README
|
42
|
+
files:
|
32
43
|
- examples/history-example.rb
|
33
44
|
- examples/publish-example.rb
|
34
45
|
- examples/subscribe-example.rb
|
35
46
|
- lib/pubnub-ruby.rb
|
36
47
|
- lib/pubnub.rb
|
37
48
|
- tests/unit-test.rb
|
38
|
-
|
39
|
-
|
49
|
+
- README
|
50
|
+
homepage: http://github.com/rubynerd/pubnub
|
51
|
+
licenses:
|
40
52
|
- MIT
|
41
53
|
post_install_message:
|
42
54
|
rdoc_options: []
|
43
|
-
|
55
|
+
|
56
|
+
require_paths:
|
44
57
|
- lib
|
45
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
59
|
none: false
|
47
|
-
requirements:
|
48
|
-
- -
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
|
51
|
-
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
68
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
57
76
|
requirements: []
|
77
|
+
|
58
78
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
79
|
+
rubygems_version: 1.8.5
|
60
80
|
signing_key:
|
61
81
|
specification_version: 3
|
62
82
|
summary: PubNub unofficial gem
|
63
83
|
test_files: []
|
84
|
+
|