review(pr): some enhancement, tx @colinux
Co-Authored-By: Colin Darie <colin@darie.eu>
This commit is contained in:
parent
66eb3dc821
commit
8e3d45b0b1
4 changed files with 16 additions and 9 deletions
|
@ -1,5 +1,5 @@
|
||||||
class EmailCheckerController < ApplicationController
|
class EmailCheckerController < ApplicationController
|
||||||
def show
|
def show
|
||||||
render json: EmailChecker.check(email: params[:email])
|
render json: EmailChecker.new.check(email: params[:email])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,11 @@ export class EmailInputController extends ApplicationController {
|
||||||
declare readonly inputTarget: HTMLInputElement;
|
declare readonly inputTarget: HTMLInputElement;
|
||||||
|
|
||||||
async checkEmail() {
|
async checkEmail() {
|
||||||
if (!this.inputTarget.value) {
|
if (
|
||||||
|
!this.inputTarget.value ||
|
||||||
|
this.inputTarget.value.length < 5 ||
|
||||||
|
!this.inputTarget.value.includes('@')
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
class EmailChecker
|
class EmailChecker
|
||||||
|
# Extracted 100 most used domain on our users table [june 2024]
|
||||||
|
# + all .gouv.fr domain on our users table
|
||||||
|
# + all .ac-xxx on our users table
|
||||||
KNOWN_DOMAINS = [
|
KNOWN_DOMAINS = [
|
||||||
'gmail.com',
|
'gmail.com',
|
||||||
'hotmail.fr',
|
'hotmail.fr',
|
||||||
|
@ -612,10 +615,10 @@ class EmailChecker
|
||||||
'ac-toulous.fr'
|
'ac-toulous.fr'
|
||||||
].freeze
|
].freeze
|
||||||
|
|
||||||
def self.check(email:)
|
def check(email:)
|
||||||
return { success: false } if email.blank?
|
return { success: false } if email.blank?
|
||||||
|
|
||||||
parsed_email = Mail::Address.new(email)
|
parsed_email = Mail::Address.new(EmailSanitizableConcern::EmailSanitizer.sanitize(email))
|
||||||
return { success: false } if parsed_email.domain.blank?
|
return { success: false } if parsed_email.domain.blank?
|
||||||
|
|
||||||
return { success: true } if KNOWN_DOMAINS.any? { _1 == parsed_email.domain }
|
return { success: true } if KNOWN_DOMAINS.any? { _1 == parsed_email.domain }
|
||||||
|
@ -628,22 +631,22 @@ class EmailChecker
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.closest_domains(domain:)
|
def 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 self.close_by_distance_of(a, b, distance:)
|
def close_by_distance_of(a, b, distance:)
|
||||||
String::Similarity.levenshtein_distance(a, b) == distance
|
String::Similarity.levenshtein_distance(a, b) == distance
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.with_same_chars_and_close_by_distance_of(a, b, distance:)
|
def 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 self.email_suggestions(parsed_email:, similar_domains:)
|
def email_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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
describe EmailChecker do
|
describe EmailChecker do
|
||||||
describe 'check' do
|
describe 'check' do
|
||||||
subject { described_class }
|
subject { described_class.new }
|
||||||
|
|
||||||
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 })
|
||||||
|
|
Loading…
Reference in a new issue