Class: IbRubyProxy::Client::CallbacksResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ib_ruby_proxy/client/callbacks_response_handler.rb

Overview

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

Defined Under Namespace

Classes: BlockCallbackHandler

Constant Summary collapse

IB_CALLBACKS_MAPPING =
{
  req_historical_ticks: { callbacks: %i[historical_ticks historical_ticks_bid_ask
                                        historical_ticks_last],
                          discriminate_by_argument_nth: 0 },
  req_contract_details: { callbacks: %i[contract_details contract_details_end],
                          discriminate_by_argument_nth: 0 },
  req_tick_by_tick_data: { callbacks: %i[tick_by_tick_bid_ask tick_by_tick_all_last
                                         tick_by_tick_mid_point],
                           discriminate_by_argument_nth: 0 },
  req_historical_data: { callbacks: %i[historical_data historical_data_end
                                       historical_data_update],
                         discriminate_by_argument_nth: 0 }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallbacksResponseHandler

Returns a new instance of CallbacksResponseHandler.



21
22
23
24
# File 'lib/ib_ruby_proxy/client/callbacks_response_handler.rb', line 21

def initialize
  @method_handlers = {}
  @callback_handlers = {}
end

Class Method Details

.for_ibObject



47
48
49
50
51
52
53
54
55
56
# 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

Instance Method Details

#callback_received(callback_name, *arguments) ⇒ Object

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

Parameters:

  • callback_name (Symbol)
  • arguments (Array<Object>)


41
42
43
44
# 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) ⇒ Object

Configures a mapping between a method invocation and a received callback

Parameters:

  • method (String, Symbol)
  • callbacks (String, Symbol)
  • discriminate_by_argument_nth (Integer, nil) (defaults to: nil)

    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)



65
66
67
68
69
70
71
72
# 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) ⇒ Object

Handle invoked method

Parameters:

  • method_name (String, Symbol)
  • arguments (Array<Object>)
  • block (Proc)


31
32
33
34
# 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