pubnub 0.1.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/README +83 -0
- data/examples/history_example.rb +38 -0
- data/examples/publish_example.rb +60 -0
- data/examples/subscribe_example.rb +43 -0
- data/examples/uuid_example.rb +15 -0
- data/lib/pubnub.rb +1 -0
- data/lib/pubnub/pubnub.rb +297 -0
- data/lib/pubnub/pubnub_crypto.rb +142 -0
- data/tests/unit_test.rb +80 -0
- metadata +112 -0
data/README
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
Pubnub - http://github/pubnub/pubnub-api
|
2
|
+
@poptartinc on Twitter, @poptart on Github
|
3
|
+
|
4
|
+
## ------------------------------------------
|
5
|
+
## PubNub 3.1 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
|
+
"demo", ## SECRET_KEY
|
22
|
+
"demo", ## CIPHER_KEY
|
23
|
+
false ## SSL_ON?
|
24
|
+
)
|
25
|
+
|
26
|
+
# -------
|
27
|
+
# PUBLISH STRING MESSAGE
|
28
|
+
# -------
|
29
|
+
# Send Message
|
30
|
+
info = pubnub.publish({
|
31
|
+
'channel' => 'hello_world',
|
32
|
+
'message' => 'hey what is up?'
|
33
|
+
})
|
34
|
+
puts(info)
|
35
|
+
|
36
|
+
# -------
|
37
|
+
# PUBLISH ARRAY OF MESSAGES
|
38
|
+
# -------
|
39
|
+
# Send Message
|
40
|
+
info = pubnub.publish({
|
41
|
+
'channel' => 'hello_world',
|
42
|
+
'message' => { ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] }
|
43
|
+
})
|
44
|
+
puts(info)
|
45
|
+
|
46
|
+
# -------
|
47
|
+
# PUBLISH OBJECT OF STRING MESSAGE
|
48
|
+
# -------
|
49
|
+
# Send Message
|
50
|
+
info = pubnub.publish({
|
51
|
+
'channel' => 'hello_world',
|
52
|
+
'message' => { 'text' => 'some text data' }
|
53
|
+
})
|
54
|
+
puts(info)
|
55
|
+
|
56
|
+
# ---------
|
57
|
+
# SUBSCRIBE
|
58
|
+
# ---------
|
59
|
+
# Listen for Messages *NON-BLOCKING*
|
60
|
+
pubnub.subscribe({
|
61
|
+
'channel' => 'hello_world',
|
62
|
+
'callback' => lambda do |message|
|
63
|
+
puts(message) ## print message
|
64
|
+
return true ## keep listening?
|
65
|
+
end
|
66
|
+
})
|
67
|
+
|
68
|
+
# -------
|
69
|
+
# HISTORY
|
70
|
+
# -------
|
71
|
+
# Load Previously Published Messages
|
72
|
+
messages = pubnub.history({
|
73
|
+
'channel' => 'hello_world',
|
74
|
+
'limit' => 10
|
75
|
+
})
|
76
|
+
puts(messages)
|
77
|
+
|
78
|
+
# -------
|
79
|
+
# UUID
|
80
|
+
# -------
|
81
|
+
# Generate UUID
|
82
|
+
uuid = pubnub.UUID()
|
83
|
+
puts(uuid)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
## -----------------------------------
|
2
|
+
## PubNub Ruby API History Example
|
3
|
+
## -----------------------------------
|
4
|
+
|
5
|
+
## including required libraries
|
6
|
+
require 'rubygems'
|
7
|
+
require 'pubnub_ruby/pubnub'
|
8
|
+
|
9
|
+
## declaring publish_key, subscribe_key, secret_key, cipher_key and ssl flag
|
10
|
+
publish_key = 'demo'
|
11
|
+
subscribe_key = 'demo'
|
12
|
+
secret_key = 'demo'
|
13
|
+
cipher_key = 'demo'
|
14
|
+
ssl_on = false
|
15
|
+
|
16
|
+
## Print usage if missing info.
|
17
|
+
if !subscribe_key
|
18
|
+
puts('
|
19
|
+
Get API Keys at http://www.pubnub.com/account
|
20
|
+
==============
|
21
|
+
EXAMPLE USAGE:
|
22
|
+
==============
|
23
|
+
ruby history-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
|
24
|
+
ruby history-example.rb demo demo true
|
25
|
+
')
|
26
|
+
end
|
27
|
+
|
28
|
+
## Create Pubnub Client API (INITIALIZATION)
|
29
|
+
puts('Initializing new Pubnub state')
|
30
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on)
|
31
|
+
|
32
|
+
## Request Past Publishes (HISTORY)
|
33
|
+
puts('Requesting History with history() Function')
|
34
|
+
message = pubnub.history({
|
35
|
+
'channel' => 'hello_world',
|
36
|
+
'limit' => 5
|
37
|
+
})
|
38
|
+
puts(message)
|
@@ -0,0 +1,60 @@
|
|
1
|
+
## -----------------------------------
|
2
|
+
## PubNub Ruby API Publish Example
|
3
|
+
## -----------------------------------
|
4
|
+
|
5
|
+
## including required libraries
|
6
|
+
require 'rubygems'
|
7
|
+
require 'pubnub_ruby/pubnub'
|
8
|
+
|
9
|
+
## declaring publish_key, subscribe_key, secret_key, cipher_key, channel, ssl flag, messages
|
10
|
+
publish_key = 'demo'
|
11
|
+
subscribe_key = 'demo'
|
12
|
+
secret_key = 'demo'
|
13
|
+
cipher_key = 'demo'
|
14
|
+
ssl_on = false
|
15
|
+
channel = 'hello_world'
|
16
|
+
|
17
|
+
strMessage = "Hi. (顶顅Ȓ)"
|
18
|
+
arrMessage = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
|
19
|
+
objMessage = {"Name"=>"John","Age"=>"23"}
|
20
|
+
|
21
|
+
## Print usage if missing info.
|
22
|
+
if !strMessage && arrMessage && objMessage
|
23
|
+
puts('
|
24
|
+
Get API Keys at http://www.pubnub.com/account
|
25
|
+
==============
|
26
|
+
EXAMPLE USAGE:
|
27
|
+
==============
|
28
|
+
ruby publish-example.rb PUB-KEY SUB-KEY SECRET-KEY "message text" SSL-ON
|
29
|
+
ruby publish-example.rb demo demo "demo" "hey what is up?" true
|
30
|
+
')
|
31
|
+
exit()
|
32
|
+
end
|
33
|
+
|
34
|
+
## Pubnub state initialization (INITIALIZATION)
|
35
|
+
puts('Initializing new Pubnub state')
|
36
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on)
|
37
|
+
|
38
|
+
## Send Message (PUBLISH) -- String
|
39
|
+
puts("\nSending message in String format with publish() Function")
|
40
|
+
info = pubnub.publish({
|
41
|
+
'channel' => channel,
|
42
|
+
'message' => strMessage
|
43
|
+
})
|
44
|
+
puts(info)
|
45
|
+
|
46
|
+
## Send Message (PUBLISH) -- Array
|
47
|
+
puts("\nSending message in Array format with publish() Function")
|
48
|
+
info = pubnub.publish({
|
49
|
+
'channel' => channel,
|
50
|
+
'message' => arrMessage
|
51
|
+
})
|
52
|
+
puts(info)
|
53
|
+
|
54
|
+
## Send Message (PUBLISH) -- Object (Dictionary)
|
55
|
+
puts("\nSending message in Dictionary Object format with publish() Function")
|
56
|
+
info = pubnub.publish({
|
57
|
+
'channel' => channel,
|
58
|
+
'message' => objMessage
|
59
|
+
})
|
60
|
+
puts(info)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
## -----------------------------------
|
2
|
+
## PubNub Ruby API Subscribe Example
|
3
|
+
## -----------------------------------
|
4
|
+
|
5
|
+
## including required libraries
|
6
|
+
require 'rubygems'
|
7
|
+
require 'pubnub_ruby/pubnub'
|
8
|
+
|
9
|
+
## declaring publish_key, subscribe_key, secret_key, cipher_key, ssl flag
|
10
|
+
publish_key = 'demo'
|
11
|
+
subscribe_key = 'demo'
|
12
|
+
secret_key = 'demo'
|
13
|
+
cipher_key = 'demo'
|
14
|
+
ssl_on = false
|
15
|
+
channel = 'hello_world'
|
16
|
+
|
17
|
+
## Print usage if missing info.
|
18
|
+
if !subscribe_key
|
19
|
+
puts('
|
20
|
+
Get API Keys at http://www.pubnub.com/account
|
21
|
+
==============
|
22
|
+
EXAMPLE USAGE:
|
23
|
+
==============
|
24
|
+
ruby subscribe-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
|
25
|
+
ruby subscribe-example.rb demo demo true
|
26
|
+
')
|
27
|
+
exit()
|
28
|
+
end
|
29
|
+
|
30
|
+
## Create Pubnub Client API (INITIALIZATION)
|
31
|
+
puts('Initializing new Pubnub state')
|
32
|
+
pubnub = Pubnub.new(publish_key,subscribe_key,secret_key,cipher_key,ssl_on)
|
33
|
+
|
34
|
+
## Listen for Messages (SUBSCRIBE)
|
35
|
+
puts('Listening for new messages with subscribe() Function')
|
36
|
+
puts('Press CTRL+C to quit.')
|
37
|
+
pubnub.subscribe({
|
38
|
+
'channel' => channel,
|
39
|
+
'callback' => lambda do |message|
|
40
|
+
puts(message) ## print message
|
41
|
+
return true ## keep listening?
|
42
|
+
end
|
43
|
+
})
|
@@ -0,0 +1,15 @@
|
|
1
|
+
## -----------------------------------
|
2
|
+
## PubNub Ruby API UUID Example
|
3
|
+
## -----------------------------------
|
4
|
+
|
5
|
+
## including required libraries
|
6
|
+
require 'rubygems'
|
7
|
+
require 'pubnub_ruby/pubnub'
|
8
|
+
|
9
|
+
## Generating UUID String
|
10
|
+
pubnub=Pubnub.new("","","","",false)
|
11
|
+
|
12
|
+
## calling function for UUID generation
|
13
|
+
puts('Generating UUID String with UUID() Function')
|
14
|
+
uuid=pubnub.UUID()
|
15
|
+
puts('UUID: '+uuid)
|
data/lib/pubnub.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "pubnub_ruby/pubnub"
|
@@ -0,0 +1,297 @@
|
|
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.1 Real-time Push Cloud API
|
10
|
+
## -----------------------------------
|
11
|
+
|
12
|
+
## including required libraries
|
13
|
+
require 'openssl'
|
14
|
+
require 'base64'
|
15
|
+
require 'open-uri'
|
16
|
+
require 'uri'
|
17
|
+
require 'net/http'
|
18
|
+
require 'json'
|
19
|
+
require 'pp'
|
20
|
+
require 'rubygems'
|
21
|
+
require 'securerandom'
|
22
|
+
require 'digest'
|
23
|
+
require 'pubnub_ruby/pubnub_crypto'
|
24
|
+
require 'eventmachine'
|
25
|
+
require 'em-http'
|
26
|
+
require 'fiber'
|
27
|
+
|
28
|
+
class Pubnub
|
29
|
+
MAX_RETRIES = 3
|
30
|
+
retries=0
|
31
|
+
#**
|
32
|
+
#* Pubnub
|
33
|
+
#*
|
34
|
+
#* Init the Pubnub Client API
|
35
|
+
#*
|
36
|
+
#* @param string publish_key required key to send messages.
|
37
|
+
#* @param string subscribe_key required key to receive messages.
|
38
|
+
#* @param string secret_key required key to sign messages.
|
39
|
+
#* @param string cipher_key required to encrypt messages.
|
40
|
+
#* @param boolean ssl required for 2048 bit encrypted messages.
|
41
|
+
#*
|
42
|
+
def initialize( publish_key, subscribe_key, secret_key, cipher_key, ssl_on )
|
43
|
+
@publish_key = publish_key
|
44
|
+
@subscribe_key = subscribe_key
|
45
|
+
@secret_key = secret_key
|
46
|
+
@cipher_key = cipher_key
|
47
|
+
@ssl = ssl_on
|
48
|
+
@origin = 'pubsub.pubnub.com'
|
49
|
+
|
50
|
+
if @ssl
|
51
|
+
@origin = 'https://' + @origin
|
52
|
+
else
|
53
|
+
@origin = 'http://' + @origin
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#**
|
58
|
+
#* Publish
|
59
|
+
#*
|
60
|
+
#* Send a message to a channel.
|
61
|
+
#*
|
62
|
+
#* @param array args with channel and message.
|
63
|
+
#* @return array success information.
|
64
|
+
#*
|
65
|
+
def publish(args)
|
66
|
+
## Fail if bad input.
|
67
|
+
if !(args['channel'] && args['message'])
|
68
|
+
puts('Missing Channel or Message')
|
69
|
+
return false
|
70
|
+
end
|
71
|
+
|
72
|
+
## Capture User Input
|
73
|
+
channel = args['channel']
|
74
|
+
message = args['message']
|
75
|
+
|
76
|
+
#encryption of message
|
77
|
+
if @cipher_key.length > 0
|
78
|
+
pc=PubnubCrypto.new(@cipher_key)
|
79
|
+
if message.is_a? Array
|
80
|
+
message=pc.encryptArray(message)
|
81
|
+
else
|
82
|
+
message=pc.encryptObject(message)
|
83
|
+
end
|
84
|
+
else
|
85
|
+
message = args['message'].to_json();
|
86
|
+
end
|
87
|
+
|
88
|
+
## Sign message using HMAC
|
89
|
+
String signature = '0'
|
90
|
+
if @secret_key.length > 0
|
91
|
+
signature = "{@publish_key,@subscribe_key,@secret_key,channel,message}"
|
92
|
+
digest = OpenSSL::Digest.new("sha256")
|
93
|
+
key = [ @secret_key ]
|
94
|
+
hmac = OpenSSL::HMAC.hexdigest(digest, key.pack("H*"), signature)
|
95
|
+
signature = hmac
|
96
|
+
end
|
97
|
+
|
98
|
+
## Send Message
|
99
|
+
return _request([
|
100
|
+
'publish',
|
101
|
+
@publish_key,
|
102
|
+
@subscribe_key,
|
103
|
+
signature,
|
104
|
+
channel,
|
105
|
+
'0',
|
106
|
+
message
|
107
|
+
])
|
108
|
+
end
|
109
|
+
|
110
|
+
#**
|
111
|
+
#* Subscribe
|
112
|
+
#*
|
113
|
+
#* This is NON-BLOCKING.
|
114
|
+
#* Listen for a message on a channel.
|
115
|
+
#*
|
116
|
+
#* @param array args with channel and message.
|
117
|
+
#* @return false on fail, array on success.
|
118
|
+
#*
|
119
|
+
def subscribe(args)
|
120
|
+
## Capture User Input
|
121
|
+
channel = args['channel']
|
122
|
+
callback = args['callback']
|
123
|
+
|
124
|
+
## Fail if missing channel
|
125
|
+
if !channel
|
126
|
+
puts "Missing Channel."
|
127
|
+
return false
|
128
|
+
end
|
129
|
+
|
130
|
+
## Fail if missing callback
|
131
|
+
if !callback
|
132
|
+
puts "Missing Callback."
|
133
|
+
return false
|
134
|
+
end
|
135
|
+
|
136
|
+
## Begin Subscribe
|
137
|
+
loop do
|
138
|
+
begin
|
139
|
+
timetoken = args['timetoken'] ? args['timetoken'] : 0
|
140
|
+
|
141
|
+
## Wait for Message
|
142
|
+
response = _request([
|
143
|
+
'subscribe',
|
144
|
+
@subscribe_key,
|
145
|
+
channel,
|
146
|
+
'0',
|
147
|
+
timetoken.to_s
|
148
|
+
])
|
149
|
+
|
150
|
+
messages = response[0]
|
151
|
+
args['timetoken'] = response[1]
|
152
|
+
|
153
|
+
## If it was a timeout
|
154
|
+
next if !messages.length
|
155
|
+
|
156
|
+
## Run user Callback and Reconnect if user permits.
|
157
|
+
## Capture the message and encrypt it
|
158
|
+
if @cipher_key.length > 0
|
159
|
+
pc = PubnubCrypto.new(@cipher_key)
|
160
|
+
messages.each do |message|
|
161
|
+
if message.is_a? Array
|
162
|
+
message=pc.decryptArray(message)
|
163
|
+
else
|
164
|
+
message=pc.decryptObject(message)
|
165
|
+
end
|
166
|
+
if !callback.call(message)
|
167
|
+
return
|
168
|
+
end
|
169
|
+
end
|
170
|
+
else
|
171
|
+
messages.each do |message|
|
172
|
+
if !callback.call(message)
|
173
|
+
return
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
rescue Timeout::Error
|
178
|
+
rescue
|
179
|
+
sleep(1)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
#**
|
185
|
+
#* History
|
186
|
+
#*
|
187
|
+
#* Load history from a channel.
|
188
|
+
#*
|
189
|
+
#* @param array args with 'channel' and 'limit'.
|
190
|
+
#* @return mixed false on fail, array on success.
|
191
|
+
#*
|
192
|
+
def history(args)
|
193
|
+
## Capture User Input
|
194
|
+
limit = +args['limit'] ? +args['limit'] : 5
|
195
|
+
channel = args['channel']
|
196
|
+
|
197
|
+
## Fail if bad input.
|
198
|
+
if (!channel)
|
199
|
+
puts 'Missing Channel.'
|
200
|
+
return false
|
201
|
+
end
|
202
|
+
|
203
|
+
## Get History
|
204
|
+
response = _request([ 'history', @subscribe_key, channel, '0', limit.to_s])
|
205
|
+
if @cipher_key.length > 0
|
206
|
+
myarr=Array.new()
|
207
|
+
response.each do |message|
|
208
|
+
pc=PubnubCrypto.new(@cipher_key)
|
209
|
+
if message.is_a? Array
|
210
|
+
message=pc.decryptArray(message)
|
211
|
+
else
|
212
|
+
message=pc.decryptObject(message)
|
213
|
+
end
|
214
|
+
myarr.push(message)
|
215
|
+
end
|
216
|
+
return myarr
|
217
|
+
else
|
218
|
+
return response
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
#**
|
223
|
+
#* Time
|
224
|
+
#*
|
225
|
+
#* Timestamp from PubNub Cloud.
|
226
|
+
#*
|
227
|
+
#* @return int timestamp.
|
228
|
+
#*
|
229
|
+
def time()
|
230
|
+
return _request([
|
231
|
+
'time',
|
232
|
+
'0'
|
233
|
+
])[0]
|
234
|
+
end
|
235
|
+
|
236
|
+
#**
|
237
|
+
#* UUID
|
238
|
+
#*
|
239
|
+
#* Unique identifier generation
|
240
|
+
#*
|
241
|
+
#* @return Unique Identifier
|
242
|
+
#*
|
243
|
+
def UUID()
|
244
|
+
uuid=SecureRandom.base64(32).gsub("/","_").gsub(/=+$/,"")
|
245
|
+
end
|
246
|
+
|
247
|
+
private
|
248
|
+
|
249
|
+
#**
|
250
|
+
#* Request URL
|
251
|
+
#*
|
252
|
+
#* @param array request of url directories.
|
253
|
+
#* @return array from JSON response.
|
254
|
+
#*
|
255
|
+
def _request(request)
|
256
|
+
## Construct Request URL
|
257
|
+
url = '/' + request.map{ |bit| bit.split('').map{ |ch|
|
258
|
+
' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
|
259
|
+
'%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
|
260
|
+
}.join('') }.join('/')
|
261
|
+
|
262
|
+
url = @origin + url
|
263
|
+
http_response = ''
|
264
|
+
|
265
|
+
EventMachine.run do
|
266
|
+
Fiber.new{
|
267
|
+
http = async_fetch(url)
|
268
|
+
http_response = http.response
|
269
|
+
EventMachine.stop
|
270
|
+
}.resume
|
271
|
+
end
|
272
|
+
JSON.parse(http_response)
|
273
|
+
end
|
274
|
+
|
275
|
+
## Non-blocking IO using EventMachine
|
276
|
+
def async_fetch(url)
|
277
|
+
f = Fiber.current
|
278
|
+
|
279
|
+
request_options = {
|
280
|
+
:timeout => 310, # set request timeout
|
281
|
+
:query => {'V' => '3.1', 'User-Agent' => 'Ruby', 'Accept-Encoding' => 'gzip'}, # set request headers
|
282
|
+
}
|
283
|
+
|
284
|
+
http = EventMachine::HttpRequest.new(url).get request_options
|
285
|
+
http.callback { f.resume(http) }
|
286
|
+
http.errback { f.resume(http) }
|
287
|
+
|
288
|
+
Fiber.yield
|
289
|
+
|
290
|
+
if http.error
|
291
|
+
p [:HTTP_ERROR, http.error]
|
292
|
+
end
|
293
|
+
|
294
|
+
http
|
295
|
+
end
|
296
|
+
|
297
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
## Pubnub cryptography
|
2
|
+
|
3
|
+
## including required libraries
|
4
|
+
require "openssl"
|
5
|
+
require 'digest/MD5'
|
6
|
+
require 'base64'
|
7
|
+
require 'rubygems'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class PubnubCrypto
|
11
|
+
#**
|
12
|
+
#* initialize encryption with cipher key, IV and algorithm
|
13
|
+
#*
|
14
|
+
#* @param cipher key (plain text password)
|
15
|
+
#*
|
16
|
+
def initialize(cipher_key)
|
17
|
+
@@alg = "AES-128-CBC"
|
18
|
+
digest = Digest::MD5.new
|
19
|
+
digest.update(cipher_key)
|
20
|
+
@@key = digest.digest
|
21
|
+
@@iv = '0123456789012345'
|
22
|
+
raise 'Key Error' if(@@key.nil? or @@key.size != 16)
|
23
|
+
end
|
24
|
+
|
25
|
+
#**
|
26
|
+
#* encrypt object
|
27
|
+
#*
|
28
|
+
#* @param plain text(message)
|
29
|
+
#* @return cipher text (encrypted text)
|
30
|
+
#*
|
31
|
+
def encryptObject(message)
|
32
|
+
params = Hash.new
|
33
|
+
if message.is_a? String
|
34
|
+
return encrypt(message)
|
35
|
+
else
|
36
|
+
message.each do |key,value|
|
37
|
+
case(key)
|
38
|
+
when(key)
|
39
|
+
params[key] = encrypt(value).chop.reverse.chop.reverse();
|
40
|
+
end
|
41
|
+
end
|
42
|
+
params = params.to_json
|
43
|
+
return params
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#**
|
48
|
+
#* decrypt object
|
49
|
+
#*
|
50
|
+
#* @param cipher object (cipher to decrypt)
|
51
|
+
#* @return plain text (decrypted text)
|
52
|
+
#*
|
53
|
+
def decryptObject(cipher_Object)
|
54
|
+
params = {};
|
55
|
+
if cipher_Object.is_a? String
|
56
|
+
return decrypt(cipher_Object)
|
57
|
+
else
|
58
|
+
cipher_Object.each do |key,value|
|
59
|
+
case(key)
|
60
|
+
when(key)
|
61
|
+
params[key] = decrypt(value);
|
62
|
+
end
|
63
|
+
end
|
64
|
+
return params
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
#**
|
69
|
+
#* encrypt array
|
70
|
+
#*
|
71
|
+
#* @param message to encrypt (array)
|
72
|
+
#* @return cipher text array (encrypted array)
|
73
|
+
#*
|
74
|
+
def encryptArray(message)
|
75
|
+
params = []
|
76
|
+
i=0
|
77
|
+
message.each do |val|
|
78
|
+
case(val)
|
79
|
+
when(val)
|
80
|
+
params[i] = encrypt(val).chop.reverse.chop.reverse();
|
81
|
+
i = i+1
|
82
|
+
end
|
83
|
+
end
|
84
|
+
params = params.to_json
|
85
|
+
return params
|
86
|
+
end
|
87
|
+
|
88
|
+
#**
|
89
|
+
#* decrypt array
|
90
|
+
#*
|
91
|
+
#* @param cipher array (cipher text array to decrypt)
|
92
|
+
#* @return message decrypted (decrypted array)
|
93
|
+
#*
|
94
|
+
def decryptArray(message)
|
95
|
+
params = []
|
96
|
+
i=0
|
97
|
+
message.each do |val|
|
98
|
+
case(val)
|
99
|
+
when(val)
|
100
|
+
params[i] = decrypt(val);
|
101
|
+
i = i+1
|
102
|
+
end
|
103
|
+
end
|
104
|
+
return params
|
105
|
+
end
|
106
|
+
|
107
|
+
#**
|
108
|
+
#* encrypt plain text
|
109
|
+
#*
|
110
|
+
#* @param plain text (string to encrypt)
|
111
|
+
#* @return cipher text (encrypted text)
|
112
|
+
#*
|
113
|
+
def encrypt(message)
|
114
|
+
aes = OpenSSL::Cipher::Cipher.new(@@alg)
|
115
|
+
aes.encrypt
|
116
|
+
aes.key = @@key
|
117
|
+
aes.iv = @@iv
|
118
|
+
@@cipher = aes.update(message)
|
119
|
+
@@cipher << aes.final
|
120
|
+
@@ciphertext = [@@cipher].pack('m')
|
121
|
+
@@ciphertext = @@ciphertext.strip
|
122
|
+
@@ciphertext = @@ciphertext.gsub(/\n/,"")
|
123
|
+
@@ciphertext = '"' + @@ciphertext + '"'
|
124
|
+
return @@ciphertext
|
125
|
+
end
|
126
|
+
|
127
|
+
#**
|
128
|
+
#* decrypt plain text
|
129
|
+
#*
|
130
|
+
#* @param cipher text
|
131
|
+
#* @return plain text (decrypted text)
|
132
|
+
#*
|
133
|
+
def decrypt(cipher_text)
|
134
|
+
decode_cipher = OpenSSL::Cipher::Cipher.new(@@alg)
|
135
|
+
decode_cipher.decrypt
|
136
|
+
decode_cipher.key = @@key
|
137
|
+
decode_cipher.iv = @@iv
|
138
|
+
plain_text = decode_cipher.update(cipher_text.unpack('m')[0])
|
139
|
+
plain_text << decode_cipher.final
|
140
|
+
return plain_text
|
141
|
+
end
|
142
|
+
end
|
data/tests/unit_test.rb
ADDED
@@ -0,0 +1,80 @@
|
|
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.1 Real-time Push Cloud API
|
10
|
+
## -----------------------------------
|
11
|
+
|
12
|
+
## including required libraries
|
13
|
+
require 'rubygems'
|
14
|
+
require 'pubnub_ruby/pubnub'
|
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
|
+
ssl_on = false
|
22
|
+
channel = 'hello_world'
|
23
|
+
message = 'Hi. (顶顅Ȓ)'
|
24
|
+
|
25
|
+
## ---------------------------------------------------------------------------
|
26
|
+
## Create Pubnub Client API (INITIALIZATION)
|
27
|
+
## ---------------------------------------------------------------------------
|
28
|
+
puts('Initializing new Pubnub state')
|
29
|
+
pubnub = Pubnub.new( publish_key, subscribe_key, secret_key,cipher_key, ssl_on )
|
30
|
+
|
31
|
+
## ---------------------------------------------------------------------------
|
32
|
+
## Unit Test Function
|
33
|
+
## ---------------------------------------------------------------------------
|
34
|
+
def test( trial, name )
|
35
|
+
trial ? puts('PASS: ' + name) : puts('FAIL: ' + name)
|
36
|
+
end
|
37
|
+
|
38
|
+
## ---------------------------------------------------------------------------
|
39
|
+
## UUID generation
|
40
|
+
## ---------------------------------------------------------------------------
|
41
|
+
uuid=pubnub.UUID()
|
42
|
+
test( uuid.length > 0, 'PubNub Client API UUID: ' + uuid )
|
43
|
+
|
44
|
+
## ---------------------------------------------------------------------------
|
45
|
+
## PubNub Server Time
|
46
|
+
## ---------------------------------------------------------------------------
|
47
|
+
timestamp = pubnub.time()
|
48
|
+
test( timestamp > 0, 'PubNub Server Time: ' + timestamp.to_s )
|
49
|
+
|
50
|
+
## ---------------------------------------------------------------------------
|
51
|
+
## PUBLISH
|
52
|
+
## ---------------------------------------------------------------------------
|
53
|
+
pubish_success = pubnub.publish({
|
54
|
+
'channel' => channel,
|
55
|
+
'message' => message
|
56
|
+
})
|
57
|
+
test( pubish_success[0] == 1, 'Publish First Message Success' )
|
58
|
+
|
59
|
+
## ---------------------------------------------------------------------------
|
60
|
+
## HISTORY
|
61
|
+
## ---------------------------------------------------------------------------
|
62
|
+
history = pubnub.history({
|
63
|
+
'channel' => channel,
|
64
|
+
'limit' => 5
|
65
|
+
})
|
66
|
+
puts(history)
|
67
|
+
test( history.length >= 1, 'Display History' )
|
68
|
+
|
69
|
+
## ---------------------------------------------------------------------------
|
70
|
+
## Subscribe
|
71
|
+
## ---------------------------------------------------------------------------
|
72
|
+
puts('Listening for new messages with subscribe() Function')
|
73
|
+
puts('Press CTRL+C to quit.')
|
74
|
+
pubnub.subscribe({
|
75
|
+
'channel' => channel,
|
76
|
+
'callback' => lambda do |message|
|
77
|
+
puts(message) ## print message
|
78
|
+
return true ## keep listening?
|
79
|
+
end
|
80
|
+
})
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pubnub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Luke Carpenter / PubNub.com
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-28 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: eventmachine
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta
|
32
|
+
- 4
|
33
|
+
- 1
|
34
|
+
version: 1.0.0.beta.4.1
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: em-http-request
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 2
|
48
|
+
version: 1.0.2
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: json
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
description: Simply Pubnub.rb in gem format, :require => "pubnub" - ask @PubNub for upgrades.
|
64
|
+
email: stephen@pubnub.com
|
65
|
+
executables: []
|
66
|
+
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files:
|
70
|
+
- README
|
71
|
+
files:
|
72
|
+
- examples/history_example.rb
|
73
|
+
- examples/publish_example.rb
|
74
|
+
- examples/subscribe_example.rb
|
75
|
+
- examples/uuid_example.rb
|
76
|
+
- lib/pubnub.rb
|
77
|
+
- lib/pubnub/pubnub.rb
|
78
|
+
- lib/pubnub/pubnub_crypto.rb
|
79
|
+
- tests/unit_test.rb
|
80
|
+
- README
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: http://github.com/pubnub/pubnub-api
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.3.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: PubNub Official GEM
|
111
|
+
test_files: []
|
112
|
+
|