Module: DomainExtractor

Defined in:
lib/domain_extractor.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/query_params.rb,
lib/domain_extractor/domain_validator.rb

Overview

Also register in DomainExtractor namespace for backwards compatibility

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'0.2.8'
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

.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'


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

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.



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

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.

Raises:



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

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.



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

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.



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

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

.valid?(url) ⇒ Boolean

Determine if a URL is considered valid by the parser.



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

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