lib/email_inquire/inquirer.rb in email_inquire-0.2.0 vs lib/email_inquire/inquirer.rb in email_inquire-0.3.0
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
require "damerau-levenshtein"
+require "set"
module EmailInquire
class Inquirer
@@ -19,11 +20,10 @@
end
def initialize(email)
@email = email.downcase
- response.email = email
parse_email
end
attr_reader :domain, :email, :name
@@ -39,22 +39,26 @@
def parse_email
@name, @domain = email.split("@")
end
def response
- @response ||= Response.new
+ @response ||= Response.new.tap do |response|
+ response.email = email
+ end
end
+ VALIDATORS = [
+ :validate_common_domains,
+ :validate_one_time_providers,
+ :validate_common_domain_mistakes,
+ :validate_uk_tld,
+ :validate_common_tld_mistakes,
+ :validate_domains_with_unique_tld,
+ ].freeze
+
def validate_typos
- [
- :validate_common_domains,
- :validate_one_time_providers,
- :validate_common_domain_mistakes,
- :validate_uk_tld,
- :validate_common_tld_mistakes,
- :validate_domains_with_unique_tld,
- ].each do |validator|
+ VALIDATORS.each do |validator|
send(validator)
break if response.valid? || response.invalid?
end
# default
@@ -115,13 +119,12 @@
return if VALID_UK_TLD.any? do |reference|
domain.end_with?(reference)
end
new_domain = domain.dup
+ new_domain.gsub!(/\.[a-z]{2}\.uk\z/, ".co.uk")
new_domain.gsub!(/(?<!\.)co\.uk\z/, ".co.uk")
- new_domain.gsub!(/\.c[^o]\.uk\z/, ".co.uk")
- new_domain.gsub!(/\.[^c]o\.uk\z/, ".co.uk")
- new_domain.gsub!(/(?<!co)\.uk\z/, ".co.uk")
+ new_domain.gsub!(/(?<!\.co)\.uk\z/, ".co.uk")
response.hint!(domain: new_domain) if new_domain != domain
end
UNIQUE_TLD_DOMAINS = load_data("unique_domain_providers").freeze