Module: Aerospike::Host::Parse

Defined in:
lib/aerospike/host/parse.rb

Constant Summary collapse

INTEGER_REGEX =
/\A\d+\z/.freeze

Class Method Summary collapse

Class Method Details

.call(hosts, default_port = 3000) ⇒ Object

Parse hosts from string format: hostname1[:port1],…

Hostname may also be an IP address in the following formats:
- xxx.xxx.xxx.xxx
- [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]
- [xxxx::xxxx]


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aerospike/host/parse.rb', line 34

def call(hosts, default_port = 3000)
  case hosts
  when Host
    [hosts]
  when Array
    hosts
  when String
    hosts.split(?,).map { |host|
      addr, tls_name, port = components(host)
      if port.nil? && tls_name && tls_name.match(INTEGER_REGEX)
        port = tls_name
        tls_name = nil
      end
      port ||= default_port
      Host.new(addr, port.to_i, tls_name)
    }
  else
    fail TypeError, "hosts should be a Host object, an Array of Host objects, or a String"
  end
end

.components(host_string) ⇒ Object

Extract addr, tls_name and port components from a host strin



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aerospike/host/parse.rb', line 56

def components(host_string)
  host_string = host_string.strip

  # IPv6
  if host_string.start_with?('[')
    end_idx = host_string.index(']')
    raise ::Aerospike::Exceptions::Parse, 'Invalid IPv6 host' if end_idx.nil?

    # Slice away brackets and what's inside them, then split on : and
    # replace first entry with string inside brackets
    host_string.slice(end_idx+1..-1).split(':').tap do |result|
      result[0] = host_string[1...end_idx]
    end
  else
    host_string.split(?:)
  end
end