Class: Pubnub

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

Defined Under Namespace

Classes: InitError, PresenceError, PublishError, SubscribeError

Constant Summary collapse

SUCCESS_RESPONSE =
200
MSG_TOO_LARGE_RESPONSE =
400
TIMEOUT_BAD_RESPONSE_CODE =
1
TIMEOUT_BAD_JSON_RESPONSE =
0.5
TIMEOUT_GENERAL_ERROR =
1
TIMEOUT_SUBSCRIBE =
310
TIMEOUT_NON_SUBSCRIBE =
5
ORIGIN_HOST =
'pubsub.pubnub.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Pubnub

ORIGIN_HOST = ‘test.pubnub.com’



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pubnub.rb', line 55

def initialize(*args)

  if args.size == 5 # passing in named parameters

    @publish_key = args[0].to_s
    @subscribe_key = args[1].to_s
    @secret_key = args[2].to_s
    @cipher_key = args[3].to_s
    @ssl = args[4]

  elsif args.size == 1 && args[0].class == Hash # passing in an options hash

    options_hash = HashWithIndifferentAccess.new(args[0])
    @publish_key = options_hash[:publish_key].blank? ? nil : options_hash[:publish_key].to_s
    @subscribe_key = options_hash[:subscribe_key].blank? ? nil : options_hash[:subscribe_key].to_s
    @secret_key = options_hash[:secret_key].blank? ? nil : options_hash[:secret_key].to_s
    @cipher_key = options_hash[:cipher_key].blank? ? nil : options_hash[:cipher_key].to_s
    @ssl = options_hash[:ssl].blank? ? false : true
    @logger = options_hash[:logger]

  else
    raise(InitError, "Initialize with either a hash of options, or exactly 5 named parameters.")
  end

  @session_uuid = uuid
  verify_init
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



50
51
52
# File 'lib/pubnub.rb', line 50

def channel
  @channel
end

#cipher_keyObject

Returns the value of attribute cipher_key.



50
51
52
# File 'lib/pubnub.rb', line 50

def cipher_key
  @cipher_key
end

#originObject

Returns the value of attribute origin.



50
51
52
# File 'lib/pubnub.rb', line 50

def origin
  @origin
end

#publish_keyObject

Returns the value of attribute publish_key.



50
51
52
# File 'lib/pubnub.rb', line 50

def publish_key
  @publish_key
end

#secret_keyObject

Returns the value of attribute secret_key.



50
51
52
# File 'lib/pubnub.rb', line 50

def secret_key
  @secret_key
end

#session_uuidObject

Returns the value of attribute session_uuid.



50
51
52
# File 'lib/pubnub.rb', line 50

def session_uuid
  @session_uuid
end

#sslObject

Returns the value of attribute ssl.



50
51
52
# File 'lib/pubnub.rb', line 50

def ssl
  @ssl
end

#subscribe_keyObject

Returns the value of attribute subscribe_key.



50
51
52
# File 'lib/pubnub.rb', line 50

def subscribe_key
  @subscribe_key
end

Instance Method Details

#check_for_em(request) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/pubnub.rb', line 269

def check_for_em request
  if EM.reactor_running?
    _request(request, true)
  else
    EM.run do
      _request(request)
    end
  end
end

#detailed_history(options = nil) ⇒ Object



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
211
# File 'lib/pubnub.rb', line 177

def detailed_history(options = nil)
  usage_error = "detailed_history() requires :channel, :callback, and :count options."
  if options.class != Hash
    raise(ArgumentError, usage_error)
  end

  options = HashWithIndifferentAccess.new(options) unless (options == nil)

  unless options[:count] && options[:channel] && options[:callback]
    raise(ArgumentError, usage_error)
  end


  detailed_history_request = PubnubRequest.new(:operation => :detailed_history)

  #TODO: refactor into initializer code on request instantiation

  # /detailed_history/SUBSCRIBE_KEY/CHANNEL/JSONP_CALLBACK/LIMIT

  detailed_history_request.ssl = @ssl
  detailed_history_request.set_channel(options)
  detailed_history_request.set_callback(options)
  detailed_history_request.set_cipher_key(options, self.cipher_key)

  detailed_history_request.set_subscribe_key(options, self.subscribe_key)

  detailed_history_request.history_count = options[:count]
  detailed_history_request.history_start = options[:start]
  detailed_history_request.history_end = options[:end]
  detailed_history_request.history_reverse = options[:reverse]

  detailed_history_request.format_url!
  check_for_em detailed_history_request

end

#here_now(options = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pubnub.rb', line 152

