From 218ceaf385b4e4cc551246cb02384d7fe7ae893d Mon Sep 17 00:00:00 2001 From: "Simeon F. Willbanks" Date: Mon, 7 Oct 2013 12:32:52 -0700 Subject: [PATCH] * 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 --- .travis.yml | 4 ++++ lib/swot.rb | 22 +++++++++------------- test/test_swot.rb | 4 ++++ 3 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..222078e126 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: ruby +rvm: + - 1.9.3 + - 2.0.0 diff --git a/lib/swot.rb b/lib/swot.rb index 4aa598a18e..c7a368183a 100644 --- a/lib/swot.rb +++ b/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 \ No newline at end of file +end diff --git a/test/test_swot.rb b/test/test_swot.rb index 46dbb0ec19..e4aa2f0dbf 100644 --- a/test/test_swot.rb +++ b/test/test_swot.rb @@ -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"