Module: Philiprehberger::EmailValidator::DomainInfoExtractor

Defined in:
lib/philiprehberger/email_validator/domain_info_extractor.rb

Overview

Domain metadata extraction from email addresses.

Extracts domain, TLD, and optionally MX records for analytics and domain-based routing.

Class Method Summary collapse

Class Method Details

.extract(email, check_mx: false) ⇒ Hash

Extract domain information from an email address.

Parameters:

  • email (String)

    the email address to analyze

  • check_mx (Boolean) (defaults to: false)

    whether to look up MX records (default: false)

Returns:

  • (Hash)

    { domain:, tld:, mx_records: }

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/philiprehberger/email_validator/domain_info_extractor.rb', line 17

def extract(email, check_mx: false)
  raise Error, 'email must be a string' unless email.is_a?(String)

  stripped = email.strip

  raise Error, 'email must not be empty' if stripped.empty?

  parts = stripped.split('@', 2)

  raise Error, 'email must contain exactly one @ symbol' unless parts.length == 2

  domain = parts[1]

  raise Error, 'domain must not be empty' if domain.nil? || domain.empty?

  domain_lower = domain.downcase
  labels = domain_lower.split('.')
  tld = labels.last

  info = {
    domain: domain_lower,
    tld: tld
  }

  info[:mx_records] = MxCheck.mx_records(domain_lower) if check_mx

  info
end