module Aerospike::Host::Parse

Constants

INTEGER_REGEX

Public Class Methods

call(hosts, default_port = 3000) click to toggle source

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
# 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) click to toggle source

Extract addr, tls_name and port components from a host strin

# 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