pubnub 3.3.0.7 → 3.4

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.

Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.yardoc/checksums +13 -0
  4. data/.yardoc/object_types +0 -0
  5. data/.yardoc/objects/root.dat +0 -0
  6. data/.yardoc/proxy_types +0 -0
  7. data/LICENSE +27 -0
  8. data/README.md +259 -0
  9. data/Rakefile +7 -0
  10. data/VERSION +1 -0
  11. data/examples/demo_console.rb +198 -0
  12. data/examples/error_server.rb +28 -0
  13. data/examples/pubnub_livestream/.gitignore +16 -0
  14. data/examples/pubnub_livestream/Gemfile +51 -0
  15. data/examples/pubnub_livestream/Gemfile.lock +191 -0
  16. data/examples/pubnub_livestream/README.rdoc +28 -0
  17. data/examples/pubnub_livestream/Rakefile +6 -0
  18. data/examples/pubnub_livestream/app/assets/images/.keep +0 -0
  19. data/examples/pubnub_livestream/app/assets/javascripts/application.js +16 -0
  20. data/examples/pubnub_livestream/app/assets/javascripts/streamer.js.coffee +42 -0
  21. data/examples/pubnub_livestream/app/assets/stylesheets/application.css.sass +22 -0
  22. data/examples/pubnub_livestream/app/assets/stylesheets/streamer.css.scss +3 -0
  23. data/examples/pubnub_livestream/app/controllers/application_controller.rb +5 -0
  24. data/examples/pubnub_livestream/app/controllers/concerns/.keep +0 -0
  25. data/examples/pubnub_livestream/app/controllers/streamer_controller.rb +38 -0
  26. data/examples/pubnub_livestream/app/helpers/application_helper.rb +2 -0
  27. data/examples/pubnub_livestream/app/helpers/streamer_helper.rb +2 -0
  28. data/examples/pubnub_livestream/app/mailers/.keep +0 -0
  29. data/examples/pubnub_livestream/app/models/.keep +0 -0
  30. data/examples/pubnub_livestream/app/models/concerns/.keep +0 -0
  31. data/examples/pubnub_livestream/app/models/message.rb +11 -0
  32. data/examples/pubnub_livestream/app/views/layouts/application.html.erb +14 -0
  33. data/examples/pubnub_livestream/app/views/streamer/index.haml +22 -0
  34. data/examples/pubnub_livestream/bin/bundle +3 -0
  35. data/examples/pubnub_livestream/bin/rails +4 -0
  36. data/examples/pubnub_livestream/bin/rake +4 -0
  37. data/examples/pubnub_livestream/config.ru +4 -0
  38. data/examples/pubnub_livestream/config/application.rb +23 -0
  39. data/examples/pubnub_livestream/config/boot.rb +4 -0
  40. data/examples/pubnub_livestream/config/database.yml +25 -0
  41. data/examples/pubnub_livestream/config/environment.rb +5 -0
  42. data/examples/pubnub_livestream/config/environments/development.rb +29 -0
  43. data/examples/pubnub_livestream/config/environments/production.rb +80 -0
  44. data/examples/pubnub_livestream/config/environments/test.rb +36 -0
  45. data/examples/pubnub_livestream/config/initializers/backtrace_silencers.rb +7 -0
  46. data/examples/pubnub_livestream/config/initializers/filter_parameter_logging.rb +4 -0
  47. data/examples/pubnub_livestream/config/initializers/inflections.rb +16 -0
  48. data/examples/pubnub_livestream/config/initializers/mime_types.rb +5 -0
  49. data/examples/pubnub_livestream/config/initializers/pubnub.rb +12 -0
  50. data/examples/pubnub_livestream/config/initializers/secret_token.rb +12 -0
  51. data/examples/pubnub_livestream/config/initializers/session_store.rb +3 -0
  52. data/examples/pubnub_livestream/config/initializers/wrap_parameters.rb +14 -0
  53. data/examples/pubnub_livestream/config/locales/en.yml +23 -0
  54. data/examples/pubnub_livestream/config/routes.rb +62 -0
  55. data/examples/pubnub_livestream/db/migrate/20130826110322_create_messages.rb +11 -0
  56. data/examples/pubnub_livestream/db/schema.rb +24 -0
  57. data/examples/pubnub_livestream/db/seeds.rb +7 -0
  58. data/examples/pubnub_livestream/lib/assets/.keep +0 -0
  59. data/examples/pubnub_livestream/lib/tasks/.keep +0 -0
  60. data/examples/pubnub_livestream/log/.keep +0 -0
  61. data/examples/pubnub_livestream/public/404.html +58 -0
  62. data/examples/pubnub_livestream/public/422.html +58 -0
  63. data/examples/pubnub_livestream/public/500.html +57 -0
  64. data/examples/pubnub_livestream/public/assets/application-22a604196dfb65fd0d602eb1eb65f9b7.js +4 -0
  65. data/examples/pubnub_livestream/public/assets/application-22a604196dfb65fd0d602eb1eb65f9b7.js.gz +0 -0
  66. data/examples/pubnub_livestream/public/assets/application-3fac0c014bbdf9ee7b3986ff615d5da0.css +5019 -0
  67. data/examples/pubnub_livestream/public/assets/application-3fac0c014bbdf9ee7b3986ff615d5da0.css.gz +0 -0
  68. data/examples/pubnub_livestream/public/assets/application-f06834e402639ad43230e3859b9bdd78.css +1 -0
  69. data/examples/pubnub_livestream/public/assets/application-f06834e402639ad43230e3859b9bdd78.css.gz +0 -0
  70. data/examples/pubnub_livestream/public/assets/application-f91b87f490140d86003c46b4d06b6c70.js +10682 -0
  71. data/examples/pubnub_livestream/public/assets/application-f91b87f490140d86003c46b4d06b6c70.js.gz +0 -0
  72. data/examples/pubnub_livestream/public/assets/manifest-c129e1f5ec52d8b661ebfa902554a2e2.json +1 -0
  73. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-0bc0341283e3bb8ec518375794cc7c28.eot +0 -0
  74. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-24dfb40c91db789b8b8faba6886ac1ef.svg +228 -0
  75. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-4b2130768da98222338d1519f9179528.ttf +0 -0
  76. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-7a07f26f72466361ac9671de2d33fd1c.woff +0 -0
  77. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-9f75212cf9fca594cee7e0e3587db9d1.svg +228 -0
  78. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-ab2f6984951c07fd89e6afdefabd93c7.eot +0 -0
  79. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-c21928f7d46b397b0af6b9ee4a7bd0dd.ttf +0 -0
  80. data/examples/pubnub_livestream/public/assets/twitter/glyphicons-halflings-regular-fa1d7f79d80d03f8a598822bd9df79bf.woff +0 -0
  81. data/examples/pubnub_livestream/public/favicon.ico +0 -0
  82. data/examples/pubnub_livestream/public/robots.txt +5 -0
  83. data/examples/pubnub_livestream/test/controllers/.keep +0 -0
  84. data/examples/pubnub_livestream/test/controllers/streamer_controller_test.rb +7 -0
  85. data/examples/pubnub_livestream/test/fixtures/.keep +0 -0
  86. data/examples/pubnub_livestream/test/fixtures/messages.yml +9 -0
  87. data/examples/pubnub_livestream/test/helpers/.keep +0 -0
  88. data/examples/pubnub_livestream/test/helpers/streamer_helper_test.rb +4 -0
  89. data/examples/pubnub_livestream/test/integration/.keep +0 -0
  90. data/examples/pubnub_livestream/test/mailers/.keep +0 -0
  91. data/examples/pubnub_livestream/test/models/.keep +0 -0
  92. data/examples/pubnub_livestream/test/models/message_test.rb +7 -0
  93. data/examples/pubnub_livestream/test/test_helper.rb +15 -0
  94. data/examples/pubnub_livestream/vendor/assets/javascripts/.keep +0 -0
  95. data/examples/pubnub_livestream/vendor/assets/stylesheets/.keep +0 -0
  96. data/examples/serial_publish.rb +46 -0
  97. data/examples/sinatra/.sass-cache/65d837cc121fc62381bb76d93e5bd081356aa3f9/application.sassc +0 -0
  98. data/examples/sinatra/.sass-cache/d1525a8542f6e7fb2ecd3275251283768779b344/main.rbc +0 -0
  99. data/examples/sinatra/.sass-cache/d35765d68c1df11fa3368aa802b3d38109cba214/application.sassc +0 -0
  100. data/examples/sinatra/main.rb +54 -0
  101. data/examples/sinatra/public/bootstrap-responsive.min.css +9 -0
  102. data/examples/sinatra/public/bootstrap.css +5909 -0
  103. data/examples/sinatra/public/bootstrap.min.css +845 -0
  104. data/examples/sinatra/public/jquery-1.10.2.min.js +5 -0
  105. data/examples/sinatra/views/application.sass +6 -0
  106. data/examples/sinatra/views/index.slim +16 -0
  107. data/examples/sinatra/views/layout.slim +12 -0
  108. data/examples/sinatra/views/streamer.coffee +41 -0
  109. data/examples/sub_and_unsub_1.rb +56 -0
  110. data/examples/translator.rb +129 -0
  111. data/lib/pubnub.rb +31 -375
  112. data/lib/pubnub/client.rb +527 -0
  113. data/lib/pubnub/configuration.rb +25 -0
  114. data/lib/pubnub/crypto.rb +53 -0
  115. data/lib/pubnub/error.rb +23 -0
  116. data/lib/pubnub/request.rb +288 -0
  117. data/lib/pubnub/response.rb +126 -0
  118. data/lib/pubnub/subscription.rb +24 -0
  119. data/lib/tasks/examples.rake +39 -0
  120. data/lib/version.rb +1 -0
  121. data/pubnub.gemspec +26 -0
  122. data/spec/lib/client_spec.rb +346 -0
  123. data/spec/lib/crypto_spec.rb +89 -0
  124. data/spec/lib/history_integration_spec.rb +0 -0
  125. data/spec/lib/presence_integration_spec.rb +16 -0
  126. data/spec/lib/publish_integration_spec.rb +994 -0
  127. data/spec/lib/pubnub_spec.rb +12 -0
  128. data/spec/lib/request_spec.rb +151 -0
  129. data/spec/lib/subscribe_integration_spec.rb +944 -0
  130. data/spec/lib/time_integration_spec.rb +0 -0
  131. data/spec/spec_helper.rb +15 -0
  132. metadata +158 -45
  133. data/lib/pubnub_crypto.rb +0 -53
  134. data/lib/pubnub_request.rb +0 -310
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 221358338157dbb82bbda8b746e0a48029a27d3a
4
+ data.tar.gz: 99d0e9f83f0b2c0a503fa03fa6e34b7571d0fef1
5
+ SHA512:
6
+ metadata.gz: 0b8a1ca3bbbbb9da4a2cd307e6b28627153be01180f4933204d982e72cdbee11017c1ba57d78ad941c6152f1aac0eed32152fb7929bead852e0a33fe0c73c2ae
7
+ data.tar.gz: 6abee24d18724fc5d4aa50fa3ae614e2db757ea2015f8d207bfa7f723cd46ed2d0c06b37aa8a64447cf892abc272e65d9cbf2fb2dac2c6551343471f9102766d
@@ -0,0 +1,7 @@
1
+ .DS_Store
2
+ pubnub-*.gem
3
+ .ruby-version
4
+ *.iml
5
+ .idea/
6
+ log/*
7
+ *.log
@@ -0,0 +1,13 @@
1
+ lib/pubnub/client.rb 24d210ab228a4803805472666581d982de7e7460
2
+ lib/pubnub/configuration.rb 16cd59eb2bba2d60b411817fc12807e37a55342f
3
+ lib/pubnub/crypto.rb 5795f15e8380ab4e4df7b66002ba27a0ef6773ca
4
+ lib/pubnub/daemon.rb 887fe6589269f6244ac45542d278107a143cc04b
5
+ lib/pubnub/em/client.rb 8132d65f67e1128dc94c2cd7a81fcb2672efb367
6
+ lib/pubnub/em/connection.rb 0e07699d0469d8f072d9b93a711173e169bf6973
7
+ lib/pubnub/em/request.rb 6e95650a088ac04a419d18235cfe84a872803bff
8
+ lib/pubnub/em/response.rb e750b47b62843dc39bfdc006b60d6552143b3b8a
9
+ lib/pubnub/error.rb dad9a587bdeb0d53dc217a4f3e156bc6787106c1
10
+ lib/pubnub.rb eab5c5b85a1760514e2d3a7d200f2b5f53690d09
11
+ lib/pubnub/request.rb affdaa20d405404058de3f0ae19bbf2ef222a172
12
+ lib/pubnub/response.rb 47a9a785572eae36391ec7da9f214efa8f826876
13
+ lib/version.rb 47184c709077ffbfe1336763c37cfd9cbc62cd77
Binary file
Binary file
Binary file
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
2
+ Copyright (c) 2013 PubNub 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) 2013 PubNub Inc.
26
+ http://www.pubnub.com/
27
+ http://www.pubnub.com/terms
@@ -0,0 +1,259 @@
1
+ ##### YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
2
+ ##### http://www.pubnub.com/account
3
+
4
+ ## PubNub Gem version 3.4
5
+
6
+ www.pubnub.com - PubNub Real-time Push Service in the Cloud.
7
+
8
+ PubNub is a Real-time Network for Mobile App, Web Apps for pushing updates and enabling real-time notifications and even games!
9
+
10
+ ### Upgrading from PubNub 3.3.x and Earlier
11
+ PubNub 3.4 is NOT compatible with earlier versions of Pubnub Ruby Client. If you are upgrading from 3.3.x, the changes you will need to make are minimal.
12
+
13
+ ### New Features of 3.4
14
+ There are a lot of cool features introduced in the 3.4 version of the gem. Notably:
15
+
16
+ * Ability to carry out requests asynchronously or synchronously
17
+ * Message handling via callback, block, and return
18
+ * Heroku and JRuby Support (JRuby requires calls to be made synchronously)
19
+
20
+ #### Asynchronous vs Synchronous Responses
21
+ Feedback from prior versions of the gem demonstrated that some users would like to 'fire and forget' PubNub calls. Others wanted to block, and take specific action, based on server response.
22
+
23
+ In 3.4, you can now have both, even on a per-call basis!
24
+
25
+ #### Message Handling: callback, block, return
26
+ Feedback from users of prior versions demonstrated the need for flexibility in how received messages were handled. The general PubNub response pattern dictated that responses be sent to callbacks, however, there was also a non-addressed use case of simply assigning a variable on return.
27
+
28
+ In 3.4, you can now have both, even on a per-call basis!
29
+
30
+ ### Code Examples
31
+
32
+ #### Require
33
+ ```ruby
34
+ require 'pubnub'
35
+ ```
36
+
37
+ #### Init and instantiate a new PubNub instance
38
+
39
+ ```ruby
40
+ pubnub = Pubnub.new(
41
+ :subscribe_key => 'demo',
42
+ :publish_key => 'demo',
43
+ :origin => origin,
44
+ :error_callback => lambda { |msg|
45
+ puts "SOMETHING TERRIBLE HAPPENED HERE: #{msg.inspect}"
46
+ },
47
+ :connect_callback => lambda { |msg|
48
+ puts "CONNECTED: #{msg.inspect}"
49
+ }
50
+ )
51
+ ```
52
+
53
+ * subscribe_key is your subscribe key
54
+ * publish_key is your publish key
55
+ * origin is your custom, PubNub origin (Contact support before production to get your own!)
56
+ * error_callback is the callback for errors
57
+ * connect_callback is the callback that lets you know when you're connected to the origin
58
+
59
+ #### Making PubNub calls
60
+ There are a few different ways to make any given PubNub call. How to do it depends first on whether or not you want the call to be blocking (synchronous), or not blocking (asynchronous).
61
+
62
+ ##### Asynchronous (non-blocking) calling
63
+
64
+ If you wish to make asyncronous calls (implemented via EM), you have a few different patterns you can follow:
65
+
66
+ ```ruby
67
+ # Lets use a callback for the first example...
68
+ cb = lambda { |envelope| puts envelope.message }
69
+
70
+ # Asynchronous is implicitly enabled by default, if you do not provide an :http_sync option
71
+ pubnub.publish(:message => msg, :channel => channel, :callback => cb)
72
+
73
+ # You can also explicitly request async with :http_sync => false
74
+ pubnub.publish(:message => msg, :channel => channel, :callback => cb, :http_sync => true)
75
+
76
+ # Alternatively, you can pass in the callback as a block
77
+
78
+ pubnub.publish(:message => msg, :channel => channel, &cb)
79
+
80
+ pubnub.publish(:message => msg, :channel => channel) do |envelope|
81
+ puts envelope.message
82
+ puts envelope.channel
83
+ puts envelope.status_code
84
+ puts envelope.timetoken
85
+ end
86
+ ```
87
+
88
+ ##### Synchronous (blocking) calling
89
+ Synchronous calling is required when using PubNub with JRuby.
90
+
91
+ If you'd prefer to make your calls blocking (implemented via HTTParty), set :http_sync => true. Again, there is a bit of flexibility in how this can be done:
92
+
93
+ ```ruby
94
+
95
+ # Lets use a callback for the first example...
96
+ cb = lambda { |envelope| puts envelope.message }
97
+
98
+ # Sync (blocking) with a callback (if you wanted to)
99
+ pubnub.publish(:http_sync => true, :message => msg, :channel => channel, &cb)
100
+
101
+ # Sync (blocking), with assignment via return
102
+ myResponse = pubnub.publish(:http_sync => true, :message => msg, :channel => channel)
103
+ puts "myR: #{myResponse}"
104
+
105
+ # Sync (blocking), with a block
106
+ pubnub.publish(:http_sync => true, :message => msg, :channel => channel) do |envelope|
107
+ puts envelope.message
108
+ puts envelope.channel
109
+ puts envelope.status_code
110
+ puts envelope.timetoken
111
+ end
112
+ ```
113
+
114
+ #### Callback / Block calling sequence
115
+
116
+ When you receive messages asynchronously from PubNub, your block or callback will be called once for each message received. For example, if you are subscribed to a channel using the callback pattern, and you receive 3 messages from your call, the callback will be called 3 times, 1 time for each unique received message.
117
+
118
+ Conceptually, the callback or block is fired once for each message in the raw server response:
119
+
120
+ ```
121
+ foreach (message in response)
122
+ callbackOrBlock(message)
123
+ ```
124
+
125
+
126
+ #### The Envelope Object and Pubnub::Response
127
+ The callback (or block) will receive the message(s) in the form of an envelope hash. An envelope will contain the following keys:
128
+
129
+ * message (aliased as 'msg') -> Holds message
130
+ * channel -> Holds channel for current message
131
+ * timetoken -> Timetoken of server response
132
+ * status_code -> Server response status code
133
+ * response -> Whole and unmodified server response
134
+
135
+ Don't confuse the **message** with the **response**. In a given callback cycle, the **response** will always be the same, as its the raw server response. It may consist of one or more messages.
136
+
137
+ Internally, the block or callback is iterates over the response array, similar to:
138
+
139
+ ```
140
+ foreach (message in response)
141
+ callbackOrBlock(message)
142
+ ```
143
+
144
+ In a given callback cycle, the **message** will be the currently iterated message item of the response.
145
+
146
+ ### Simple Usage Examples
147
+
148
+ #### Init and instantiate a new PubNub instance
149
+
150
+ ```ruby
151
+ pubnub = Pubnub.new(
152
+ :subscribe_key => 'demo',
153
+ :publish_key => 'demo',
154
+ :origin => origin,
155
+ :uuid => "myUserID",
156
+ :error_callback => lambda { |msg|
157
+ puts "SOMETHING TERRIBLE HAPPENED HERE: #{msg.inspect}"
158
+ },
159
+ :connect_callback => lambda { |msg|
160
+ puts "CONNECTED: #{msg.inspect}"
161
+ }
162
+ )
163
+ ```
164
+
165
+ #### Publish
166
+ When publishing, send a string, number, array, or hash. PubNub automatically serializes it to JSON for you, so you don't have to.
167
+
168
+
169
+ ```ruby
170
+ @my_callback = lambda { |envelope| puts(envelope.msg) }
171
+
172
+ pubnub.publish(
173
+ :channel => "hello_world"",
174
+ :message => "hi",
175
+ :callback => @my_callback
176
+ )
177
+ ```
178
+
179
+ #### Subscribe
180
+
181
+ ```ruby
182
+ pubnub.subscribe(
183
+ :channel => :hello_world,
184
+ :callback => @my_callback
185
+ )
186
+ ```
187
+
188
+ #### History
189
+ Retrieve previously published messages (requires activation via admin.pubnub.com)
190
+ Optional start, end, and reverse option usage can be found in the tests.
191
+
192
+ ```ruby
193
+ pubnub.history(
194
+ :channel => channel,
195
+ :count => 10,
196
+ :callback => @my_callback
197
+ )
198
+ ```
199
+
200
+ #### Presence
201
+ In real-time see people join and leave with occupancy summaries. (requires activation via admin.pubnub.com)
202
+
203
+ ```ruby
204
+ pubnub.presence(
205
+ :channel => :hello_world,
206
+ :callback => @my_callback
207
+ )
208
+ ```
209
+
210
+ #### Here_now
211
+ See who is "here now" in a channel at this very moment.
212
+
213
+ ```ruby
214
+ pubnub.here_now(
215
+ :channel => channel,
216
+ :callback => @my_callback
217
+ )
218
+ ```
219
+
220
+ #### UUID
221
+
222
+ Session-UUID is automatic, so you will probably not end up ever using this. But if you need a UUID...
223
+
224
+ ```ruby
225
+ pubnub.uuid
226
+ ```
227
+
228
+ If you wish to manually set a custom UUID, pass in a uuid key in the initializer. See "Init and instantiate a new PubNub instance" for an example.
229
+
230
+
231
+ #### Time
232
+ Get the current PubNub time. This is great to use as a "PubNub Ping"
233
+
234
+ ```ruby
235
+ pubnub.time("callback" => @my_callback)
236
+ ```
237
+
238
+ ### Advanced Usage Examples
239
+
240
+ Advanced usage examples can be found in the examples directory.
241
+
242
+ #### demo_console
243
+ A demo console app which shows how to use just about every PubNub Ruby call, just about every possible way!
244
+
245
+ #### translator
246
+ A chat room, with real-time translation! This is using PubNub for the real-time chat, and Bing translation services for babel.
247
+
248
+ #### pubnub_livestream
249
+ Demo rails chat app. It has also been tested with Heroku.
250
+
251
+ #### sinatra
252
+ Sinatra demo.
253
+
254
+ #### sub_and_unsub_1
255
+ Mixing up some async pubs and subs, using blocks and callbacks.
256
+
257
+ #### serial_publish
258
+ Publish 1000 times with an explicit 0.25s delay between publishes
259
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Ruby2::Application.load_tasks
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.4.0
@@ -0,0 +1,198 @@
1
+ require 'pubnub'
2
+
3
+ puts 'Provide origin [demo.pubnub.com]:'
4
+ origin = gets.chomp!
5
+ origin = 'demo.pubnub.com' if origin == ''
6
+
7
+ p = Pubnub.new(
8
+ :subscribe_key => 'demo',
9
+ :publish_key => 'demo',
10
+ :origin => origin,
11
+ :error_callback => lambda { |msg|
12
+ puts "SOMETHING TERRIBLE HAPPENED HERE: #{msg.inspect}"
13
+ },
14
+ :connect_callback => lambda { |msg|
15
+ puts "CONNECTED: #{msg.inspect}"
16
+ }
17
+ )
18
+ default_cb = lambda { |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
19
+
20
+ while(true)
21
+
22
+ ssl = false
23
+ while !%w(Y N).include? ssl
24
+ puts('Should next operation be ssl [Y/N]?')
25
+ ssl = gets.chomp!
26
+ end
27
+
28
+ sync_or_async = false
29
+ while !%w(S A).include? sync_or_async
30
+ puts('Should next operation be [S]ync or [A]sync?')
31
+ sync_or_async = gets.chomp!
32
+ end
33
+
34
+ block_or_parameter = false
35
+ while !%w(B P).include? block_or_parameter
36
+ puts('Do you want pass callback as [B]lock or [P]arameter?')
37
+ block_or_parameter = gets.chomp!
38
+ end
39
+
40
+ puts('1. subscribe')
41
+ puts('2. unsubscribe (leave)')
42
+ puts('3. publish')
43
+ puts('4. history')
44
+ puts('5. presence')
45
+ puts('6. here_now')
46
+ puts('7. time')
47
+ puts("\n\n")
48
+ puts('Enter a selection')
49
+ choice = gets.chomp!
50
+
51
+ case choice
52
+ when '1' #SUBSCRIBE
53
+
54
+ puts('Enter channel')
55
+ channel = gets.chomp!
56
+
57
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
58
+ p.subscribe(:channel => channel, :callback => default_cb, :http_sync => false, :ssl => ssl)
59
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
60
+ p.subscribe(:channel => channel, :http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
61
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
62
+ p.subscribe(:channel => channel, :callback => default_cb, :http_sync => true, :ssl => ssl)
63
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
64
+ p.subscribe(:channel => channel, :http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
65
+ end
66
+
67
+
68
+ when '2' #UNSUBSCRIBE
69
+
70
+ puts('Enter channel')
71
+ channel = gets.chomp!
72
+
73
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
74
+ p.unsubscribe(:channel => channel, :callback => default_cb, :http_sync => false, :ssl => ssl)
75
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
76
+ p.unsubscribe(:channel => channel, :http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
77
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
78
+ p.unsubscribe(:channel => channel, :callback => default_cb, :http_sync => true, :ssl => ssl)
79
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
80
+ p.unsubscribe(:channel => channel, :http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
81
+ end
82
+
83
+ when '3' #PUBLISH
84
+
85
+ puts('Enter channel')
86
+ channel = gets.chomp!
87
+
88
+ puts('Enter message')
89
+ message = gets.chomp!
90
+
91
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
92
+ p.publish(:message => message, :channel => channel, :callback => default_cb, :http_sync => false, :ssl => ssl)
93
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
94
+ p.publish(:message => message, :channel => channel, :http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
95
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
96
+ p.publish(:message => message, :channel => channel, :callback => default_cb, :http_sync => true, :ssl => ssl)
97
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
98
+ p.publish(:message => message, :channel => channel, :http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
99
+ end
100
+
101
+ when '4' #HISTORY
102
+ puts('Enter channel')
103
+ channel = gets.chomp!
104
+
105
+ puts('Enter count')
106
+ count = gets.chomp!
107
+ if (count == '') then count = nil end
108
+
109
+ puts('Enter start')
110
+ history_start = gets.chomp!
111
+ if (history_start == '') then history_start = nil end
112
+
113
+ puts('Enter end')
114
+ history_end = gets.chomp!
115
+ if (history_end == '') then history_end = nil end
116
+
117
+ puts('Enter reverse (y/n)')
118
+ reverse = gets.chomp!
119
+ if (reverse == "" || reverse == "n") then reverse = false else reverse = true end
120
+
121
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
122
+ p.history(:channel => channel,
123
+ :count => count,
124
+ :start => history_start,
125
+ :end => history_end,
126
+ :reverse => reverse,
127
+ :callback => default_cb,
128
+ :http_sync => false,
129
+ :ssl => ssl) do |envelope| puts(envelope.inspect) end
130
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
131
+ p.history(:channel => channel,
132
+ :count => count,
133
+ :start => history_start,
134
+ :end => history_end,
135
+ :reverse => reverse,
136
+ :http_sync => false,
137
+ :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
138
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
139
+ p.history(:channel => channel,
140
+ :count => count,
141
+ :start => history_start,
142
+ :end => history_end,
143
+ :reverse => reverse,
144
+ :callback => default_cb,
145
+ :http_sync => true,
146
+ :ssl => ssl) do |envelope| puts(envelope.inspect) end
147
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
148
+ p.history(:channel => channel,
149
+ :count => count,
150
+ :start => history_start,
151
+ :end => history_end,
152
+ :reverse => true,
153
+ :http_sync => false,
154
+ :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
155
+ end
156
+
157
+ when '5' #PRESENCE
158
+ puts('Enter channel')
159
+ channel = gets.chomp!
160
+
161
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
162
+ p.presence(:channel => channel, :callback => default_cb, :http_sync => false, :ssl => ssl)
163
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
164
+ p.presence(:channel => channel, :http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
165
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
166
+ p.presence(:channel => channel, :callback => default_cb, :http_sync => true, :ssl => ssl)
167
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
168
+ p.presence(:channel => channel, :http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
169
+ end
170
+
171
+ when '6' #HERE_NOW
172
+ puts('Enter channel')
173
+ channel = gets.chomp!
174
+
175
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
176
+ p.here_now(:channel => channel, :callback => default_cb, :http_sync => false, :ssl => ssl)
177
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
178
+ p.here_now(:channel => channel, :http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
179
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
180
+ p.here_now(:channel => channel, :callback => default_cb, :http_sync => true, :ssl => ssl)
181
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
182
+ p.here_now(:channel => channel, :http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
183
+ end
184
+
185
+
186
+ when '7' #TIME
187
+ if sync_or_async == 'A' && block_or_parameter == 'P' #ASYNC AND CALLBACK AS PASSED AS PARAMETER
188
+ p.time(:callback => default_cb, :http_sync => false, :ssl => ssl)
189
+ elsif sync_or_async == 'A' && block_or_parameter == 'B' #ASYNC AND CALLBACK AS PASSED AS BLOCK
190
+ p.time(:http_sync => false, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
191
+ elsif sync_or_async == 'S' && block_or_parameter == 'P' #SYNC AND CALLBACK AS PASSED AS PARAMETER
192
+ p.time(:callback => default_cb, :http_sync => true, :ssl => ssl)
193
+ elsif sync_or_async == 'S' && block_or_parameter == 'B' #SYNC AND CALLBACK AS PASSED AS BLOCK
194
+ p.time(:http_sync => true, :ssl => ssl){ |envelope| puts("\nchannel: #{envelope.channel}: \nmsg: #{envelope.message}") }
195
+ end
196
+
197
+ end
198
+ end