This commit is contained in:
Lee Reilly 2012-02-16 19:44:09 -08:00
parent 8b3c3763dd
commit 1aac455a13
2 changed files with 42 additions and 5 deletions

View File

@ -132,12 +132,9 @@ module Swot
# false oterwise.
def is_academic?(text)
return false if text.nil?
text.downcase!
text = text.split("@")[1] if text.include? "@"
begin
domain = PublicSuffix.parse(text)
domain = get_domain(text)
return false if domain.nil?
if ACADEMIC_TLDS.include?(domain.tld)
true
@ -155,6 +152,13 @@ module Swot
end
end
def get_institution_name(text)
text.downcase!
text = text.split("@")[1] if text.include? "@"
domain = PublicSuffix.parse(text)
name_from_academic_domain(domain)
end
# Figure out if a domain name is a know academic institution.
#
# Returns true if the domain name belongs to a known academic instition;
@ -162,5 +166,35 @@ module Swot
def match_academic_domain?(domain)
File.exists?("lib/domains/#{domain.tld}/#{domain.sld}")
end
# Figure out the insitutions' name based on the domain name.
#
# Return the institution name, or nil if not found.
def name_from_academic_domain(domain)
begin
file = File.open("lib/domains/#{domain.tld}/#{domain.sld}", "rb")
contents = file.read
contents.strip!
return contents
rescue
return nil
end
end
# Get the FQDN name from a URL or email address.
#
#
def get_domain(text)
return false if text.nil?
text.downcase!
text = text.split("@")[1] if text.include? "@"
begin
domain = PublicSuffix.parse(text)
return domain
rescue
return nil
end
end
end
end

View File

@ -38,6 +38,9 @@ class TestEmail < Test::Unit::TestCase
assert_equal Swot::is_academic?(nil), false
assert_equal Swot::is_academic?(''), false
assert_equal Swot::is_academic?('the'), false
end
should "returns name of valid institution" do
assert_equal Swot::get_institution_name('lreilly@cs.strath.ac.uk'), "University of Strathclyde"
end
end