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,25 @@
1
+ module Pubnub
2
+ module Configuration
3
+ DEFAULT_SECRET_KEY = 0
4
+ DEFAULT_TIMETOKEN = 0
5
+ DEFAULT_ORIGIN = 'pubsub.pubnub.com'
6
+ DEFAULT_PORT = 80
7
+ DEFAULT_METHOD = 'GET'
8
+ DEFAULT_CONTENT_TYPE = 'application/json'
9
+ DEFAULT_PATH = '/'
10
+ DEFAULT_PARAMS = {}
11
+ DEFAULT_HEADERS = {}
12
+ DEFAULT_USER_AGENT = "Pubnub Ruby #{PUBNUB_VERSION}"
13
+ DEFAULT_SSL_SET = false
14
+ DEFAULT_TIMEOUT = 5
15
+ DEFAULT_ENCODING = nil
16
+ DEFAULT_AUTO_RECONNECT = true
17
+ DEFAULT_CALLBACK = lambda { |data| puts data }
18
+ DEFAULT_PUBLISH_KEY = 'demo'
19
+ DEFAULT_SUBSCRIBE_KEY = 'demo'
20
+ DEFAULT_CHANNEL = 'hello_world'
21
+ PERIODIC_TIMER = 0.25
22
+ MAX_RETRIES = 60
23
+ end
24
+ end
25
+
@@ -0,0 +1,53 @@
1
+ module Pubnub
2
+ class Crypto
3
+ def initialize(cipher_key)
4
+ @alg = 'AES-256-CBC'
5
+ sha256_key = Digest::SHA256.hexdigest(cipher_key)
6
+ @key = sha256_key.slice(0,32)
7
+
8
+ @iv = '0123456789012345'
9
+ end
10
+
11
+
12
+ def encrypt(message)
13
+
14
+ aes = OpenSSL::Cipher::Cipher.new(@alg)
15
+ aes.encrypt
16
+ aes.key = @key
17
+ aes.iv = @iv
18
+
19
+ json_message = message.to_json
20
+ cipher = aes.update(json_message)
21
+ cipher << aes.final
22
+
23
+ Base64.strict_encode64(cipher)
24
+
25
+ end
26
+
27
+
28
+ def decrypt(cipher_text)
29
+ decode_cipher = OpenSSL::Cipher::Cipher.new(@alg)
30
+ decode_cipher.decrypt
31
+ decode_cipher.key = @key
32
+ decode_cipher.iv = @iv
33
+
34
+ plain_text = ''
35
+
36
+ begin
37
+ undecoded_text = Base64.decode64(cipher_text)
38
+ plain_text = decode_cipher.update(undecoded_text)
39
+ plain_text << decode_cipher.final
40
+ rescue => e
41
+ return "DECRYPTION_ERROR #{e}"
42
+ end
43
+
44
+ begin
45
+ JSON.load(plain_text)
46
+ rescue JSON::ParserError
47
+ JSON.load("[#{plain_text}]")[0] # srsly ruby? srsly?
48
+ rescue
49
+ return 'PARSE DECRYPTION MESSAGE ERROR'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ module Pubnub
2
+ module Error
3
+ class PresenceError < RuntimeError;
4
+ end
5
+ class PublishError < RuntimeError;
6
+ end
7
+ class SubscribeError < RuntimeError;
8
+ end
9
+ class InitError < RuntimeError;
10
+ end
11
+ class PublishError < RuntimeError;
12
+ end
13
+ class OperationError < RuntimeError
14
+ def operation_exception
15
+ #if @operation.present?
16
+ # ("Pubnub::" + @operation.to_s.capitalize + "Error").constantize
17
+ #else
18
+ PubnubRequest::RequestError
19
+ #end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,288 @@
1
+ require 'pubnub/configuration.rb'
2
+ require 'pubnub/error.rb'
3
+ require 'pubnub/response.rb'
4
+
5
+ require 'openssl'
6
+ require 'digest/sha2'
7
+ require 'base64'
8
+
9
+ require 'json'
10
+
11
+ module Pubnub
12
+ class Request
13
+ include Pubnub::Configuration
14
+ include Pubnub::Error
15
+
16
+ attr_accessor :error_callback, :envelopes, :port, :timetoken, :operation, :response, :ssl, :channel, :callback, :cipher_key, :subscribe_key, :secret_key, :operation, :message, :publish_key
17
+
18
+ def initialize(options = {})
19
+ @options = options
20
+
21
+ @params = options[:params]
22
+ @operation = options[:operation]
23
+ @callback = options[:callback]
24
+ @error_callback = options[:error_callback]
25
+ @error_callback = lambda { |x| puts "AN ERROR OCCURRED: #{x.msg}" } unless @error_callback
26
+ @channel = options[:channel]
27
+ @message = options[:message]
28
+ @timetoken = options[:timetoken] || "0"
29
+ @timetoken = options[:override_timetoken] if options[:override_timetoken]
30
+ @ssl = options[:ssl]
31
+ @params = options[:params]
32
+
33
+ @history_limit = options[:limit]
34
+
35
+ @port = options[:port]
36
+ @host = options[:origin]
37
+ @query = options[:query]
38
+
39
+ set_cipher_key(options, @cipher_key) if %w(publish subscribe history).include? @operation
40
+ set_message(options, @cipher_key) if %w(publish).include? @operation
41
+ set_publish_key(options, @publish_key) if %w(publish).include? @operation
42
+ set_subscribe_key(options, @subscribe_key) if %w(publish presence here_now history subscribe leave).include? @operation
43
+ set_secret_key(options, @secret_key) if %w(publish subscribe).include? @operation
44
+
45
+ end
46
+
47
+ def ssl=(ssl)
48
+ if ssl
49
+ @ssl = true
50
+ @port = 443
51
+ else
52
+ @ssl = false
53
+ @port = 80
54
+ end
55
+ end
56
+
57
+ def origin
58
+ if @ssl
59
+ @origin = 'https://' + @host
60
+ @port = 443
61
+ else
62
+ @origin = 'http://' + @host
63
+ @port = 80
64
+ end
65
+ @origin
66
+ end
67
+
68
+ def path
69
+ encode_path(case @operation
70
+ when 'publish'
71
+ [
72
+ @operation,
73
+ @publish_key,
74
+ @subscribe_key,
75
+ @secret_key,
76
+ @channel,
77
+ '0',
78
+ @message.to_json
79
+ ]
80
+ when 'subscribe'
81
+ [
82
+ @operation,
83
+ @subscribe_key,
84
+ @channel,
85
+ '0',
86
+ @timetoken
87
+ ]
88
+ when 'presence'
89
+ [
90
+ 'subscribe',
91
+ @subscribe_key,
92
+ @channel.to_s + '-pnpres',
93
+ '0',
94
+ @timetoken
95
+ ]
96
+ when 'time'
97
+ [
98
+ @operation,
99
+ '0'
100
+ ]
101
+ when 'history'
102
+ [
103
+ 'v2',
104
+ 'history',
105
+ 'sub-key',
106
+ @subscribe_key,
107
+ 'channel',
108
+ @channel
109
+ ]
110
+ when 'here_now'
111
+ [
112
+ 'v2',
113
+ 'presence',
114
+ 'sub-key',
115
+ @subscribe_key,
116
+ 'channel',
117
+ @channel
118
+ ]
119
+ when 'leave'
120
+ [
121
+ 'v2',
122
+ 'presence',
123
+ 'sub-key',
124
+ @subscribe_key,
125
+ 'channel',
126
+ @channel
127
+ ]
128
+ else
129
+ raise("I can't create that URL for you due to unknown operation type.")
130
+ end
131
+ )
132
+ end
133
+
134
+ def encode_path(request)
135
+ $log.debug 'ENCODING PATH'
136
+ path = URI.escape('/' + request.map{|i| i.to_s}.reject(&:empty?).join('/')).gsub(/\?/,'%3F')
137
+ if @operation == 'leave'
138
+ $log.debug "#{path}/leave"
139
+ "#{path}/leave"
140
+ else
141
+ $log.debug path
142
+ path
143
+ end
144
+ end
145
+
146
+ def params
147
+ flat = {}
148
+ @params.each do |param,val|
149
+ next if val.to_s.empty?
150
+ flat[param.to_s] = val.to_s
151
+ end
152
+ flat
153
+ end
154
+
155
+ def query
156
+ params.map do |param, value|
157
+ [param, value].join('=')
158
+ end.sort.join('&')
159
+ end
160
+
161
+ def handle_response(http)
162
+ @response = nil
163
+
164
+ if http.respond_to?(:body) && http.respond_to?(:code) && http.respond_to?(:message) && http.respond_to?(:headers) # httparty
165
+ @response = JSON.load(http.body)
166
+ else # em-http-request
167
+ @response = http.response.respond_to?(:content) ? JSON.load(http.response.content) : JSON.load(http.response)
168
+ end
169
+
170
+
171
+ @last_timetoken = @timetoken
172
+ @timetoken = @response[1] unless @operation == 'time'
173
+
174
+ if @cipher_key.present? && %w(subscribe history).include?(@operation)
175
+
176
+ response_array = Array.new
177
+ crypto = Pubnub::Crypto.new(@cipher_key)
178
+
179
+ if %w(subscribe history).include?(@operation)
180
+ iteration = @response[0]
181
+ else
182
+ iteration = @response
183
+ end
184
+
185
+ if iteration.class == Array
186
+ iteration.each do |msg|
187
+ response_array << crypto.decrypt(msg)
188
+ end if iteration
189
+ elsif iteration.class == String
190
+ response_array = [crypto.decrypt(iteration)]
191
+ end
192
+
193
+ case @operation
194
+ when 'subscribe'
195
+ @response[0] = response_array
196
+ when 'history'
197
+ json_response_data = JSON.load(http.response)
198
+ @response = [response_array, json_response_data[1], json_response_data[2]]
199
+ end
200
+ end
201
+
202
+ @envelopes = Array.new
203
+ if %w(subscribe history).include? @operation
204
+ @response.first.each_with_index do |res,index|
205
+ @envelopes << Pubnub::Response.new(:http => http, :index => index, :response => @response, :channel => @channel, :operation => @operation, :path => path, :query => query)
206
+ end
207
+ else
208
+ @envelopes << Pubnub::Response.new(:http => http, :channel => @channel, :response => @response, :operation => @operation, :path => path, :query => query)
209
+ end
210
+ end
211
+
212
+
213
+ private
214
+
215
+ def aes_encrypt(cipher_key, options, publish_request)
216
+
217
+ pc = Pubnub::Crypto.new(cipher_key)
218
+ publish_request.message = pc.encrypt(options[:message])
219
+
220
+ end
221
+
222
+ def set_cipher_key(options, self_cipher_key)
223
+ if self_cipher_key.present? && options[:cipher_key].present?
224
+ raise(OperationError, "existing cipher_key #{self_cipher_key} cannot be overridden at publish-time.")
225
+
226
+ elsif (self_cipher_key.present? && options[:cipher_key].blank?) || (self_cipher_key.blank? && options[:cipher_key].present?)
227
+
228
+ this_cipher_key = self_cipher_key || options[:cipher_key]
229
+ raise(OperationError, 'secret key must be a string.') if this_cipher_key.class != String
230
+ @cipher_key = this_cipher_key
231
+ end
232
+ end
233
+
234
+ def set_secret_key(options, self_secret_key)
235
+ if self_secret_key.present? && options[:secret_key].present?
236
+ raise(OperationError, "existing secret_key #{self_secret_key} cannot be overridden at publish-time.")
237
+
238
+ elsif (self_secret_key.present? && options[:secret_key].blank?) || (self_secret_key.blank? && options[:secret_key].present?)
239
+
240
+ my_secret_key = self_secret_key || options[:secret_key]
241
+ raise(OperationError, 'secret key must be a string.') if my_secret_key.class != String
242
+
243
+ signature = '{ @publish_key, @subscribe_key, @secret_key, channel, message}'
244
+ digest = OpenSSL::Digest.new('sha256')
245
+ key = [my_secret_key]
246
+ hmac = OpenSSL::HMAC.hexdigest(digest, key.pack('H*'), signature)
247
+ @secret_key = hmac
248
+ else
249
+ @secret_key = '0'
250
+ end
251
+ end
252
+
253
+ def set_message(options, self_cipher_key)
254
+ if options[:message].blank? && options[:message] != ''
255
+ raise(OperationError, 'message is a required parameter.')
256
+ else
257
+ my_cipher_key = options[:cipher_key] || self_cipher_key
258
+
259
+ if my_cipher_key.present?
260
+ @message = aes_encrypt(my_cipher_key, options, self)
261
+ else
262
+ @message = options[:message]
263
+ end
264
+ end
265
+ end
266
+
267
+ def set_publish_key(options, self_publish_key)
268
+ if options[:publish_key].blank? && self_publish_key.blank?
269
+ raise(OperationError, 'publish_key is a required parameter.')
270
+ elsif self_publish_key.present? && options[:publish_key].present?
271
+ raise(OperationError, "existing publish_key #{self_publish_key} cannot be overridden at publish-time.")
272
+ else
273
+ @publish_key = (self_publish_key || options[:publish_key]).to_s
274
+ end
275
+ end
276
+
277
+ def set_subscribe_key(options, self_subscribe_key)
278
+ if options[:subscribe_key].blank? && self_subscribe_key.blank?
279
+ raise(OperationError, 'subscribe_key is a required parameter.')
280
+ elsif self_subscribe_key.present? && options[:subscribe_key].present?
281
+ raise(OperationError, "existing subscribe_key #{self_subscribe_key} cannot be overridden at subscribe-time.")
282
+ else
283
+ @subscribe_key = (self_subscribe_key || options[:subscribe_key]).to_s
284
+ end
285
+ end
286
+
287
+ end
288
+ end
@@ -0,0 +1,126 @@
1
+ class Pubnub::Response
2
+ attr_reader :message, :channel, :timetoken, :status_code
3
+ attr_reader :response, :path, :query
4
+ alias :msg :message
5
+
6
+ # Creates Pubnub::Response object based on options hash
7
+ #
8
+ # Sets @message, @channel, @timetoken, @status_code, :response attributes
9
+ # @message contains usable single message for callback
10
+ # @channel contains channel which was used in request
11
+ # @timetoken contains server timetoken
12
+ # @status_code contains status code of server response
13
+ # @response contains whole server response body which was used to build that Pubnub::Response
14
+ # @headers contains whole server response headers
15
+ #
16
+ # To properly init new response object it need :http, :response, :channel options
17
+ # While you pass :index option it will treat :response as array which size is greater than one
18
+ # In case you want create your custom error response, you have to pass :error_init option with true value and :message with your message
19
+ def initialize(options = {})
20
+ @path = options[:path]
21
+ @query = options[:query]
22
+
23
+ if options[:error_init]
24
+ @message = options[:message]
25
+ @response = options[:message]
26
+ @timetoken = 0
27
+ else
28
+ if options[:http].respond_to?(:body) && options[:http].respond_to?(:code) && options[:http].respond_to?(:message) && options[:http].respond_to?(:headers)
29
+ httparty = true
30
+ else
31
+ httparty = false
32
+ end
33
+
34
+ if httparty
35
+ case options[:operation]
36
+ when 'publish'
37
+ set_for_httparty(options)
38
+ when 'subscribe'
39
+ set_for_httparty(options)
40
+ when 'presence'
41
+ set_for_httparty(options)
42
+ when 'history'
43
+ set_for_httparty(options)
44
+ when 'leave'
45
+ set_for_httparty(options)
46
+ when 'here_now'
47
+ set_for_httparty(options)
48
+ when 'time'
49
+ set_for_httparty(options)
50
+ end
51
+ else # EM.http_request
52
+ case options[:operation]
53
+ when 'publish'
54
+ set_for_em_http_request(options)
55
+ when 'subscribe'
56
+ set_for_em_http_request(options)
57
+ when 'presence'
58
+ set_for_em_http_request(options)
59
+ when 'history'
60
+ set_for_em_http_request(options)
61
+ when 'leave'
62
+ set_for_em_http_request(options)
63
+ when 'here_now'
64
+ set_for_em_http_request(options)
65
+ when 'time'
66
+ set_for_em_http_request(options)
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ # Simple compare to other object basing on string representation of response
73
+ # (see #to_s)
74
+ def ==(other)
75
+ @response == other.to_s
76
+ end
77
+
78
+ private
79
+
80
+ def set_for_httparty(options)
81
+ if options[:response][2] && options[:operation] == 'subscribe'
82
+ @channel = options[:index] ? options[:response][2].split(',')[options[:index]] : options[:channel]
83
+ else
84
+ @channel = options[:channel]
85
+ end
86
+
87
+ if options[:operation] == 'publish'
88
+ set_for_publish(options[:response][1],options[:response][2])
89
+ elsif %w(here_now leave).include? options[:operation]
90
+ @message = options[:response]
91
+ else
92
+ @timetoken = options[:response][1]
93
+ @message = options[:index] ? options[:response][0][options[:index]] : options[:response][0]
94
+ end
95
+
96
+ @status_code = options[:http].response.code
97
+ @headers = options[:http].headers.inspect
98
+ @response = options[:http].body
99
+ end
100
+
101
+ def set_for_em_http_request(options)
102
+ if options[:response][2] && options[:operation] == 'subscribe'
103
+ @channel = options[:index] ? options[:response][2].split(',')[options[:index]] : options[:channel]
104
+ else
105
+ @channel = options[:channel]
106
+ end
107
+
108
+ if options[:operation] == 'publish'
109
+ set_for_publish(options[:response][1],options[:response][2])
110
+ elsif %w(here_now leave).include? options[:operation]
111
+ @message = options[:response]
112
+ else
113
+ @timetoken = options[:response][1]
114
+ @message = options[:index] ? options[:response][0][options[:index]] : options[:response][0]
115
+ end
116
+
117
+ @status_code = options[:http].response_header.status.to_i
118
+ @headers = options[:http].response_header
119
+ @response = options[:http].response
120
+ end
121
+
122
+ def set_for_publish(msg, timetoken)
123
+ @message = msg
124
+ @timetoken = timetoken
125
+ end
126
+ end