Module: Philiprehberger::EmailValidator
- Defined in:
- lib/philiprehberger/email_validator.rb,
lib/philiprehberger/email_validator/result.rb,
lib/philiprehberger/email_validator/syntax.rb,
lib/philiprehberger/email_validator/version.rb,
lib/philiprehberger/email_validator/mx_check.rb,
lib/philiprehberger/email_validator/disposable.rb,
lib/philiprehberger/email_validator/normalizer.rb,
lib/philiprehberger/email_validator/configuration.rb,
lib/philiprehberger/email_validator/typo_suggester.rb,
lib/philiprehberger/email_validator/domain_info_extractor.rb
Defined Under Namespace
Modules: Disposable, DomainInfoExtractor, MxCheck, Normalizer, Syntax, TypoSuggester Classes: Configuration, Error, Result
Constant Summary collapse
- ROLE_BASED_LOCALS =
Role-based local parts that typically represent groups, not individuals.
Set.new(%w[ abuse admin billing contact dev devnull ftp help hostmaster info mail mailer-daemon marketing noc noreply no-reply office postmaster press registrar remove root sales security spam subscribe support sysadmin tech undisclosed-recipients unsubscribe usenet uucp webmaster www ]).freeze
- VERSION =
'0.4.0'
Class Method Summary collapse
-
.batch_validate(emails, **opts) ⇒ Hash{String => Result}
Validate multiple emails and return a hash mapping each email to its Result.
-
.canonical_equal?(a, b) ⇒ Boolean
Compare two email addresses after normalization.
-
.configuration ⇒ Configuration
The current configuration instance.
-
.configure {|Configuration| ... } ⇒ void
Configure the email validator.
-
.disposable?(email) ⇒ Boolean
Check if an email address uses a known disposable domain.
-
.domain_info(email, check_mx: false) ⇒ Hash
Extract domain information from an email address.
-
.mx_valid?(domain) ⇒ Boolean
Check if a domain has valid MX records.
-
.normalize(email) ⇒ String
Normalize an email address.
-
.reset_configuration! ⇒ void
Reset configuration to defaults.
-
.role_based?(email) ⇒ Boolean
Detect role-based email addresses (info@, admin@, support@, etc.).
-
.suggest(email) ⇒ Hash?
Suggest a corrected email if the domain appears to be a typo.
-
.valid?(email) ⇒ Boolean
Quick syntax check for an email address.
-
.valid_all?(emails) ⇒ Boolean
Check if all emails in an array are valid.
-
.validate(email, check_mx: false, allow_disposable: true) ⇒ Result
Full validation returning a Result object.
-
.validate_all(emails, **opts) ⇒ Array<Result>
Validate an array of email addresses.
Class Method Details
.batch_validate(emails, **opts) ⇒ Hash{String => Result}
Validate multiple emails and return a hash mapping each email to its Result.
78 79 80 81 82 |
# File 'lib/philiprehberger/email_validator.rb', line 78 def batch_validate(emails, **opts) emails.to_h do |email| [email, validate(email, **opts)] end end |
.canonical_equal?(a, b) ⇒ Boolean
Compare two email addresses after normalization.
Returns false rather than raising if either input is invalid.
159 160 161 162 163 |
# File 'lib/philiprehberger/email_validator.rb', line 159 def canonical_equal?(a, b) Normalizer.normalize(a) == Normalizer.normalize(b) rescue Error, StandardError false end |
.configuration ⇒ Configuration
The current configuration instance.
139 140 141 |
# File 'lib/philiprehberger/email_validator.rb', line 139 def configuration @configuration ||= Configuration.new end |
.configure {|Configuration| ... } ⇒ void
This method returns an undefined value.
Configure the email validator.
125 126 127 |
# File 'lib/philiprehberger/email_validator.rb', line 125 def configure yield configuration end |
.disposable?(email) ⇒ Boolean
Check if an email address uses a known disposable domain.
104 105 106 |
# File 'lib/philiprehberger/email_validator.rb', line 104 def disposable?(email) disposable_domain?(email) end |
.domain_info(email, check_mx: false) ⇒ Hash
Extract domain information from an email address.
179 180 181 |
# File 'lib/philiprehberger/email_validator.rb', line 179 def domain_info(email, check_mx: false) DomainInfoExtractor.extract(email, check_mx: check_mx) end |
.mx_valid?(domain) ⇒ Boolean
Check if a domain has valid MX records.
96 97 98 |
# File 'lib/philiprehberger/email_validator.rb', line 96 def mx_valid?(domain) MxCheck.valid?(domain) end |
.normalize(email) ⇒ String
Normalize an email address.
148 149 150 |
# File 'lib/philiprehberger/email_validator.rb', line 148 def normalize(email) Normalizer.normalize(email) end |
.reset_configuration! ⇒ void
This method returns an undefined value.
Reset configuration to defaults.
132 133 134 |
# File 'lib/philiprehberger/email_validator.rb', line 132 def reset_configuration! @configuration = Configuration.new end |
.role_based?(email) ⇒ Boolean
Detect role-based email addresses (info@, admin@, support@, etc.).
112 113 114 115 116 117 118 119 |
# File 'lib/philiprehberger/email_validator.rb', line 112 def role_based?(email) return false unless email.is_a?(String) local = extract_local(email) return false if local.nil? ROLE_BASED_LOCALS.include?(local.downcase) end |
.suggest(email) ⇒ Hash?
Suggest a corrected email if the domain appears to be a typo.
169 170 171 |
# File 'lib/philiprehberger/email_validator.rb', line 169 def suggest(email) TypoSuggester.suggest(email) end |
.valid?(email) ⇒ Boolean
Quick syntax check for an email address.
33 34 35 |
# File 'lib/philiprehberger/email_validator.rb', line 33 def valid?(email) Syntax.valid?(email) end |
.valid_all?(emails) ⇒ Boolean
Check if all emails in an array are valid.
88 89 90 |
# File 'lib/philiprehberger/email_validator.rb', line 88 def valid_all?(emails) emails.all? { |email| valid?(email) } end |
.validate(email, check_mx: false, allow_disposable: true) ⇒ Result
Full validation returning a Result object.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/philiprehberger/email_validator.rb', line 43 def validate(email, check_mx: false, allow_disposable: true) errors = [] warnings = [] syntax_errors = Syntax.validate(email) errors.concat(syntax_errors) if syntax_errors.empty? errors << 'disposable email domains are not allowed' if !allow_disposable && disposable_domain?(email) warnings << 'address appears to be role-based' if role_based?(email) if check_mx domain = extract_domain(email) errors << "domain '#{domain}' has no MX or A records" unless MxCheck.valid?(domain) end end Result.new(errors: errors, warnings: warnings) end |
.validate_all(emails, **opts) ⇒ Array<Result>
Validate an array of email addresses.
69 70 71 |
# File 'lib/philiprehberger/email_validator.rb', line 69 def validate_all(emails, **opts) emails.map { |email| validate(email, **opts) } end |