Class: RingCentralSdk::REST::Subscription

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/ringcentral_sdk/rest/subscription.rb

Constant Summary collapse

RENEW_HANDICAP =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Subscription

Returns a new instance of Subscription.



17
18
19
20
21
22
23
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 17

def initialize(client)
  @_client = client
  @event_filters = []
  @_timeout = nil
  @_subscription = nil_subscription()
  @_pubnub = nil
end

Instance Attribute Details

#event_filtersObject (readonly)

Returns the value of attribute event_filters.



15
16
17
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 15

def event_filters
  @event_filters
end

Instance Method Details

#_clear_timeoutObject



258
259
260
261
262
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 258

def _clear_timeout()
  if @_timeout.is_a? Timers::Group
    @_timeout.cancel()
  end
end

#_decrypt(message) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 212

def _decrypt(message)
  unless alive?()
    raise 'Subscription is not alive'
  end

  if _encrypted?()
    delivery_mode = @_subscription['deliveryMode']

    cipher = OpenSSL::Cipher::AES.new(128, :ECB)
    cipher.decrypt
    cipher.key = Base64.decode64(delivery_mode['encryptionKey'].to_s)

    ciphertext = Base64.decode64(message)
    plaintext = cipher.update(ciphertext) + cipher.final

    message = MultiJson.decode(plaintext, :symbolize_keys=>false)
  end

  return message
end

#_encrypted?Boolean

Returns:

  • (Boolean)


233
234
235
236
237
238
239
240
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 233

def _encrypted?()
  delivery_mode = @_subscription['deliveryMode']
  is_encrypted  = delivery_mode.has_key?('encryption') && \
    delivery_mode['encryption']                        && \
    delivery_mode.has_key?('encryptionKey')            && \
    delivery_mode['encryptionKey']
  return is_encrypted
end

#_notify(message) ⇒ Object



206
207
208
209
210
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 206

def _notify(message)
  message = _decrypt(message)
  changed
  notify_observers(message)
end

#_set_timeoutObject



250
251
252
253
254
255
256
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 250

def _set_timeout()
  time_to_expiration = @_subscription['expiresIn'] - RENEW_HANDICAP
  @_timeout = Timers::Group.new
  @_timeout.after(time_to_expiration) do
    renew()
  end
end

#_subscribe_at_pubnubObject



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
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 180

def ()
  if ! alive?()
    raise 'Subscription is not alive'
  end

  s_key = @_subscription['deliveryMode']['subscriberKey']

  @_pubnub = new_pubnub(s_key, false, '')

  callback = lambda { |envelope|
    _notify(envelope.msg)
    changed
    #notify_observers {}
    #notify_observers('GOT_PUBNUB_MESSAGE_NOTIFY')
  }

  @_pubnub.subscribe(
    channel:    @_subscription['deliveryMode']['address'],
    callback:   callback,
    error:      lambda { |envelope| puts('ERROR: ' + envelope.msg.to_s) },
    connect:    lambda { |envelope| puts('CONNECTED') },
    reconnect:  lambda { |envelope| puts('RECONNECTED') },
    disconnect: lambda { |envelope| puts('DISCONNECTED') }
  )
end

#_unsubscribe_at_pubnubObject



242
243
244
245
246
247
248
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 242

def _unsubscribe_at_pubnub()
  if @_pubnub && alive?()
    @_pubnub.unsubscribe(channel: @_subscription['deliveryMode']['address']) do |envelope|
      # puts envelope.message
    end
  end
end

#add_events(events) ⇒ Object



53
54
55
56
57
58
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 53

def add_events(events)
  unless events.is_a?(Array)
    raise 'Events is not an array.'
  end
  @event_filters.push(events) if events.length > 0
end

#alive?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 150

def alive?()
  s = @_subscription
  return (s.has_key?('deliveryMode') && s['deliveryMode']) && \
    (s['deliveryMode'].has_key?('subscriberKey') && s['deliveryMode']['subscriberKey']) && \
    (
      s['deliveryMode'].has_key?('address') && s['deliveryMode']['address'] && \
      s['deliveryMode']['address'].length>0) \
    ? true : false
end

#destroyObject



176
177
178
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 176

def destroy()
  reset()
end

#new_pubnub(subscribe_key = '', ssl_on = false, publish_key = '', my_logger = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 264

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

#nil_subscriptionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 25

def nil_subscription()
  subscription        =  {
    'eventFilters'    => [],
    'expirationTime'  => '', # 2014-03-12T19:54:35.613Z
    'expiresIn'       => 0,
    'deliveryMode'    => {
      'transportType' => 'PubNub',
      'encryption'    => false,
      'address'       => '',
      'subscriberKey' => '',
      'secretKey'     => ''
    },
    'id'              => '',
    'creationTime'    => '', # 2014-03-12T19:54:35.613Z
    'status'          => '', # Active
    'uri'             => ''
  }
  return subscription
end

#pubnubObject



45
46
47
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 45

def pubnub()
  return @_pubnub
end

#register(events = nil) ⇒ Object



49
50
51
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 49

def register(events = nil)
  return alive?() ? renew(events) : subscribe(events)
end

#removeObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 130

def remove()
  unless alive?()
    raise 'Subscription is not alive'
  end

  begin
    response = @_client.http.delete do |req|
      req.url 'subscription/' + @_subscription['id'].to_s
    end
    reset()
    changed
    notify_observers(response.body)
    return response
  rescue StandardError => e
    reset()
    changed
    notify_observers(e)
  end
end

#renew(events = nil) ⇒ Object



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
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 97

def renew(events = nil)
  set_events(events) if events.is_a?(Array)

  unless alive?()
    raise 'Subscription is not alive'
  end

  if !@event_filters.is_a?(Array) || @event_filters.length ==0
    raise 'Events are undefined'
  end
 
  _clear_timeout()

  begin
    response = @_client.http.put do |req|
      req.url 'subscription/' + @_subscription['id'].to_s
      req.body = {
        eventFilters: @_client.create_urls(@event_filters)
      }
    end

    set_subscription(response.body)
    changed
    notify_observers(response)
    return response
  rescue StandardError => e
    reset()
    changed
    notify_observers(e)
    raise 'Renew HTTP Request Error'
  end
end

#resetObject



170
171
172
173
174
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 170

def reset()
  _clear_timeout()
  _unsubscribe_at_pubnub()
  @_subscription = nil_subscription()
end

#set_events(events) ⇒ Object



60
61
62
63
64
65
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 60

def set_events(events)
  unless events.is_a?(Array)
    raise 'Events is not an array.'
  end
  @event_filters = events
end

#set_subscription(data) ⇒ Object



164
165
166
167
168
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 164

def set_subscription(data)
  _clear_timeout()
  @_subscription = data
  _set_timeout()
end

#subscribe(events = nil) ⇒ Object



67
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
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 67

def subscribe(events=nil)
  set_events(events) if events.is_a?(Array)

  if !@event_filters.is_a?(Array) || @event_filters.length == 0
    raise 'Events are undefined'
  end

  begin
    response = @_client.http.post do |req|
      req.url 'subscription'
      req.body = {
        eventFilters: @_client.create_urls(@event_filters),
        deliveryMode: {
          transportType: 'PubNub'
        }
      }
    end
    set_subscription(response.body)
    ()
    changed
    notify_observers(response)
    return response
  rescue StandardError => e
    reset()
    changed
    notify_observers(e)
    raise 'Subscribe HTTP Request Error: ' + e.to_s
  end
end

#subscriptionObject



160
161
162
# File 'lib/ringcentral_sdk/rest/subscription.rb', line 160

def subscription()
  return @_subscription
end