From a00d1eb0d288db072c79c8f03fc9cee0463746b9 Mon Sep 17 00:00:00 2001 From: Ryan Palo Date: Wed, 23 Oct 2019 11:36:32 -0700 Subject: [PATCH] Refactor ArticleHelper#get_host_without_www (#4558) [ci skip] Also add specs for it --- app/helpers/articles_helper.rb | 4 ++-- spec/helpers/articles_helper_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 spec/helpers/articles_helper_spec.rb 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