Class: IB::IBSocket

Inherits:
TCPSocket
  • Object
show all
Defined in:
lib/ib/socket.rb

Instance Method Summary collapse

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_booleanObject



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_decimalObject



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_2Object



66
67
68
# File 'lib/ib/socket.rb', line 66

def read_decimal_limit_2
  read_decimal_limit -2
end

#read_decimal_maxObject



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_hashObject

Returns loaded Hash



79
80
81
82
# File 'lib/ib/socket.rb', line 79

def read_hash
  tags = read_array { |_| [read_string, read_string] }
  tags.empty? ? Hash.new : Hash[*tags.flatten]
end

#read_intObject



28
29
30
# File 'lib/ib/socket.rb', line 28

def read_int
  self.read_string.to_i
end

#read_int_maxObject



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_stringObject



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