Module: DomainExtractor::Validators

Defined in:
lib/domain_extractor/validators.rb

Overview

Validators hosts fast checks for excluding unsupported hostnames (e.g. IP addresses).

Constant Summary collapse

IPV4_SEGMENT =

Frozen regex patterns for zero allocation

'(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)'
IPV4_REGEX =
/\A#{IPV4_SEGMENT}(?:\.#{IPV4_SEGMENT}){3}\z/
IPV6_REGEX =
/\A\[?[0-9a-fA-F:]+\]?\z/
HOSTNAME_REGEX =

Valid hostname pattern (RFC 1123) Allows: letters, numbers, hyphens, dots Must start with alphanumeric, can contain hyphens, must end with alphanumeric

/\A[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*\z/i
DOT =

Frozen string constants

'.'
COLON =
':'
BRACKET_OPEN =
'['

Class Method Summary collapse

Class Method Details

.ip_address?(host) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/domain_extractor/validators.rb', line 23

def ip_address?(host)
  return false if host.nil? || host.empty?

  # Fast path: check for dot or colon before running regex
  if host.include?(DOT)
    IPV4_REGEX.match?(host)
  elsif host.include?(COLON) || host.include?(BRACKET_OPEN)
    IPV6_REGEX.match?(host)
  else
    false
  end
end

.valid_hostname?(host) ⇒ Boolean

Check if a string is a valid hostname

Parameters:

  • host (String)

    The hostname to validate

Returns:

  • (Boolean)

    True if valid hostname



39
40
41
42
43
44
# File 'lib/domain_extractor/validators.rb', line 39

def valid_hostname?(host)
  return false if host.nil? || host.empty?
  return false if host.length > 253 # Max hostname length

  HOSTNAME_REGEX.match?(host)
end