lib/ib-ruby/messages/incoming.rb in ib-ruby-0.5.15 vs lib/ib-ruby/messages/incoming.rb in ib-ruby-0.5.16

- old
+ new

@@ -48,12 +48,33 @@ def to_human self.inspect end + # Object#id is always defined, we cannot rely on method_missing + def id + @data.has_key?(:id) ? @data[:id] : super + end + protected + def method_missing method, *args + getter = method.to_s.sub(/=$/, '').to_sym + if @data.has_key? method + @data[method] + elsif @data.has_key? getter + @data[getter] = *args + else + super method, *args + end + end + + def respond_to? method + getter = method.to_s.sub(/=$/, '').to_sym + @data.has_key?(method) || @data.has_key?(getter) || super + end + # Every message loads received message version first def load @data[:version] = @socket.read_int end @@ -155,11 +176,11 @@ end # This message is always sent by TWS automatically at connect. # The IB::Connection class subscribes to it automatically and stores # the order id in its @next_order_id attribute. - NextValidID = def_message 9, [:id, :int] + NextValidID = def_message(9, [:id, :int]) { "<NextValidID: #{@data[:id]}>" } NewsBulletins = def_message 14, [:id, :int], # unique incrementing bulletin ID. [:type, :int], # Type of bulletin. Valid values include: # 1 = Regular news bulletin @@ -191,16 +212,19 @@ # Receive Reuters global fundamental market data. There must be a subscription to # Reuters Fundamental set up in Account Management before you can receive this data. FundamentalData = def_message 50, [:id, :int], # request_id [:data, :string] - ContractDataEnd = def_message 52, [:id, :int] # request_id + ContractDataEnd = def_message(52, [:id, :int]) { "<ContractDataEnd: #{@data[:id]}>" } # request_id - OpenOrderEnd = def_message 53 + OpenOrderEnd = def_message(53) { "<OpenOrderEnd>" } - AccountDownloadEnd = def_message 54, [:account_name, :string] + AccountDownloadEnd = def_message(54, [:account_name, :string]) do + "<AccountDownloadEnd: #{@data[:account_name]}}>" + end # request_id + ExecutionDataEnd = def_message 55, [:id, :int] # request_id TickSnapshotEnd = def_message 57, [:id, :int] # request_id ### Actual message classes (long definitions): @@ -370,18 +394,10 @@ # Called Error in Java code, but in fact this type of messages also # deliver system alerts and additional (non-error) info from TWS. # It has additional accessors: #code and #message, derived from @data Alert = def_message 4, [:id, :int], [:code, :int], [:message, :string] class Alert - def code - @data && @data[:code] - end - - def message - @data && @data[:message] - end - # Is it an Error message? def error? code < 1000 end @@ -395,11 +411,11 @@ code > 2000 end def to_human "TWS #{ error? ? 'Error' : system? ? 'System' : 'Warning' - } Message #{@data[:code]}: #{@data[:message]}" + } Message #{code}: #{message}" end end # class ErrorMessage Error = Alert ErrorMessage = Alert @@ -550,17 +566,17 @@ load_map [:position, :int], [:market_price, :decimal], [:market_value, :decimal], [:average_cost, :decimal], [:unrealized_pnl, :decimal], # TODO: Check for Double.MAX_VALUE - [:realized_pnl, :decimal], # TODO: Check for Double.MAX_VALUE + [:realized_pnl, :decimal], # TODO: Check for Double.MAX_VALUE [:account_name, :string] end def to_human - "<PortfolioValue: #{@contract.to_human} (#{@data[:position]}): Market #{@data[:market_price]}" + - " price #{@data[:market_value]} value; PnL: #{@data[:unrealized_pnl]} unrealized," + - " #{@data[:realized_pnl]} realized; account #{@data[:account_name]}>" + "<PortfolioValue: #{@contract.to_human} (#{position}): Market #{market_price}" + + " price #{market_value} value; PnL: #{unrealized_pnl} unrealized," + + " #{realized_pnl} realized; account #{account_name}>" end end # PortfolioValue class ContractData < AbstractMessage