clean(tech): EmailChecker, use class method, not instance

This commit is contained in:
mfo 2024-07-01 10:38:43 +02:00
parent 554141bb67
commit ced634295e
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
6 changed files with 25 additions and 31 deletions

View file

@ -9,17 +9,11 @@ module Administrateurs
end
def create
email_checker = EmailChecker.new
emails = params['emails'].presence || [].to_json
emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) }
@maybe_typo, emails = emails.map do |email|
result = email_checker.check(email: email)
if result[:email_suggestions].present?
[email, result[:email_suggestions].first]
else
[email, nil]
end
end.partition { _1[1].present? }
@maybe_typo, emails = emails
.map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] }
.partition { _1[1].present? }
errors = if !@maybe_typo.empty?
["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.map(&:first).join(', ')}"]
else

View file

@ -1,5 +1,5 @@
class EmailCheckerController < ApplicationController
def show
render json: EmailChecker.new.check(email: params[:email])
render json: EmailChecker.check(email: params[:email])
end
end

View file

@ -4,7 +4,7 @@ import { ApplicationController } from './application_controller';
type checkEmailResponse = {
success: boolean;
email_suggestions: string[];
suggestions: string[];
};
export class EmailInputController extends ApplicationController {
@ -36,8 +36,8 @@ export class EmailInputController extends ApplicationController {
url.toString()
).json();
if (data && data.email_suggestions && data.email_suggestions.length > 0) {
this.suggestionTarget.innerHTML = data.email_suggestions[0];
if (data && data.suggestions && data.suggestions.length > 0) {
this.suggestionTarget.innerHTML = data.suggestions[0];
show(this.ariaRegionTarget);
this.ariaRegionTarget.setAttribute('aria-live', 'assertive');
}

View file

@ -615,7 +615,7 @@ class EmailChecker
'ac-toulous.fr'
].freeze
def check(email:)
def self.check(email:)
return { success: false } if email.blank?
parsed_email = Mail::Address.new(EmailSanitizableConcern::EmailSanitizer.sanitize(email))
@ -626,29 +626,29 @@ class EmailChecker
similar_domains = closest_domains(domain: parsed_email.domain)
return { success: true } if similar_domains.empty?
{ success: true, email_suggestions: email_suggestions(parsed_email:, similar_domains:) }
{ success: true, suggestions: suggestions(parsed_email:, similar_domains:) }
rescue Mail::Field::IncompleteParseError
return { success: false }
end
private
def closest_domains(domain:)
def self.closest_domains(domain:)
KNOWN_DOMAINS.filter do |known_domain|
close_by_distance_of(domain, known_domain, distance: 1) ||
with_same_chars_and_close_by_distance_of(domain, known_domain, distance: 2)
end
end
def close_by_distance_of(a, b, distance:)
def self.close_by_distance_of(a, b, distance:)
String::Similarity.levenshtein_distance(a, b) == distance
end
def with_same_chars_and_close_by_distance_of(a, b, distance:)
def self.with_same_chars_and_close_by_distance_of(a, b, distance:)
close_by_distance_of(a, b, distance: 2) && a.chars.sort == b.chars.sort
end
def email_suggestions(parsed_email:, similar_domains:)
def self.suggestions(parsed_email:, similar_domains:)
similar_domains.map { Mail::Address.new("#{parsed_email.local}@#{_1}").to_s }
end
end

View file

@ -16,7 +16,7 @@ describe EmailCheckerController, type: :controller do
let(:params) { { email: 'martin@orane.fr' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: true, email_suggestions: ['martin@orange.fr'] })
expect(body).to eq({ success: true, suggestions: ['martin@orange.fr'] })
end
end

View file

@ -1,6 +1,6 @@
describe EmailChecker do
describe 'check' do
subject { described_class.new }
subject { described_class }
it 'works with identified use cases' do
expect(subject.check(email: nil)).to eq({ success: false })
@ -10,22 +10,22 @@ describe EmailChecker do
# allow same domain
expect(subject.check(email: "martin@orange.fr")).to eq({ success: true })
# find difference of 1 lev distance
expect(subject.check(email: "martin@orane.fr")).to eq({ success: true, email_suggestions: ['martin@orange.fr'] })
expect(subject.check(email: "martin@orane.fr")).to eq({ success: true, suggestions: ['martin@orange.fr'] })
# find difference of 2 lev distance, only with same chars
expect(subject.check(email: "martin@oragne.fr")).to eq({ success: true, email_suggestions: ['martin@orange.fr'] })
expect(subject.check(email: "martin@oragne.fr")).to eq({ success: true, suggestions: ['martin@orange.fr'] })
# ignore unknown domain
expect(subject.check(email: "martin@ore.fr")).to eq({ success: true })
end
it 'passes through real use cases, with levenshtein_distance 1' do
expect(subject.check(email: "martin@asn.com")).to eq({ success: true, email_suggestions: ['martin@msn.com'] })
expect(subject.check(email: "martin@gamail.com")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@glail.com")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@gmail.coml")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@gmail.con")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@hotmil.fr")).to eq({ success: true, email_suggestions: ['martin@hotmail.fr'] })
expect(subject.check(email: "martin@mail.com")).to eq({ success: true, email_suggestions: ["martin@gmail.com", "martin@ymail.com", "martin@mailo.com"] })
expect(subject.check(email: "martin@msc.com")).to eq({ success: true, email_suggestions: ["martin@msn.com", "martin@mac.com"] })
expect(subject.check(email: "martin@asn.com")).to eq({ success: true, suggestions: ['martin@msn.com'] })
expect(subject.check(email: "martin@gamail.com")).to eq({ success: true, suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@glail.com")).to eq({ success: true, suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@gmail.coml")).to eq({ success: true, suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@gmail.con")).to eq({ success: true, suggestions: ['martin@gmail.com'] })
expect(subject.check(email: "martin@hotmil.fr")).to eq({ success: true, suggestions: ['martin@hotmail.fr'] })
expect(subject.check(email: "martin@mail.com")).to eq({ success: true, suggestions: ["martin@gmail.com", "martin@ymail.com", "martin@mailo.com"] })
expect(subject.check(email: "martin@msc.com")).to eq({ success: true, suggestions: ["martin@msn.com", "martin@mac.com"] })
expect(subject.check(email: "martin@ymail.com")).to eq({ success: true })
end