diff --git a/app/models/link.rb b/app/models/link.rb index 78c6cc115..09b7aa05e 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -6,7 +6,9 @@ def host end def site - PublicSuffix.parse(host).sld.to_sym + PublicSuffix.parse(host).sld + rescue PublicSuffix::DomainInvalid + host end def self.ransackable_attributes(_auth_object = nil) diff --git a/spec/models/link_spec.rb b/spec/models/link_spec.rb new file mode 100644 index 000000000..be98fddee --- /dev/null +++ b/spec/models/link_spec.rb @@ -0,0 +1,12 @@ +require "rails_helper" +RSpec.describe Link do + it "extracts site name from domain" do + link = described_class.new(url: "https://www.example.com") + expect(link.site).to eq "example" + end + + it "extracts site fails safe with bad domain" do + link = described_class.new(url: "https://boop") + expect(link.site).to eq "boop" + end +end