Class: Pubnub::Request
- Includes:
- Configuration, Error
- Defined in:
- lib/pubnub/request.rb
Constant Summary
Constants included from Configuration
Configuration::DEFAULT_AUTO_RECONNECT, Configuration::DEFAULT_CALLBACK, Configuration::DEFAULT_CHANNEL, Configuration::DEFAULT_CONTENT_TYPE, Configuration::DEFAULT_ENCODING, Configuration::DEFAULT_HEADERS, Configuration::DEFAULT_METHOD, Configuration::DEFAULT_ORIGIN, Configuration::DEFAULT_PARAMS, Configuration::DEFAULT_PATH, Configuration::DEFAULT_PORT, Configuration::DEFAULT_PUBLISH_KEY, Configuration::DEFAULT_SECRET_KEY, Configuration::DEFAULT_SSL_SET, Configuration::DEFAULT_SUBSCRIBE_KEY, Configuration::DEFAULT_TIMEOUT, Configuration::DEFAULT_TIMETOKEN, Configuration::DEFAULT_USER_AGENT, Configuration::MAX_RETRIES, Configuration::PERIODIC_TIMER
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.
-
#envelopes ⇒ Object
Returns the value of attribute envelopes.
-
#error_callback ⇒ Object
Returns the value of attribute error_callback.
-
#message ⇒ Object
Returns the value of attribute message.
-
#operation ⇒ Object
Returns the value of attribute operation.
-
#port ⇒ Object
Returns the value of attribute port.
-
#publish_key ⇒ Object
Returns the value of attribute publish_key.
-
#response ⇒ Object
Returns the value of attribute response.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#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.
Instance Method Summary collapse
- #encode_path(request) ⇒ Object
- #handle_response(http) ⇒ Object
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #origin ⇒ Object
- #params ⇒ Object
- #path ⇒ Object
- #query ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Request
Returns a new instance of Request.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pubnub/request.rb', line 18 def initialize( = {}) @options = @params = [:params] @operation = [:operation] @callback = [:callback] @error_callback = [:error_callback] @error_callback = lambda { |x| puts "AN ERROR OCCURRED: #{x.msg}" } unless @error_callback @channel = [:channel] @message = [:message] @timetoken = [:timetoken] || "0" @timetoken = [:override_timetoken] if [:override_timetoken] @ssl = [:ssl] @params = [:params] @history_limit = [:limit] @port = [:port] @host = [:origin] @query = [:query] set_cipher_key(, @cipher_key) if %w(publish subscribe history).include? @operation (, @cipher_key) if %w(publish).include? @operation set_publish_key(, @publish_key) if %w(publish).include? @operation set_subscribe_key(, @subscribe_key) if %w(publish presence here_now history subscribe leave).include? @operation set_secret_key(, @secret_key) if %w(publish subscribe).include? @operation end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def callback @callback end |
#channel ⇒ Object
Returns the value of attribute channel.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def channel @channel end |
#cipher_key ⇒ Object
Returns the value of attribute cipher_key.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def cipher_key @cipher_key end |
#envelopes ⇒ Object
Returns the value of attribute envelopes.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def envelopes @envelopes end |
#error_callback ⇒ Object
Returns the value of attribute error_callback.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def error_callback @error_callback end |
#message ⇒ Object
Returns the value of attribute message.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def @message end |
#operation ⇒ Object
Returns the value of attribute operation.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def operation @operation end |
#port ⇒ Object
Returns the value of attribute port.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def port @port end |
#publish_key ⇒ Object
Returns the value of attribute publish_key.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def publish_key @publish_key end |
#response ⇒ Object
Returns the value of attribute response.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def response @response end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def secret_key @secret_key end |
#ssl ⇒ Object
Returns the value of attribute ssl.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def ssl @ssl end |
#subscribe_key ⇒ Object
Returns the value of attribute subscribe_key.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def subscribe_key @subscribe_key end |
#timetoken ⇒ Object
Returns the value of attribute timetoken.
16 17 18 |
# File 'lib/pubnub/request.rb', line 16 def timetoken @timetoken end |
Instance Method Details
#encode_path(request) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/pubnub/request.rb', line 134 def encode_path(request) $log.debug 'ENCODING PATH' path = URI.escape('/' + request.map{|i| i.to_s}.reject(&:empty?).join('/')).gsub(/\?/,'%3F') if @operation == 'leave' $log.debug "#{path}/leave" "#{path}/leave" else $log.debug path path end end |
#handle_response(http) ⇒ 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 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/pubnub/request.rb', line 161 def handle_response(http) @response = nil if http.respond_to?(:body) && http.respond_to?(:code) && http.respond_to?(:message) && http.respond_to?(:headers) # httparty @response = JSON.load(http.body) else # em-http-request @response = http.response.respond_to?(:content) ? JSON.load(http.response.content) : JSON.load(http.response) end @last_timetoken = @timetoken @timetoken = @response[1] unless @operation == 'time' if @cipher_key.present? && %w(subscribe history).include?(@operation) response_array = Array.new crypto = Pubnub::Crypto.new(@cipher_key) if %w(subscribe history).include?(@operation) iteration = @response[0] else iteration = @response end if iteration.class == Array iteration.each do |msg| response_array << crypto.decrypt(msg) end if iteration elsif iteration.class == String response_array = [crypto.decrypt(iteration)] end case @operation when 'subscribe' @response[0] = response_array when 'history' json_response_data = JSON.load(http.response) @response = [response_array, json_response_data[1], json_response_data[2]] end end @envelopes = Array.new if %w(subscribe history).include? @operation @response.first.each_with_index do |res,index| @envelopes << Pubnub::Response.new(:http => http, :index => index, :response => @response, :channel => @channel, :operation => @operation, :path => path, :query => query) end else @envelopes << Pubnub::Response.new(:http => http, :channel => @channel, :response => @response, :operation => @operation, :path => path, :query => query) end end |
#origin ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pubnub/request.rb', line 57 def origin if @ssl @origin = 'https://' + @host @port = 443 else @origin = 'http://' + @host @port = 80 end @origin end |
#params ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/pubnub/request.rb', line 146 def params flat = {} @params.each do |param,val| next if val.to_s.empty? flat[param.to_s] = val.to_s end flat end |
#path ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pubnub/request.rb', line 68 def path encode_path(case @operation when 'publish' [ @operation, @publish_key, @subscribe_key, @secret_key, @channel, '0', @message.to_json ] when 'subscribe' [ @operation, @subscribe_key, @channel, '0', @timetoken ] when 'presence' [ 'subscribe', @subscribe_key, @channel.to_s + '-pnpres', '0', @timetoken ] when 'time' [ @operation, '0' ] when 'history' [ 'v2', 'history', 'sub-key', @subscribe_key, 'channel', @channel ] when 'here_now' [ 'v2', 'presence', 'sub-key', @subscribe_key, 'channel', @channel ] when 'leave' [ 'v2', 'presence', 'sub-key', @subscribe_key, 'channel', @channel ] else raise("I can't create that URL for you due to unknown operation type.") end ) end |
#query ⇒ Object
155 156 157 158 159 |
# File 'lib/pubnub/request.rb', line 155 def query params.map do |param, value| [param, value].join('=') end.sort.join('&') end |