Module: DomainExtractor

Defined in:
lib/domain_extractor.rb,
lib/domain_extractor/auth.rb,
lib/domain_extractor/errors.rb,
lib/domain_extractor/parser.rb,
lib/domain_extractor/result.rb,
lib/domain_extractor/version.rb,
lib/domain_extractor/formatter.rb,
lib/domain_extractor/normalizer.rb,
lib/domain_extractor/parsed_url.rb,
lib/domain_extractor/validators.rb,
lib/domain_extractor/uri_helpers.rb,
lib/domain_extractor/query_params.rb,
lib/domain_extractor/domain_validator.rb

Overview

Also register in DomainExtractor namespace for backwards compatibility

Defined Under Namespace

Modules: Auth, Formatter, Normalizer, Parser, QueryParams, Result, URIHelpers, Validators Classes: InvalidURLError, ParsedURL

Constant Summary collapse

VERSION =
'0.2.9'
DomainValidator =

DomainValidator is now defined at the top level for Rails autoloading. This constant provides a reference for explicit usage.

Validation modes:

  • :standard - Validates any valid URL using DomainExtractor.valid?

  • :root_domain - Only allows root domains (no subdomains) like mysite.com

  • :root_or_custom_subdomain - Allows root or custom subdomains, but excludes ‘www’

Optional flags:

  • use_protocol (default: true) - Whether protocol (http/https) is required

  • use_https (default: true) - Whether https is required (only if use_protocol is true)

Examples:

Standard validation

validates :url, domain: { validation: :standard }

Root domain only, no protocol required

validates :url, domain: { validation: :root_domain, use_protocol: false }
::DomainValidator

Class Method Summary collapse

Class Method Details

.basic_auth_header(username, password) ⇒ String

Generate Basic Authentication header

Parameters:

  • username (String)

    The username

  • password (String)

    The password

Returns:

  • (String)

    The Authorization header value



107
108
109
# File 'lib/domain_extractor.rb', line 107

def basic_auth_header(username, password)
  URIHelpers.basic_auth_header(username, password)
end

.bearer_auth_header(token) ⇒ String

Generate Bearer token header

Parameters:

  • token (String)

    The bearer token

Returns:

  • (String)

    The Authorization header value



114
115
116
# File 'lib/domain_extractor.rb', line 114

def bearer_auth_header(token)
  URIHelpers.bearer_auth_header(token)
end

.decode_credential(value) ⇒ String

Decode a percent-encoded credential

Parameters:

  • value (String)

    The encoded value

Returns:

  • (String)

    Decoded value



128
129
130
# File 'lib/domain_extractor.rb', line 128

def decode_credential(value)
  URIHelpers.decode_credential(value)
end

.encode_credential(value) ⇒ String

Encode a credential for use in URLs (percent-encoding)

Parameters:

  • value (String)

    The value to encode

Returns:

  • (String)

    Percent-encoded value



121
122
123
# File 'lib/domain_extractor.rb', line 121

def encode_credential(value)
  URIHelpers.encode_credential(value)
end

.format(url) ⇒ String?

Format a URL according to the specified options. Returns a formatted URL string or nil if the input is invalid.

Examples:

Standard formatting

DomainExtractor.format('https://www.example.com/')
# => 'https://www.example.com'

Root domain only

DomainExtractor.format('https://shop.example.com/path', validation: :root_domain)
# => 'https://example.com'

Without protocol

DomainExtractor.format('https://example.com', use_protocol: false)
# => 'example.com'

Parameters:

  • url (String)

    The URL to format

  • options (Hash)

    Formatting options

Returns:

  • (String, nil)


97
98
99
# File 'lib/domain_extractor.rb', line 97

def format(url, **)
  Formatter.call(url, **)
end

.parse(url) ⇒ ParsedURL

Parse an individual URL and extract domain attributes. Returns a ParsedURL object that supports hash-style access and method calls. For invalid inputs the returned ParsedURL will be marked invalid and all accessors (without bang) will evaluate to nil/false.

Parameters:

  • url (String, #to_s)

Returns:



31
32
33
# File 'lib/domain_extractor.rb', line 31

def parse(url)
  Parser.call(url)
end

.parse!(url) ⇒ ParsedURL

Parse an individual URL and raise when extraction fails. This mirrors the legacy behaviour of .parse while giving callers an explicit opt-in to strict validation.

Parameters:

  • url (String, #to_s)

Returns:

Raises:



41
42
43
44
45
46
# File 'lib/domain_extractor.rb', line 41

def parse!(url)
  result = Parser.call(url)
  raise InvalidURLError unless result.valid?

  result
end

.parse_batch(urls) ⇒ Array<ParsedURL, nil>

Parse many URLs and return their individual parse results. Returns nil for invalid URLs to maintain backward compatibility.

Parameters:

  • urls (Enumerable<String>)

Returns:



59
60
61
62
63
64
65
66
# File 'lib/domain_extractor.rb', line 59

def parse_batch(urls)
  return [] unless urls.respond_to?(:map)

  urls.map do |url|
    result = Parser.call(url)
    result.valid? ? result : nil
  end
end

.parse_query_params(query_string) ⇒ Hash Also known as: parse_query

Convert a query string into a Hash representation.

Parameters:

  • query_string (String, nil)

Returns:

  • (Hash)


71
72
73
# File 'lib/domain_extractor.rb', line 71

def parse_query_params(query_string)
  QueryParams.call(query_string)
end

.valid?(url) ⇒ Boolean

Determine if a URL is considered valid by the parser.

Parameters:

  • url (String, #to_s)

Returns:

  • (Boolean)


51
52
53
# File 'lib/domain_extractor.rb', line 51

def valid?(url)
  Parser.valid?(url)
end