Class: PubnubRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/pubnub_request.rb

Defined Under Namespace

Classes: RequestError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#callbackObject

Returns the value of attribute callback.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def callback
  @callback
end

#channelObject

Returns the value of attribute channel.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def channel
  @channel
end

#cipher_keyObject

Returns the value of attribute cipher_key.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def cipher_key
  @cipher_key
end

#errorObject

Returns the value of attribute error.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def error
  @error
end

#history_countObject

Returns the value of attribute history_count.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def history_count
  @history_count
end

#history_endObject

Returns the value of attribute history_end.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def history_end
  @history_end
end

#history_limitObject

Returns the value of attribute history_limit.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def history_limit
  @history_limit
end

#history_reverseObject

Returns the value of attribute history_reverse.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def history_reverse
  @history_reverse
end

#history_startObject

Returns the value of attribute history_start.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def history_start
  @history_start
end

#hostObject

Returns the value of attribute host.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def host
  @host
end

#jsonpObject

Returns the value of attribute jsonp.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def jsonp
  @jsonp
end

#last_timetokenObject

Returns the value of attribute last_timetoken.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def last_timetoken
  @last_timetoken
end

#messageObject

Returns the value of attribute message.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def message
  @message
end

#operationObject

Returns the value of attribute operation.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def operation
  @operation
end

#originObject

Returns the value of attribute origin.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def origin
  @origin
end

#portObject

Returns the value of attribute port.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def port
  @port
end

#publish_keyObject

Returns the value of attribute publish_key.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def publish_key
  @publish_key
end

#queryObject

Returns the value of attribute query.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def query
  @query
end

#responseObject

Returns the value of attribute response.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def response
  @response
end

#secret_keyObject

Returns the value of attribute secret_key.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def secret_key
  @secret_key
end

#session_uuidObject

Returns the value of attribute session_uuid.



3
4
5
# File 'lib/pubnub_request.rb', line 3

def session_uuid
  @session_uuid
end

#sslObject

Returns the value of attribute ssl.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def ssl
  @ssl
end

#subscribe_keyObject

Returns the value of attribute subscribe_key.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def subscribe_key
  @subscribe_key
end

#timetokenObject

Returns the value of attribute timetoken.



2
3
4
# File 'lib/pubnub_request.rb', line 2

def timetoken
  @timetoken
end

#urlObject

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.message == another.message
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, options, publish_request)
  options = HashWithIndifferentAccess.new(options)

  pc = PubnubCrypto.new(cipher_key)
  publish_request.message = pc.encrypt(options[: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

Returns:

  • (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.message]

    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_exceptionObject



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 |message|
      message = pc.decrypt(message)
      myarr.push(message)
    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(options)
  options = HashWithIndifferentAccess.new(options)

  if options[:callback].blank?
    raise(op_exception, "callback is a required parameter.")
  elsif !options[:callback].try(:respond_to?, "call")
    raise(op_exception, "callback is invalid.")
  else
    self.callback = options[: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(options)
  options = HashWithIndifferentAccess.new(options)

  if options[:channel].blank?
    raise(op_exception, "channel is a required parameter.")
  else
    self.channel = options[: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(options, self_cipher_key)
  options = HashWithIndifferentAccess.new(options)

  if self_cipher_key.present? && options['cipher_key'].present?
    raise(op_exception, "existing cipher_key #{self_cipher_key} cannot be overridden at publish-time.")

  elsif (self_cipher_key.present? && options[:cipher_key].blank?) || (self_cipher_key.blank? && options[:cipher_key].present?)

    this_cipher_key = self_cipher_key || options[: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(options)
  options = HashWithIndifferentAccess.new(options)

  if options[: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 set_message(options, self_cipher_key)
  options = HashWithIndifferentAccess.new(options)

  if options[:message].blank? && options[:message] != ""
    raise(op_exception, "message is a required parameter.")
  else
    my_cipher_key = options[:cipher_key] || self_cipher_key

    if my_cipher_key.present?
      self.message = Yajl.dump(aes_encrypt(my_cipher_key, options, self))
    else
      self.message = Yajl.dump(options[: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(options)
  options = HashWithIndifferentAccess.new(options)

  if options[:origin].present?
    self.origin = options[: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(options, self_publish_key)
  options = HashWithIndifferentAccess.new(options)

  if options[:publish_key].blank? && self_publish_key.blank?
    raise(Pubnub::PublishError, "publish_key is a required parameter.")
  elsif self_publish_key.present? && options['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 || options[: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(options, self_secret_key)
  options = HashWithIndifferentAccess.new(options)

  if self_secret_key.present? && options['secret_key'].present?
    raise(Pubnub::PublishError, "existing secret_key #{self_secret_key} cannot be overridden at publish-time.")

  elsif (self_secret_key.present? && options[:secret_key].blank?) || (self_secret_key.blank? && options[:secret_key].present?)

    my_secret_key = self_secret_key || options[: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(options, self_subscribe_key)
  options = HashWithIndifferentAccess.new(options)

  if options[:subscribe_key].blank? && self_subscribe_key.blank?
    raise(op_exception, "subscribe_key is a required parameter.")
  elsif self_subscribe_key.present? && options['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 || options[:subscribe_key]).to_s
  end
end