Refactor ArticleHelper#get_host_without_www (#4558) [ci skip]
Also add specs for it
This commit is contained in:
parent
9e6c64fa83
commit
a00d1eb0d2
2 changed files with 27 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
25
spec/helpers/articles_helper_spec.rb
Normal file
25
spec/helpers/articles_helper_spec.rb
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue