Skip to content

Commit

Permalink
Consider all subdomains in Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
owst committed Mar 18, 2016
1 parent fa7696d commit db84501
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
9 changes: 6 additions & 3 deletions platform/ruby/mail_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module MailChecker
def self.valid?(email)
return false unless valid_email?(email)

!BLACKLIST.include?(extract_domain(email))
!extract_all_domain_suffixes(email).any? { |domain| BLACKLIST.include?(domain) }
end

def self.valid_email?(email)
Expand All @@ -21,9 +21,12 @@ def self.valid_email?(email)
email =~ EMAIL_REGEX
end

def self.extract_domain(email)
def self.extract_all_domain_suffixes(email)
domain = email.gsub(/.+@([^.]+)/, '\1').downcase
domain.split('.')[-2, 2].join('.') # Don't include subdomains

domain_components = domain.split('.')

(0...domain_components.length).map { |n| domain_components.drop(n).join('.') }
end
end

Expand Down
9 changes: 6 additions & 3 deletions platform/ruby/mail_checker.tmpl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module MailChecker
def self.valid?(email)
return false unless valid_email?(email)

!BLACKLIST.include?(extract_domain(email))
!extract_all_domain_suffixes(email).any? { |domain| BLACKLIST.include?(domain) }
end

def self.valid_email?(email)
Expand All @@ -21,9 +21,12 @@ def self.valid_email?(email)
email =~ EMAIL_REGEX
end

def self.extract_domain(email)
def self.extract_all_domain_suffixes(email)
domain = email.gsub(/.+@([^.]+)/, '\1').downcase
domain.split('.')[-2, 2].join('.') # Don't include subdomains

domain_components = domain.split('.')

(0...domain_components.length).map { |n| domain_components.drop(n).join('.') }
end
end

Expand Down
20 changes: 17 additions & 3 deletions test/platform.ruby.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class TestMailChecker < MiniTest::Unit::TestCase
def valid!(email)
assert_equal MailChecker(email), true
assert_equal true, MailChecker(email)
end

def invalid!(email)
assert_equal MailChecker(email), false
assert_equal false, MailChecker(email)
end

def test_return_true_if_valid
Expand All @@ -33,7 +33,21 @@ def test_return_false_if_throwable_domain
invalid!('ok@guerrillamailblock.com')
end

def test_return_false_for_blacklisted_domains_and_their_subdomains
MailChecker::BLACKLIST.each do |blacklisted_domain|
invalid!("test@#{blacklisted_domain}")
invalid!("test@subdomain.#{blacklisted_domain}")
# Should not be invalid as a subdomain of a valid domain.
valid!("test@#{blacklisted_domain}.gmail.com")
end
end

def test_can_be_called_as_regular_method
assert_equal MailChecker.valid?(nil), false
assert_equal false, MailChecker.valid?(nil)
end

def test_extract_all_domain_suffixes
expected = %w(sub.example.org example.org org)
assert_equal expected, MailChecker.extract_all_domain_suffixes('test@sub.example.org')
end
end

0 comments on commit db84501

Please sign in to comment.