Class: IB::IBSocket
Instance Method Summary collapse
-
#read_array(&block) ⇒ Object
Returns loaded Array or [] if count was 0.
- #read_boolean ⇒ Object
- #read_decimal ⇒ Object
-
#read_decimal_limit(limit = -1) ⇒ Object
(also: #read_decimal_limit_1)
If received decimal is below limit (“not yet computed”), return nil.
- #read_decimal_limit_2 ⇒ Object
- #read_decimal_max ⇒ Object
-
#read_hash ⇒ Object
Returns loaded Hash.
- #read_int ⇒ Object
- #read_int_max ⇒ Object
- #read_string ⇒ Object
-
#write_data(data) ⇒ Object
send nice null terminated binary data into socket.
Instance Method Details
#read_array(&block) ⇒ Object
Returns loaded Array or [] if count was 0
73 74 75 76 |
# File 'lib/ib/socket.rb', line 73 def read_array &block count = read_int count > 0 ? Array.new(count, &block) : [] end |
#read_boolean ⇒ Object
37 38 39 40 |
# File 'lib/ib/socket.rb', line 37 def read_boolean str = self.read_string str.nil? ? false : str.to_i != 0 end |
#read_decimal ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ib/socket.rb', line 42 def read_decimal # Floating-point numbers shouldn't be used to store money... # ...but BigDecimals are too unwieldy to use in this case... maybe later # self.read_string.to_d self.read_string.to_f end |
#read_decimal_limit(limit = -1) ⇒ Object Also known as: read_decimal_limit_1
If received decimal is below limit (“not yet computed”), return nil
58 59 60 61 62 |
# File 'lib/ib/socket.rb', line 58 def read_decimal_limit limit = -1 value = self.read_decimal # limit is the "not yet computed" indicator value <= limit ? nil : value end |
#read_decimal_limit_2 ⇒ Object
66 67 68 |
# File 'lib/ib/socket.rb', line 66 def read_decimal_limit_2 read_decimal_limit -2 end |
#read_decimal_max ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ib/socket.rb', line 49 def read_decimal_max str = self.read_string # Floating-point numbers shouldn't be used to store money... # ...but BigDecimals are too unwieldy to use in this case... maybe later # str.nil? || str.empty? ? nil : str.to_d str.to_f unless str.nil? || str.empty? || str.to_f > 1.797 * 10.0 ** 306 end |
#read_hash ⇒ Object
Returns loaded Hash
79 80 81 82 |
# File 'lib/ib/socket.rb', line 79 def read_hash = read_array { |_| [read_string, read_string] } .empty? ? Hash.new : Hash[*.flatten] end |
#read_int ⇒ Object
28 29 30 |
# File 'lib/ib/socket.rb', line 28 def read_int self.read_string.to_i end |
#read_int_max ⇒ Object
32 33 34 35 |
# File 'lib/ib/socket.rb', line 32 def read_int_max str = self.read_string str.to_i unless str.nil? || str.empty? end |
#read_string ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ib/socket.rb', line 16 def read_string string = self.gets(EOL) until string # Silently ignores nils string = self.gets(EOL) sleep 0.1 end string.chop end |
#write_data(data) ⇒ Object
send nice null terminated binary data into socket
7 8 9 10 11 12 13 14 |
# File 'lib/ib/socket.rb', line 7 def write_data data # TWS wants to receive booleans as 1 or 0 data = "1" if data == true data = "0" if data == false #p data.to_s + EOL self.syswrite(data.to_s + EOL) end |