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

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ describe EmailCheckerController, type: :controller do
let(:params) { { email: 'martin@orane.fr' } } let(:params) { { email: 'martin@orane.fr' } }
it do it do
expect(response).to have_http_status(:success) 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
end end

View file

@ -1,6 +1,6 @@
describe EmailChecker do describe EmailChecker do
describe 'check' do describe 'check' do
subject { described_class.new } subject { described_class }
it 'works with identified use cases' do it 'works with identified use cases' do
expect(subject.check(email: nil)).to eq({ success: false }) expect(subject.check(email: nil)).to eq({ success: false })
@ -10,22 +10,22 @@ describe EmailChecker do
# allow same domain # allow same domain
expect(subject.check(email: "martin@orange.fr")).to eq({ success: true }) expect(subject.check(email: "martin@orange.fr")).to eq({ success: true })
# find difference of 1 lev distance # 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 # 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 # ignore unknown domain
expect(subject.check(email: "martin@ore.fr")).to eq({ success: true }) expect(subject.check(email: "martin@ore.fr")).to eq({ success: true })
end end
it 'passes through real use cases, with levenshtein_distance 1' do 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@asn.com")).to eq({ success: true, 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@gamail.com")).to eq({ success: true, 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@glail.com")).to eq({ success: true, 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.coml")).to eq({ success: true, 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@gmail.con")).to eq({ success: true, 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@hotmil.fr")).to eq({ success: true, 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@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, email_suggestions: ["martin@msn.com", "martin@mac.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 }) expect(subject.check(email: "martin@ymail.com")).to eq({ success: true })
end end