pubnub-ruby 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,59 @@
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 - RUBY
10
+ ## ------------------------------------------
11
+ ##
12
+ ## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
13
+ ## http://www.pubnub.com/blog/ruby-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
+ ## -------------
20
+ ## Ruby Push API
21
+ ## -------------
22
+ pubnub = Pubnub.new(
23
+ "demo", ## PUBLISH_KEY
24
+ "demo", ## SUBSCRIBE_KEY
25
+ "", ## SECRET_KEY
26
+ false ## SSL_ON?
27
+ )
28
+
29
+ # -------
30
+ # PUBLISH
31
+ # -------
32
+ # Send Message
33
+ info = pubnub.publish({
34
+ 'channel' => 'hello_world',
35
+ 'message' => { 'text' => 'some text data' }
36
+ })
37
+ puts(info)
38
+
39
+ # ---------
40
+ # SUBSCRIBE
41
+ # ---------
42
+ # Listen for Messages *BLOCKING*
43
+ pubnub.subscribe({
44
+ 'channel' => 'hello_world',
45
+ 'callback' => lambda do |message|
46
+ puts(message) ## print message
47
+ return true ## keep listening?
48
+ end
49
+ })
50
+
51
+ # -------
52
+ # HISTORY
53
+ # -------
54
+ # Load Previously Published Messages
55
+ messages = pubnub.history({
56
+ 'channel' => 'hello_world',
57
+ 'limit' => 10
58
+ })
59
+ puts(messages)
@@ -0,0 +1,36 @@
1
+ require '../lib/pubnub'
2
+
3
+ publish_key = ARGV[0]
4
+ subscribe_key = ARGV[1]
5
+ ssl_on = !!ARGV[2]
6
+
7
+ ## Print usage if missing info.
8
+ if !subscribe_key
9
+ puts('
10
+ Get API Keys at http://www.pubnub.com/account
11
+ ==============
12
+ EXAMPLE USAGE:
13
+ ==============
14
+ ruby history-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
15
+ ruby history-example.rb demo demo true
16
+
17
+ ')
18
+ exit()
19
+ end
20
+
21
+ ## -----------------------------------------
22
+ ## Create Pubnub Client API (INITIALIZATION)
23
+ ## -----------------------------------------
24
+ puts('Creating new Pubnub Client API')
25
+ pubnub = Pubnub.new( publish_key, subscribe_key, nil, ssl_on )
26
+
27
+ ## --------------------------------
28
+ ## Request Past Publishes (HISTORY)
29
+ ## --------------------------------
30
+ puts('Requesting History with history() Function')
31
+ messages = pubnub.history({
32
+ 'channel' => 'hello_world',
33
+ 'limit' => 10
34
+ })
35
+
36
+ puts(messages)
@@ -0,0 +1,39 @@
1
+ require '../lib/pubnub'
2
+
3
+ publish_key = ARGV[0]
4
+ subscribe_key = ARGV[1]
5
+ secret_key = ARGV[2]
6
+ message = ARGV[3]
7
+ ssl_on = !!ARGV[4]
8
+
9
+ ## Print usage if missing info.
10
+ if !message
11
+ puts('
12
+ Get API Keys at http://www.pubnub.com/account
13
+ ==============
14
+ EXAMPLE USAGE:
15
+ ==============
16
+ ruby publish-example.rb PUB-KEY SUB-KEY SECRET-KEY "message text" SSL-ON
17
+ ruby publish-example.rb demo demo "" "hey what is up?" true
18
+
19
+ ')
20
+ exit()
21
+ end
22
+
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, ssl_on )
28
+
29
+ ## ----------------------
30
+ ## Send Message (PUBLISH)
31
+ ## ----------------------
32
+ puts('Sending a message with publish() Function')
33
+ info = pubnub.publish({
34
+ 'channel' => 'hello_world',
35
+ 'message' => message
36
+ })
37
+
38
+ ## Print Pretty
39
+ puts(info)
@@ -0,0 +1,38 @@
1
+ require '../lib/pubnub'
2
+
3
+ publish_key = ARGV[0]
4
+ subscribe_key = ARGV[1]
5
+ ssl_on = !!ARGV[2]
6
+
7
+ ## Print usage if missing info.
8
+ if !subscribe_key
9
+ puts('
10
+ Get API Keys at http://www.pubnub.com/account
11
+ ==============
12
+ EXAMPLE USAGE:
13
+ ==============
14
+ ruby subscribe-example.rb PUBLISH-KEY SUBSCRIBE-KEY SSL-ON
15
+ ruby subscribe-example.rb demo demo true
16
+
17
+ ')
18
+ exit()
19
+ end
20
+
21
+ ## -----------------------------------------
22
+ ## Create Pubnub Client API (INITIALIZATION)
23
+ ## -----------------------------------------
24
+ puts('Creating new Pubnub Client API')
25
+ pubnub = Pubnub.new( publish_key, subscribe_key, nil, ssl_on )
26
+
27
+ ## -------------------------------
28
+ ## Listen for Messages (SUBSCRIBE)
29
+ ## -------------------------------
30
+ puts('Listening for new messages with subscribe() Function')
31
+ puts('Press CTRL+C to quit.')
32
+ pubnub.subscribe({
33
+ 'channel' => 'hello_world',
34
+ 'callback' => lambda do |message|
35
+ puts(message) ## print message
36
+ return true ## keep listening?
37
+ end
38
+ })
@@ -0,0 +1,217 @@
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
+ require 'digest/md5'
13
+ require 'open-uri'
14
+ require 'uri'
15
+ require 'json'
16
+ require 'pp'
17
+
18
+ class Pubnub
19
+ #**
20
+ #* Pubnub
21
+ #*
22
+ #* Init the Pubnub Client API
23
+ #*
24
+ #* @param string publish_key required key to send messages.
25
+ #* @param string subscribe_key required key to receive messages.
26
+ #* @param string secret_key required key to sign messages.
27
+ #* @param boolean ssl required for 2048 bit encrypted messages.
28
+ #*
29
+ def initialize( publish_key, subscribe_key, secret_key, ssl_on )
30
+ @publish_key = publish_key
31
+ @subscribe_key = subscribe_key
32
+ @secret_key = secret_key
33
+ @ssl = ssl_on
34
+ @origin = 'pubsub.pubnub.com'
35
+ @limit = 1800
36
+
37
+ if @ssl
38
+ @origin = 'https://' + @origin
39
+ else
40
+ @origin = 'http://' + @origin
41
+ end
42
+ end
43
+
44
+ #**
45
+ #* Publish
46
+ #*
47
+ #* Send a message to a channel.
48
+ #*
49
+ #* @param array args with channel and message.
50
+ #* @return array success information.
51
+ #*
52
+ def publish(args)
53
+ ## Fail if bad input.
54
+ if !(args['channel'] && args['message'])
55
+ puts('Missing Channel or Message')
56
+ return false
57
+ end
58
+
59
+ ## Capture User Input
60
+ channel = args['channel']
61
+ message = args['message'].to_json
62
+
63
+ ## Sign Message
64
+ signature = @secret_key.length > 0 ? Digest::MD5.hexdigest([
65
+ @publish_key,
66
+ @subscribe_key,
67
+ @secret_key,
68
+ channel,
69
+ message
70
+ ].join('/')) : '0'
71
+
72
+ ## Fail if message too long.
73
+ if message.length > @limit
74
+ puts('Message TOO LONG (' + @limit.to_s + ' LIMIT)')
75
+ return [ 0, 'Message Too Long.' ]
76
+ end
77
+
78
+ ## Send Message
79
+ return self._request([
80
+ 'publish',
81
+ @publish_key,
82
+ @subscribe_key,
83
+ signature,
84
+ channel,
85
+ '0',
86
+ message
87
+ ])
88
+ end
89
+
90
+ #**
91
+ #* Subscribe
92
+ #*
93
+ #* This is BLOCKING.
94
+ #* Listen for a message on a channel.
95
+ #*
96
+ #* @param array args with channel and message.
97
+ #* @return false on fail, array on success.
98
+ #*
99
+ def subscribe(args)
100
+ ## Capture User Input
101
+ channel = args['channel']
102
+ callback = args['callback']
103
+ timetoken = args['timetoken'] ? args['timetoken'] : 0
104
+
105
+ ## Fail if missing channel
106
+ if !channel
107
+ echo("Missing Channel.\n")
108
+ return false
109
+ end
110
+
111
+ ## Fail if missing callback
112
+ if !callback
113
+ echo("Missing Callback.\n")
114
+ return false
115
+ end
116
+
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
140
+ end
141
+ 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
+ end
151
+ end
152
+
153
+ #**
154
+ #* History
155
+ #*
156
+ #* Load history from a channel.
157
+ #*
158
+ #* @param array args with 'channel' and 'limit'.
159
+ #* @return mixed false on fail, array on success.
160
+ #*
161
+ def history(args)
162
+ ## Capture User Input
163
+ limit = +args['limit'] ? +args['limit'] : 10
164
+ channel = args['channel']
165
+
166
+ ## Fail if bad input.
167
+ if (!channel)
168
+ echo('Missing Channel')
169
+ return false
170
+ end
171
+
172
+ ## Get History
173
+ return self._request([
174
+ 'history',
175
+ @subscribe_key,
176
+ channel,
177
+ '0',
178
+ limit.to_s
179
+ ]);
180
+ end
181
+
182
+ #**
183
+ #* Time
184
+ #*
185
+ #* Timestamp from PubNub Cloud.
186
+ #*
187
+ #* @return int timestamp.
188
+ #*
189
+ def time()
190
+ return self._request([
191
+ 'time',
192
+ '0'
193
+ ])[0]
194
+ end
195
+
196
+ #**
197
+ #* Request URL
198
+ #*
199
+ #* @param array request of url directories.
200
+ #* @return array from JSON response.
201
+ #*
202
+ def _request(request)
203
+ ## Construct Request URL
204
+ url = @origin + '/' + request.map{ |bit| bit.split('').map{ |ch|
205
+ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
206
+ '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
207
+ }.join('') }.join('/')
208
+
209
+ response = ''
210
+ open(url) do |f|
211
+ response = f.read
212
+ end
213
+
214
+ return JSON.parse(response)
215
+ end
216
+ end
217
+
@@ -0,0 +1,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
+ require 'Pubnub'
12
+
13
+ publish_key = ARGV[0] || 'demo'
14
+ subscribe_key = ARGV[1] || 'demo'
15
+ secret_key = ARGV[2] || ''
16
+ ssl_on = !!ARGV[3]
17
+
18
+
19
+ ## ---------------------------------------------------------------------------
20
+ ## Create Pubnub Client API (INITIALIZATION)
21
+ ## ---------------------------------------------------------------------------
22
+ pubnub = Pubnub.new( publish_key, subscribe_key, secret_key, ssl_on )
23
+ channel = 'ruby-unit-test-' + rand().to_s
24
+
25
+
26
+ ## ---------------------------------------------------------------------------
27
+ ## Unit Test Function
28
+ ## ---------------------------------------------------------------------------
29
+ def test( trial, name )
30
+ trial ? puts('PASS: ' + name) : puts('FAIL: ' + name)
31
+ end
32
+
33
+
34
+ ## ---------------------------------------------------------------------------
35
+ ## PubNub Server Time
36
+ ## ---------------------------------------------------------------------------
37
+ timestamp = pubnub.time()
38
+ test( timestamp > 0, 'PubNub Server Time: ' + timestamp.to_s )
39
+
40
+
41
+ ## ---------------------------------------------------------------------------
42
+ ## PUBLISH
43
+ ## ---------------------------------------------------------------------------
44
+ first_message = 'Hi. (顶顅Ȓ)'
45
+ pubish_success = pubnub.publish({
46
+ 'channel' => channel,
47
+ 'message' => first_message
48
+ })
49
+ test( pubish_success[0] == 1, 'Publish First Message Success' )
50
+
51
+ crazy_channel = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':",./<>?abcd'
52
+ pubish_success = pubnub.publish({
53
+ 'channel' => crazy_channel,
54
+ 'message' => crazy_channel
55
+ })
56
+ test( pubish_success[0] == 1, 'Publish First Message Success' )
57
+
58
+
59
+ ## ---------------------------------------------------------------------------
60
+ ## Request Past Publishes (HISTORY)
61
+ ## ---------------------------------------------------------------------------
62
+ history = pubnub.history({
63
+ 'channel' => crazy_channel,
64
+ 'limit' => 1
65
+ })
66
+ test( history.length == 1, 'History Length 2' )
67
+ test( history[0] == crazy_channel, 'History Message UTF8: ' + history[0] )
68
+
69
+
70
+ ## ---------------------------------------------------------------------------
71
+ ## Subscribe
72
+ ## ---------------------------------------------------------------------------
73
+ pubnub.subscribe({
74
+ 'channel' => crazy_channel,
75
+ 'callback' => lambda do |message|
76
+ puts(message) ## print message
77
+ return true ## keep listening?
78
+ end
79
+ })
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pubnub-ruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Luke Carpenter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-28 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: Simply Pubnub.rb in gem format - ask @lukella for upgrades
27
+ email: orlyowlizere@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - README
34
+ files:
35
+ - examples/history-example.rb
36
+ - examples/publish-example.rb
37
+ - examples/subscribe-example.rb
38
+ - lib/pubnub.rb
39
+ - tests/unit-test.rb
40
+ - README
41
+ homepage: http://github.com/rubynerd/pubnub
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.7.2
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: PubNub unofficial gem
68
+ test_files: []
69
+