Module: Philiprehberger::EmailValidator::Normalizer

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

Overview

Email address normalization.

Lowercases, strips whitespace, removes Gmail dots, and strips plus-addressing aliases.

Constant Summary collapse

GMAIL_DOMAINS =

Gmail-like domains where dots in the local part are ignored.

Set.new(%w[gmail.com googlemail.com]).freeze

Class Method Summary collapse

Class Method Details

.normalize(email) ⇒ String

Normalize an email address.

Parameters:

  • email (String)

    the email address to normalize

Returns:

  • (String)

    the normalized email address

Raises:



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

def normalize(email)
  raise Error, 'email must be a string' unless email.is_a?(String)

  stripped = email.strip.downcase

  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

  local, domain = parts

  raise Error, 'local part must not be empty' if local.empty?
  raise Error, 'domain must not be empty' if domain.empty?

  # Remove plus-addressing alias
  local = local.split('+', 2).first

  # Remove dots from Gmail local parts
  local = local.delete('.') if GMAIL_DOMAINS.include?(domain)

  "#{local}@#{domain}"
end