From 7e652bce3ae9514ece6a4124dfb8345a4cf91a0f Mon Sep 17 00:00:00 2001 From: Lee Reilly Date: Sat, 4 Feb 2012 20:57:29 -0800 Subject: [PATCH] Test case sensitivity --- lib/swot.rb | 27 +++++++++++++++++++-------- test/test_email.rb | 8 +++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/swot.rb b/lib/swot.rb index ba3212dfaf..bf2b9b67a3 100644 --- a/lib/swot.rb +++ b/lib/swot.rb @@ -101,17 +101,28 @@ module Swot # Returns true if the domain name belongs to an academic institution; # false oterwise. def is_academic?(text) + return false if text.nil? + + text.downcase! text = text.split("@")[1] if text.include? "@" - domain = PublicSuffix.parse(text) - if ACADEMIC_TLDS.include?(domain.tld) - true - elsif match_academic_domain?(domain) - true - else - false + begin + domain = PublicSuffix.parse(text) + + if ACADEMIC_TLDS.include?(domain.tld) + true + elsif match_academic_domain?(domain) + true + else + false + end + + rescue PublicSuffix::DomainInvalid => di + return false + + rescue PublicSuffix::DomainNotAllowed => dna + return false end - end # Figure out if a domain name is a know academic institution. diff --git a/test/test_email.rb b/test/test_email.rb index b8d3279ff7..501f687a87 100644 --- a/test/test_email.rb +++ b/test/test_email.rb @@ -1,8 +1,10 @@ require 'helper' class TestEmail < Test::Unit::TestCase - should "work with all the things" do + should "recognizes academic email addresses and domains" do assert_equal Swot::is_academic?('lreilly@stanford.edu'), true + assert_equal Swot::is_academic?('LREILLY@STANFORD.EDU'), true + assert_equal Swot::is_academic?('Lreilly@Stanford.Edu'), true assert_equal Swot::is_academic?('lreilly@slac.stanford.edu'), true assert_equal Swot::is_academic?('lreilly@strath.ac.uk'), true assert_equal Swot::is_academic?('lreilly@soft-eng.strath.ac.uk'), true @@ -33,5 +35,9 @@ class TestEmail < Test::Unit::TestCase assert_equal Swot::is_academic?('stanford.edu.com'), false assert_equal Swot::is_academic?('strath.ac.uk.com'), false + assert_equal Swot::is_academic?(nil), false + assert_equal Swot::is_academic?(''), false + assert_equal Swot::is_academic?('the'), false + end end