From 550c5abd6c331e03072c8fa0cd487e6105baf0ff Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 30 Jun 2021 14:16:50 +0500 Subject: [PATCH] Add punycode email support --- app/interactions/actions/email_check.rb | 13 +++++++--- config/initializers/{xsd.rb => libs.rb} | 1 + lib/email_address_converter.rb | 33 +++++++++++++++++++++++++ lib/tasks/verify_email.rake | 2 ++ 4 files changed, 45 insertions(+), 4 deletions(-) rename config/initializers/{xsd.rb => libs.rb} (60%) create mode 100644 lib/email_address_converter.rb diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index 5c664cb6e..c17831289 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -9,16 +9,17 @@ module Actions end def call - result = check_email + parsed_email = EmailAddressConverter.punycode_to_unicode(email) + result = check_email(parsed_email) save_result(result) - log_failure(result) unless result.success + result.success ? log_success : log_failure(result) result.success end private - def check_email - Truemail.validate(email, with: check_level).result + def check_email(parsed_email) + Truemail.validate(parsed_email, with: check_level).result end def save_result(result) @@ -46,6 +47,10 @@ module Actions logger.info "Validation level #{check_level}, the result was #{result}" end + def log_success + logger.info "Successfully validated email #{email} for the #{log_object_id}." + end + def log_object_id "#{validation_eventable.class}: #{validation_eventable.id}" end diff --git a/config/initializers/xsd.rb b/config/initializers/libs.rb similarity index 60% rename from config/initializers/xsd.rb rename to config/initializers/libs.rb index 12a8af78a..c140f21ba 100644 --- a/config/initializers/xsd.rb +++ b/config/initializers/libs.rb @@ -1,2 +1,3 @@ require 'application_service' +require 'email_address_converter' require 'xsd/schema' diff --git a/lib/email_address_converter.rb b/lib/email_address_converter.rb new file mode 100644 index 000000000..e0d8c47c4 --- /dev/null +++ b/lib/email_address_converter.rb @@ -0,0 +1,33 @@ +module EmailAddressConverter + module_function + + def punycode_to_unicode(email) + return email if domain(email) == 'not_found' + + local = local(email) + domain = SimpleIDN.to_unicode(domain(email)) + "#{local}@#{domain}"&.downcase + end + + def unicode_to_punycode(email) + return email if domain(email) == 'not_found' + + local = local(email) + domain = SimpleIDN.to_ascii(domain(email)) + "#{local}@#{domain}"&.downcase + end + + private + + def domain(email) + Mail::Address.new(email).domain&.downcase || 'not_found' + rescue Mail::Field::IncompleteParseError + 'not_found' + end + + def local(email) + Mail::Address.new(email).local&.downcase || email + rescue Mail::Field::IncompleteParseError + email + end +end diff --git a/lib/tasks/verify_email.rake b/lib/tasks/verify_email.rake index daffabf19..368cd4a55 100644 --- a/lib/tasks/verify_email.rake +++ b/lib/tasks/verify_email.rake @@ -20,4 +20,6 @@ namespace :verify_email do .by_domain(args[:domain_name]) verifications_by_domain.map { |ver| VerifyEmailsJob.perform_later(ver.id) } end + + desc 'Starts verifying email jobs with check level and ' end