class IbRubyProxy::Client::CallbacksResponseHandler

This class maps callbacks with method invocations that originated them so that a block can be passed to the api method and it will be invoked when a callback is received

Constants

IB_CALLBACKS_MAPPING

Attributes

callback_handlers[R]
method_handlers[R]

Public Class Methods

for_ib() click to toggle source

@private

# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 47
def self.for_ib
  new.tap do |handler|
    IbRubyProxy.config['mapped_callbacks'].each do |method, callback_config|
      nth_argument = callback_config['discriminate_by_argument_nth']
      handler.configure_block_callback method: method.to_sym,
                                       callbacks: callback_config['callbacks'],
                                       discriminate_by_argument_nth: nth_argument
    end
  end
end
new() click to toggle source
# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 21
def initialize
  @method_handlers = {}
  @callback_handlers = {}
end

Public Instance Methods

callback_received(callback_name, *arguments) click to toggle source

Handle callback received: it will invoke the block passed in the corresponding {#method_invoked}, if any.

@param [Symbol] callback_name @param [Array<Object>] arguments

# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 41
def callback_received(callback_name, *arguments)
  callback_name = callback_name.to_sym
  callback_handlers[callback_name]&.callback_received(callback_name, *arguments)
end
configure_block_callback(method:, callbacks:, discriminate_by_argument_nth: nil) click to toggle source

Configures a mapping between a method invocation and a received callback

@param [String, Symbol] method @param [String, Symbol] callbacks @param [Integer, nil] discriminate_by_argument_nth The position of the argument that

will be used to discriminate received callbacks and match them with invocation methods.
+nil+ indicates no argument should be used for discriminating (default)
# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 65
def configure_block_callback(method:, callbacks:, discriminate_by_argument_nth: nil)
  validate_can_add_callback_on_method!(method)

  handler = BlockCallbackHandler.new(discriminate_by_argument_nth)

  configure_callbacks_handler(callbacks, handler)
  configure_method_handler(method, handler)
end
method_invoked(method_name, *arguments, &block) click to toggle source

Handle invoked method

@param [String, Symbol] method_name @param [Array<Object>] arguments @param [Proc] block

# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 31
def method_invoked(method_name, *arguments, &block)
  method_name = method_name.to_sym
  method_handlers[method_name]&.method_invoked(*arguments, &block)
end

Private Instance Methods

configure_callbacks_handler(callbacks, handler) click to toggle source
# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 82
def configure_callbacks_handler(callbacks, handler)
  callbacks << :error
  callbacks.each do |callback_name|
    callback_handlers[callback_name.to_sym] = handler
  end
end
configure_method_handler(method, handler) click to toggle source
# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 78
def configure_method_handler(method, handler)
  method_handlers[method.to_sym] = handler
end
validate_can_add_callback_on_method!(method) click to toggle source
# File lib/ib_ruby_proxy/client/callbacks_response_handler.rb, line 89
def validate_can_add_callback_on_method!(method)
  raise "Already configured handler for #{method}" if method_handlers[method]
end