def here_now(options = nil)
  usage_error = "here_now() requires :channel and :callback options."
  if options.class != Hash
    raise(ArgumentError, usage_error)
  end

  options = HashWithIndifferentAccess.new(options) unless (options == nil)

  unless options[:channel] && options[:callback]
    raise(ArgumentError, usage_error)
  end

  here_now_request = PubnubRequest.new(:operation => :here_now)

  here_now_request.ssl = @ssl
  here_now_request.set_channel(options)
  here_now_request.set_callback(options)

  here_now_request.set_subscribe_key(options, self.subscribe_key)

  here_now_request.format_url!
  check_for_em here_now_request

end

#history(options = nil) ⇒ Object



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
# File 'lib/pubnub.rb', line 213

def history(options = nil)
  usage_error = "history() requires :channel, :callback, and :limit options."
  if options.class != Hash
    raise(ArgumentError, usage_error)
  end

  options = HashWithIndifferentAccess.new(options) unless (options == nil)

  unless options[:limit] && options[:channel] && options[:callback]
    raise(ArgumentError, usage_error)
  end


  history_request = PubnubRequest.new(:operation => :history)

  #TODO: refactor into initializer code on request instantiation

  # /history/SUBSCRIBE_KEY/CHANNEL/JSONP_CALLBACK/LIMIT

  history_request.ssl = @ssl
  history_request.set_channel(options)
  history_request.set_callback(options)
  history_request.set_cipher_key(options, self.cipher_key)

  history_request.set_subscribe_key(options, self.subscribe_key)
  history_request.history_limit = options[:limit]

  history_request.format_url!
  check_for_em history_request

end

#my_callback(x, quiet = false) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/pubnub.rb', line 256

def my_callback(x, quiet = false)
  if quiet !=false
    puts("mycallback says: #{x.to_s}")
  else
    ""
  end

end

#presence(options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pubnub.rb', line 135

def presence(options)
  usage_error = "presence() requires :channel and :callback options."
  if options.class != Hash
    raise(ArgumentError, usage_error)
  end

  options = HashWithIndifferentAccess.new(options) unless (options == nil)

  unless options[:channel] && options[:callback]
    raise(ArgumentError, usage_error)
  end

  subscribe(options.merge(:operation => "presence"))

end

#publish(options) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pubnub.rb', line 89

def publish(options)
  options = HashWithIndifferentAccess.new(options)
  publish_request = PubnubRequest.new(:operation => :publish)

  #TODO: refactor into initializer code on request instantiation

  publish_request.ssl = @ssl
  publish_request.set_origin(options)
  publish_request.set_channel(options)
  publish_request.set_callback(options)
  publish_request.set_cipher_key(options, self.cipher_key)
  publish_request.set_message(options, self.cipher_key)
  publish_request.set_publish_key(options, self.publish_key)
  publish_request.set_subscribe_key(options, self.subscribe_key)
  publish_request.set_secret_key(options, self.secret_key)


  publish_request.format_url!

  check_for_em publish_request
end

#subscribe(options) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pubnub.rb', line 111

def subscribe(options)
  options = HashWithIndifferentAccess.new(options)

  operation = options[:operation].nil? ? :subscribe : :presence

  subscribe_request = PubnubRequest.new(:operation => operation, :session_uuid => @session_uuid)

  #TODO: refactor into initializer code on request instantiation

  subscribe_request.ssl = @ssl
  subscribe_request.set_origin(options)
  subscribe_request.set_channel(options)
  subscribe_request.set_callback(options)
  subscribe_request.set_cipher_key(options, self.cipher_key) unless subscribe_request.operation == "presence"

  subscribe_request.set_subscribe_key(options, self.subscribe_key)

  format_url_options = options[:override_timetoken].present? ? options[:override_timetoken] : nil
  subscribe_request.format_url!(format_url_options)

  check_for_em subscribe_request

end

#time(options) ⇒ Object

Raises:

  • (PubNubRuntimeError)


245
246
247
248
249
250
251
252
253
254
# File 'lib/pubnub.rb', line 245

def time(options)
  options = HashWithIndifferentAccess.new(options)
  raise(PubNubRuntimeError, "You must supply a callback.") if options['callback'].blank?

  time_request = PubnubRequest.new(:operation => :time)
  time_request.set_callback(options)

  time_request.format_url!
  check_for_em time_request
end

#uuidObject



265
266
267
# File 'lib/pubnub.rb', line 265

def uuid
  UUID.new.generate
end

#verify_initObject

Raises:



83
84
85
86
# File 'lib/pubnub.rb', line 83

def verify_init
  # publish_key and cipher_key are both optional.
  raise(InitError, "subscribe_key is a mandatory parameter.") if @subscribe_key.blank?
end