* Refactor #get_institution_name to use #get_domain
* Define `#get_path` to get file path from domain * `#match_academic_domain?` and `#name_from_academic_domain` use `#get_path` * Strip input `text` within `#get_domain` * Replace `File.open` with `File.read` in `#name_from_academic_domain` * Be more concise * Ensure file is closed * Add Travis CI
This commit is contained in:
parent
5b20e20974
commit
218ceaf385
4
.travis.yml
Normal file
4
.travis.yml
Normal file
@ -0,0 +1,4 @@
|
||||
language: ruby
|
||||
rvm:
|
||||
- 1.9.3
|
||||
- 2.0.0
|
||||
22
lib/swot.rb
22
lib/swot.rb
@ -131,7 +131,6 @@ module Swot
|
||||
# false otherwise.
|
||||
def is_academic?(text)
|
||||
return false if text.nil?
|
||||
text.strip!
|
||||
begin
|
||||
domain = get_domain(text)
|
||||
return false if domain.nil?
|
||||
@ -157,11 +156,7 @@ module Swot
|
||||
#
|
||||
# Returns a string with the institution name; nil if nothing is found.
|
||||
def get_institution_name(text)
|
||||
text.strip!
|
||||
text.downcase!
|
||||
text = text.split("@")[1] if text.include? "@"
|
||||
domain = PublicSuffix.parse(text)
|
||||
name_from_academic_domain(domain)
|
||||
name_from_academic_domain(get_domain(text))
|
||||
end
|
||||
alias_method :school_name, :get_institution_name
|
||||
|
||||
@ -170,7 +165,7 @@ module Swot
|
||||
# Returns true if the domain name belongs to a known academic institution;
|
||||
# false otherwise.
|
||||
def match_academic_domain?(domain)
|
||||
File.exists?("#{File.expand_path(__FILE__+'/..')}/domains/#{domain.tld}/#{domain.sld}")
|
||||
File.exists?(get_path(domain))
|
||||
end
|
||||
|
||||
# Figure out the institutions' name based on the domain name.
|
||||
@ -178,10 +173,7 @@ module Swot
|
||||
# Return the institution name, or nil if not found.
|
||||
def name_from_academic_domain(domain)
|
||||
begin
|
||||
file = File.open("#{File.expand_path(__FILE__+'/..')}/domains/#{domain.tld}/#{domain.sld}", "rb")
|
||||
contents = file.read
|
||||
contents.strip!
|
||||
return contents
|
||||
File.read(get_path(domain), :mode => "rb").strip
|
||||
rescue
|
||||
return nil
|
||||
end
|
||||
@ -191,11 +183,15 @@ module Swot
|
||||
#
|
||||
# Returns a string with the FQDN; nil if there's an error.
|
||||
def get_domain(text)
|
||||
PublicSuffix.parse text.downcase.match(domain_regex).captures.first
|
||||
PublicSuffix.parse text.strip.downcase.match(domain_regex).captures.first
|
||||
rescue
|
||||
return nil
|
||||
end
|
||||
|
||||
def get_path(domain)
|
||||
File.join(File.dirname(__FILE__), "domains", domain.tld, domain.sld)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# http://rubular.com/r/uW2GqSxvqD
|
||||
@ -203,4 +199,4 @@ module Swot
|
||||
/([^@\/:]+)[:\d]*$/
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -48,6 +48,10 @@ class TestEmail < Test::Unit::TestCase
|
||||
assert_equal Swot::get_institution_name('lreilly@cs.strath.ac.uk'), "University of Strathclyde"
|
||||
end
|
||||
|
||||
should "returns nil when institution invalid" do
|
||||
assert_equal Swot::get_institution_name('foo@shop.com'), nil
|
||||
end
|
||||
|
||||
should "test aliased methods" do
|
||||
assert_equal Swot::academic?('stanford.edu'), true
|
||||
assert_equal Swot::school_name('lreilly@cs.strath.ac.uk'), "University of Strathclyde"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user