Class: ScaleRb::SubscriptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/scale_rb/client/ws_client.rb

Instance Method Summary collapse

Constructor Details

#initializeSubscriptionHandler

Returns a new instance of SubscriptionHandler.



170
171
172
# File 'lib/scale_rb/client/ws_client.rb', line 170

def initialize
  @callbacks = {}
end

Instance Method Details

#handle(notification) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/scale_rb/client/ws_client.rb', line 182

def handle(notification)
  subscription_id = notification.dig(:params, :subscription)
  return if subscription_id.nil?

  return unless @callbacks.key?(subscription_id)

  @callbacks[subscription_id].call(notification)
end

#subscribe(subscription_id, callback) ⇒ Object



174
175
176
# File 'lib/scale_rb/client/ws_client.rb', line 174

def subscribe(subscription_id, callback)
  @callbacks[subscription_id] = callback
end

#unsubscribe(subscription_id) ⇒ Object



178
179
180
# File 'lib/scale_rb/client/ws_client.rb', line 178

def unsubscribe(subscription_id)
  @callbacks.delete(subscription_id)
end