Module: DomainExtractor::Normalizer

Defined in:
lib/domain_extractor/normalizer.rb

Overview

Normalizer ensures URLs include a scheme and removes extraneous whitespace before passing them into the URI parser.

Constant Summary collapse

SCHEME_PATTERN =

Frozen constants for zero allocation

%r{\A[A-Za-z][A-Za-z0-9+\-.]*://}
HTTPS_SCHEME =
'https://'
HTTP_SCHEME =
'http://'

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/domain_extractor/normalizer.rb', line 14

def call(input)
  return if input.nil?

  string = coerce_to_string(input)
  return if string.empty?

  # Fast path: check if already has http or https scheme
  return string if string.start_with?(HTTPS_SCHEME, HTTP_SCHEME)

  # Check for any scheme
  string.match?(SCHEME_PATTERN) ? string : HTTPS_SCHEME + string
end