lib/ringcentral_sdk/subscription.rb in ringcentral_sdk-0.4.0 vs lib/ringcentral_sdk/subscription.rb in ringcentral_sdk-0.5.0
- old
+ new
@@ -1,21 +1,23 @@
require 'base64'
+require 'logger'
require 'multi_json'
require 'observer'
+require 'openssl'
+require 'pubnub'
require 'timers'
module RingCentralSdk
class Subscription
include Observable
RENEW_HANDICAP = 60
attr_reader :event_filters
- def initialize(platform, pubnub_factory)
+ def initialize(platform)
@_platform = platform
- @_pubnub_factory = pubnub_factory
@event_filters = []
@_timeout = nil
@_subscription = nil_subscription()
@_pubnub = nil
end
@@ -71,11 +73,11 @@
begin
response = @_platform.client.post do |req|
req.url 'subscription'
req.body = {
- :eventFilters => _get_full_events_filter(),
+ :eventFilters => @_platform.create_urls(@event_filters),
:deliveryMode => {
:transportType => 'PubNub'
}
}
end
@@ -89,11 +91,10 @@
changed
notify_observers(e)
raise 'Subscribe HTTP Request Error'
end
- return nil
end
def renew(events=nil)
set_events(events) if events.is_a?(Array)
@@ -107,13 +108,13 @@
_clear_timeout()
begin
response = @_platform.client.put do |req|
- req.url 'subscription' + @_subscription['id']
+ req.url 'subscription/' + @_subscription['id'].to_s
req.body = {
- :eventFilters => _get_full_events_filter()
+ :eventFilters => @_platform.create_urls(@event_filters),
}
end
set_subscription(response.body)
changed
@@ -131,10 +132,11 @@
unless alive?()
raise 'Subscription is not alive'
end
begin
+ url = 'subscription/' + @_subscription['id'].to_s
response = @_platform.client.delete do |req|
req.url = 'subscription' + @_subscription['id']
end
reset()
changed
@@ -181,12 +183,13 @@
if ! alive?()
raise 'Subscription is not alive'
end
s_key = @_subscription['deliveryMode']['subscriberKey']
- @_pubnub = @_pubnub_factory.pubnub(s_key, false, '')
+ @_pubnub = new_pubnub(s_key, false, '')
+
callback = lambda { |envelope|
_notify(envelope.msg)
changed
notify_observers('GOT_PUBNUB_MESSAGE_NOTIFY')
}
@@ -212,18 +215,17 @@
raise 'Subscription is not alive'
end
if _encrypted?()
delivery_mode = @_subscription['deliveryMode']
- key = Base64.decode64(delivery_mode['encryptionKey'])
- ciphertext = Base64.decode64(message)
- decipher = OpenSSL::Cipher::AES.new(128, :ECB)
- decipher.decrypt
- decipher.key = key
+ cipher = OpenSSL::Cipher::AES.new(128, :ECB)
+ cipher.decrypt
+ cipher.key = Base64.decode64(delivery_mode['encryptionKey'].to_s)
- plaintext = decipher.update(ciphertext) + decipher.final
+ ciphertext = Base64.decode64(message)
+ plaintext = cipher.update(ciphertext) + cipher.final
message = MultiJson.decode(plaintext)
end
return message
@@ -242,20 +244,10 @@
if @_pubnub && alive?()
@_pubnub.unsubscribe(@_subscription['deliveryMode']['address'])
end
end
- def _get_full_events_filter()
- full_events_filter = []
- @event_filters.each do |filter|
- if filter.to_s
- full_events_filter.push(@_platform.create_url(filter.to_s))
- end
- end
- return full_events_filter
- end
-
def _set_timeout()
time_to_expiration = @_subscription['expiresIn'] - RENEW_HANDICAP
@_timeout = Timers::Group.new
@_timeout.after(time_to_expiration) do
renew()
@@ -264,9 +256,25 @@
def _clear_timeout()
if @_timeout.is_a?(Timers::Group)
@_timeout.cancel()
end
+ end
+
+ def new_pubnub(subscribe_key='', ssl_on=false, publish_key='', my_logger=nil)
+ my_logger = Logger.new(STDOUT) if my_logger.nil?
+
+ return Pubnub.new(
+ :subscribe_key => subscribe_key.to_s,
+ :publish_key => publish_key.to_s,
+ :error_callback => lambda { |msg|
+ puts "Error callback says: #{msg.inspect}"
+ },
+ :connect_callback => lambda { |msg|
+ puts "CONNECTED: #{msg.inspect}"
+ },
+ :logger => my_logger
+ )
end
end
end
\ No newline at end of file