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



68
69
70
71
# File 'lib/ib/socket.rb', line 68

def read_array &block
  count = read_int
  count > 0 ? Array.new(count, &block) : []
end

#read_booleanObject



32
33
34
35
# File 'lib/ib/socket.rb', line 32

def read_boolean
  str = self.read_string
  str.nil? ? false : str.to_i != 0
end

#read_decimalObject



37
38
39
40
41
42
# File 'lib/ib/socket.rb', line 37

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



53
54
55
56
57
# File 'lib/ib/socket.rb', line 53

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



61
62
63
# File 'lib/ib/socket.rb', line 61

def read_decimal_limit_2
  read_decimal_limit -2
end

#read_decimal_maxObject



44
45
46
47
48
49
50
# File 'lib/ib/socket.rb', line 44

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



74
75
76
77
# File 'lib/ib/socket.rb', line 74

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

#read_intObject



23
24
25
# File 'lib/ib/socket.rb', line 23

def read_int
  self.read_string.to_i
end

#read_int_maxObject



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

def read_int_max
  str = self.read_string
  str.to_i unless str.nil? || str.empty?
end

#read_stringObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ib/socket.rb', line 11

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

Sends null terminated data string into socket



7
8
9
# File 'lib/ib/socket.rb', line 7

def write_data data
  self.syswrite(data.to_s + EOL)
end