Class: ScaleRb::ResponseHandler

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

Instance Method Summary collapse

Constructor Details

#initializeResponseHandler

Returns a new instance of ResponseHandler.



148
149
150
# File 'lib/scale_rb/client/ws_client.rb', line 148

def initialize
  @callbacks = {}
end

Instance Method Details

#handle(response) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/scale_rb/client/ws_client.rb', line 157

def handle(response)
  id = response[:id]
  if @callbacks.key?(id)
    callback = @callbacks[id]
    callback.call(response)
    @callbacks.delete(id)
  else
    ScaleRb.logger.info "Received a message with unknown id: #{response}"
  end
end

#register(id, callback) ⇒ Object

callback: a proc with response data as param



153
154
155
# File 'lib/scale_rb/client/ws_client.rb', line 153

def register(id, callback)
  @callbacks[id] = callback
end