Class: Pubnub::Request

Inherits:
Object show all
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

Instance Method Summary collapse

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 = {})
  @options = options

  @params         = options[:params]
  @operation      = options[:operation]
  @callback       = options[:callback]
  @error_callback = options[:error_callback]
  @error_callback = lambda { |x| puts "AN ERROR OCCURRED: #{x.msg}" } unless @error_callback
  @channel        = options[:channel]
  @message        = options[:message]
  @timetoken      = options[:timetoken] || "0"
  @timetoken      = options[:override_timetoken] if options[:override_timetoken]
  @ssl            = options[:ssl]
  @params         = options[:params]

  @history_limit  = options[:limit]

  @port           = options[:port]
  @host           = options[:origin]
  @query          = options[:query]

  set_cipher_key(options, @cipher_key) if %w(publish subscribe history).include? @operation
  set_message(options, @cipher_key) if %w(publish).include? @operation
  set_publish_key(options, @publish_key) if %w(publish).include? @operation
  set_subscribe_key(options, @subscribe_key) if %w(publish presence here_now history subscribe leave).include? @operation
  set_secret_key(options, @secret_key) if %w(publish subscribe).include? @operation

end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def callback
  @callback
end

#channelObject

Returns the value of attribute channel.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def channel
  @channel
end

#cipher_keyObject

Returns the value of attribute cipher_key.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def cipher_key
  @cipher_key
end

#envelopesObject

Returns the value of attribute envelopes.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def envelopes
  @envelopes
end

#error_callbackObject

Returns the value of attribute error_callback.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def error_callback
  @error_callback
end

#messageObject

Returns the value of attribute message.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def message
  @message
end

#operationObject

Returns the value of attribute operation.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def operation
  @operation
end

#portObject

Returns the value of attribute port.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def port
  @port
end

#publish_keyObject

Returns the value of attribute publish_key.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def publish_key
  @publish_key
end

#responseObject

Returns the value of attribute response.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def response
  @response
end

#secret_keyObject

Returns the value of attribute secret_key.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def secret_key
  @secret_key
end

#sslObject

Returns the value of attribute ssl.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def ssl
  @ssl
end

#subscribe_keyObject

Returns the value of attribute subscribe_key.



16
17
18
# File 'lib/pubnub/request.rb', line 16

def subscribe_key
  @subscribe_key
end

#timetokenObject

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

#originObject



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

#paramsObject



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

#pathObject



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

#queryObject



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