diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb index 5f2b841ef..f5e2507e1 100644 --- a/app/helpers/articles_helper.rb +++ b/app/helpers/articles_helper.rb @@ -49,7 +49,7 @@ module ArticlesHelper def get_host_without_www(url) url = "http://#{url}" if URI.parse(url).scheme.nil? host = URI.parse(url).host.downcase - host.gsub!("medium.com", "Medium") if host.include?("medium.com") - host.start_with?("www.") ? host[4..-1] : host + host.gsub!("medium.com", "Medium") + host.delete_prefix("www.") end end diff --git a/spec/helpers/articles_helper_spec.rb b/spec/helpers/articles_helper_spec.rb new file mode 100644 index 000000000..173c6fe79 --- /dev/null +++ b/spec/helpers/articles_helper_spec.rb @@ -0,0 +1,25 @@ +require "rails_helper" + +describe ArticlesHelper do + describe ".get_host_without_www" do + it "drops the www off of a valid url" do + host = helper.get_host_without_www("https://www.example.com") + expect(host).to eq "example.com" + end + + it "lowercases the host name in general" do + host = helper.get_host_without_www("https://www.EXAMPLE.COM") + expect(host).to eq "example.com" + end + + it "titlecases the host for medium.com and drops .com" do + host = helper.get_host_without_www("https://www.medium.com") + expect(host).to eq "Medium" + end + + it "can handle urls without schemes" do + host = helper.get_host_without_www("www.example.com") + expect(host).to eq "example.com" + end + end +end