Class: PubnubRequest
- Inherits:
-
Object
- Object
- PubnubRequest
- Defined in:
- lib/pubnub_request.rb
Defined Under Namespace
Classes: RequestError
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#cipher_key ⇒ Object
Returns the value of attribute cipher_key.
-
#error ⇒ Object
Returns the value of attribute error.
-
#history_count ⇒ Object
Returns the value of attribute history_count.
-
#history_end ⇒ Object
Returns the value of attribute history_end.
-
#history_limit ⇒ Object
Returns the value of attribute history_limit.
-
#history_reverse ⇒ Object
Returns the value of attribute history_reverse.
-
#history_start ⇒ Object
Returns the value of attribute history_start.
-
#host ⇒ Object
Returns the value of attribute host.
-
#jsonp ⇒ Object
Returns the value of attribute jsonp.
-
#last_timetoken ⇒ Object
Returns the value of attribute last_timetoken.
-
#message ⇒ Object
Returns the value of attribute message.
-
#operation ⇒ Object
Returns the value of attribute operation.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#port ⇒ Object
Returns the value of attribute port.
-
#publish_key ⇒ Object
Returns the value of attribute publish_key.
-
#query ⇒ Object
Returns the value of attribute query.
-
#response ⇒ Object
Returns the value of attribute response.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#session_uuid ⇒ Object
Returns the value of attribute session_uuid.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#subscribe_key ⇒ Object
Returns the value of attribute subscribe_key.
-
#timetoken ⇒ Object
Returns the value of attribute timetoken.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #==(another) ⇒ Object
- #aes_encrypt(cipher_key, options, publish_request) ⇒ Object
- #encode_URL(request) ⇒ Object
- #first_request? ⇒ Boolean
- #format_url!(override_timetoken = nil) ⇒ Object
-
#initialize(args = {}) ⇒ PubnubRequest
constructor
A new instance of PubnubRequest.
- #op_exception ⇒ Object
- #package_response!(response_data) ⇒ Object
- #set_callback(options) ⇒ Object
- #set_channel(options) ⇒ Object
- #set_cipher_key(options, self_cipher_key) ⇒ Object
- #set_error(options) ⇒ Object
- #set_message(options, self_cipher_key) ⇒ Object
- #set_origin(options) ⇒ Object
- #set_publish_key(options, self_publish_key) ⇒ Object
- #set_secret_key(options, self_secret_key) ⇒ Object
- #set_subscribe_key(options, self_subscribe_key) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ PubnubRequest
Returns a new instance of PubnubRequest.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pubnub_request.rb', line 8 def initialize(args = {}) args = HashWithIndifferentAccess.new(args) @operation = args[:operation].to_s @callback = args[:callback] @cipher_key = args[:cipher_key] @session_uuid = args[:session_uuid] @publish_key = args[:publish_key] @subscribe_key = args[:subscribe_key] @channel = args[:channel] @jsonp = args[:jsonp].present? ? "1" : "0" @message = args[:message] @secret_key = args[:secret_key] || "0" @timetoken = args[:timetoken] || "0" @ssl = args[:ssl] @port = args[:port] @url = args[:url] @host = args[:host] @query = args[:query] end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def callback @callback end |
#channel ⇒ Object
Returns the value of attribute channel.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def channel @channel end |
#cipher_key ⇒ Object
Returns the value of attribute cipher_key.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def cipher_key @cipher_key end |
#error ⇒ Object
Returns the value of attribute error.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def error @error end |
#history_count ⇒ Object
Returns the value of attribute history_count.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def history_count @history_count end |
#history_end ⇒ Object
Returns the value of attribute history_end.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def history_end @history_end end |
#history_limit ⇒ Object
Returns the value of attribute history_limit.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def history_limit @history_limit end |
#history_reverse ⇒ Object
Returns the value of attribute history_reverse.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def history_reverse @history_reverse end |
#history_start ⇒ Object
Returns the value of attribute history_start.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def history_start @history_start end |
#host ⇒ Object
Returns the value of attribute host.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def host @host end |
#jsonp ⇒ Object
Returns the value of attribute jsonp.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def jsonp @jsonp end |
#last_timetoken ⇒ Object
Returns the value of attribute last_timetoken.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def last_timetoken @last_timetoken end |
#message ⇒ Object
Returns the value of attribute message.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def @message end |
#operation ⇒ Object
Returns the value of attribute operation.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def operation @operation end |
#origin ⇒ Object
Returns the value of attribute origin.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def origin @origin end |
#port ⇒ Object
Returns the value of attribute port.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def port @port end |
#publish_key ⇒ Object
Returns the value of attribute publish_key.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def publish_key @publish_key end |
#query ⇒ Object
Returns the value of attribute query.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def query @query end |
#response ⇒ Object
Returns the value of attribute response.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def response @response end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def secret_key @secret_key end |
#session_uuid ⇒ Object
Returns the value of attribute session_uuid.
3 4 5 |
# File 'lib/pubnub_request.rb', line 3 def session_uuid @session_uuid end |
#ssl ⇒ Object
Returns the value of attribute ssl.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def ssl @ssl end |
#subscribe_key ⇒ Object
Returns the value of attribute subscribe_key.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def subscribe_key @subscribe_key end |
#timetoken ⇒ Object
Returns the value of attribute timetoken.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def timetoken @timetoken end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/pubnub_request.rb', line 2 def url @url end |
Instance Method Details
#==(another) ⇒ Object
38 39 40 41 |
# File 'lib/pubnub_request.rb', line 38 def ==(another) self.operation == another.operation && self.callback == another.callback && self.channel == another.channel && self. == another. end |
#aes_encrypt(cipher_key, options, publish_request) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/pubnub_request.rb', line 289 def aes_encrypt(cipher_key, , publish_request) = HashWithIndifferentAccess.new() pc = PubnubCrypto.new(cipher_key) publish_request. = pc.encrypt([:message]) end |
#encode_URL(request) ⇒ Object
297 298 299 300 301 302 303 304 |
# File 'lib/pubnub_request.rb', line 297 def encode_URL(request) ## Construct Request URL url = '/' + request.map { |bit| bit.split('').map { |ch| ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ? '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch) }.join('') }.join('/') return url end |
#first_request? ⇒ Boolean
306 307 308 |
# File 'lib/pubnub_request.rb', line 306 def first_request? @last_timetoken == "0" end |
#format_url!(override_timetoken = nil) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/pubnub_request.rb', line 202 def format_url!(override_timetoken = nil) raise(Pubnub::PublishError, "Missing .operation in PubnubRequest object") if self.operation.blank? if @ssl.present? origin = 'https://' + (self.origin.present? ? self.origin : Pubnub::ORIGIN_HOST) @port = 443 else origin = 'http://' + (self.origin.present? ? self.origin : Pubnub::ORIGIN_HOST) @port = 80 end if override_timetoken.present? self.timetoken = override_timetoken.to_s end case self.operation.to_s when "publish" url_array = [self.operation.to_s, self.publish_key.to_s, self.subscribe_key.to_s, self.secret_key.to_s, self.channel.to_s, "0", self.] when "subscribe" url_array = [self.operation.to_s, self.subscribe_key.to_s, self.channel.to_s, "0", @timetoken] when "presence" url_array = ["subscribe", self.subscribe_key.to_s, ((self.channel.to_s) + "-pnpres"), "0", @timetoken] when "time" url_array = [self.operation.to_s, "0"] when "history" url_array = [self.operation.to_s, self.subscribe_key.to_s, self.channel.to_s, "0", self.history_limit.to_s] when "detailed_history" url_array = ["v2", "history", "sub-key", self.subscribe_key.to_s, "channel", self.channel.to_s] when "here_now" url_array = ["v2", "presence", "sub-key", self.subscribe_key.to_s, "channel", self.channel.to_s] else raise(PubnubRequest::RequestError, "I can't create that URL for you due to unknown operation type.") end self.url = origin + encode_URL(url_array) uri = URI.parse(self.url) self.host = uri.host url_params = "" if %w(subscribe presence).include?(@operation) uri.query = uri.query.blank? ? "uuid=#{@session_uuid}" : (uri.query + "uuid=#{@session_uuid}") elsif @operation == "detailed_history" url_sep = "?" if @history_count || @history_start || @history_end || @history_reverse if @history_count url_params += url_sep + "count=" + @history_count.to_s url_sep = "&" end if @history_start url_params += url_sep + "start=" + @history_start.to_s url_sep = "&" end if @history_end url_params += url_sep + "end=" + @history_end.to_s url_sep = "&" end if @history_reverse url_params += url_sep + "reverse=true" url_sep = "&" end end end self.query = uri.path + (uri.query.present? ? ("?" + uri.query) : "") + url_params self.url += url_params self end |
#op_exception ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/pubnub_request.rb', line 30 def op_exception if @operation.present? ("Pubnub::" + @operation.to_s.capitalize + "Error").constantize else PubnubRequest::RequestError end end |
#package_response!(response_data) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/pubnub_request.rb', line 161 def package_response!(response_data) self.response = response_data.respond_to?(:content) ? Yajl.load(response_data.content) : Yajl.load(response_data) self.last_timetoken = self.timetoken self.timetoken = self.response[1] unless self.operation == "time" if self.cipher_key.present? && %w(subscribe history detailed_history).include?(self.operation) myarr = Array.new pc = PubnubCrypto.new(@cipher_key) case @operation when "publish" iterate = self.response.first when "subscribe" iterate = self.response.first when "history" iterate = self.response when "detailed_history" iterate = self.response.first else raise(RequestError, "Don't know how to iterate on this operation.") end iterate.each do || = pc.decrypt() myarr.push() end if %w(publish subscribe).include?(@operation) self.response[0] = myarr elsif @operation == "detailed_history" json_response_data = Yajl.load(response_data) self.response = [myarr, json_response_data[1], json_response_data[2]] else self.response = myarr end end end |
#set_callback(options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pubnub_request.rb', line 72 def set_callback() = HashWithIndifferentAccess.new() if [:callback].blank? raise(op_exception, "callback is a required parameter.") elsif ![:callback].try(:respond_to?, "call") raise(op_exception, "callback is invalid.") else self.callback = [:callback] self end end |
#set_channel(options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pubnub_request.rb', line 61 def set_channel() = HashWithIndifferentAccess.new() if [:channel].blank? raise(op_exception, "channel is a required parameter.") else self.channel = [:channel].to_s self end end |
#set_cipher_key(options, self_cipher_key) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pubnub_request.rb', line 86 def set_cipher_key(, self_cipher_key) = HashWithIndifferentAccess.new() if self_cipher_key.present? && ['cipher_key'].present? raise(op_exception, "existing cipher_key #{self_cipher_key} cannot be overridden at publish-time.") elsif (self_cipher_key.present? && [:cipher_key].blank?) || (self_cipher_key.blank? && [:cipher_key].present?) this_cipher_key = self_cipher_key || [:cipher_key] raise(Pubnub::PublishError, "secret key must be a string.") if this_cipher_key.class != String self.cipher_key = this_cipher_key end end |
#set_error(options) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/pubnub_request.rb', line 43 def set_error() = HashWithIndifferentAccess.new() if [:error].present? self.error = true end self end |
#set_message(options, self_cipher_key) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pubnub_request.rb', line 121 def (, self_cipher_key) = HashWithIndifferentAccess.new() if [:message].blank? && [:message] != "" raise(op_exception, "message is a required parameter.") else my_cipher_key = [:cipher_key] || self_cipher_key if my_cipher_key.present? self. = Yajl.dump(aes_encrypt(my_cipher_key, , self)) else self. = Yajl.dump([:message]) end end end |
#set_origin(options) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/pubnub_request.rb', line 52 def set_origin() = HashWithIndifferentAccess.new() if [:origin].present? self.origin = [:origin].to_s self end end |
#set_publish_key(options, self_publish_key) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pubnub_request.rb', line 137 def set_publish_key(, self_publish_key) = HashWithIndifferentAccess.new() if [:publish_key].blank? && self_publish_key.blank? raise(Pubnub::PublishError, "publish_key is a required parameter.") elsif self_publish_key.present? && ['publish_key'].present? raise(Pubnub::PublishError, "existing publish_key #{self_publish_key} cannot be overridden at publish-time.") else self.publish_key = (self_publish_key || [:publish_key]).to_s end end |
#set_secret_key(options, self_secret_key) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pubnub_request.rb', line 100 def set_secret_key(, self_secret_key) = HashWithIndifferentAccess.new() if self_secret_key.present? && ['secret_key'].present? raise(Pubnub::PublishError, "existing secret_key #{self_secret_key} cannot be overridden at publish-time.") elsif (self_secret_key.present? && [:secret_key].blank?) || (self_secret_key.blank? && [:secret_key].present?) my_secret_key = self_secret_key || [:secret_key] raise(Pubnub::PublishError, "secret key must be a string.") if my_secret_key.class != String signature = "{ @publish_key, @subscribe_key, @secret_key, channel, message}" digest = OpenSSL::Digest.new("sha256") key = [my_secret_key] hmac = OpenSSL::HMAC.hexdigest(digest, key.pack("H*"), signature) self.secret_key = hmac else self.secret_key = "0" end end |
#set_subscribe_key(options, self_subscribe_key) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/pubnub_request.rb', line 149 def set_subscribe_key(, self_subscribe_key) = HashWithIndifferentAccess.new() if [:subscribe_key].blank? && self_subscribe_key.blank? raise(op_exception, "subscribe_key is a required parameter.") elsif self_subscribe_key.present? && ['subscribe_key'].present? raise(op_exception, "existing subscribe_key #{self_subscribe_key} cannot be overridden at subscribe-time.") else self.subscribe_key = (self_subscribe_key || [:subscribe_key]).to_s end end |