diff --git a/lib/twingly/url.rb b/lib/twingly/url.rb index 64c6795..cf4457f 100644 --- a/lib/twingly/url.rb +++ b/lib/twingly/url.rb @@ -24,15 +24,11 @@ def self.parse(potential_url) end def self.internal_parse(potential_url) - if potential_url.is_a?(Addressable::URI) - addressable_uri = potential_url - else - addressable_uri = Addressable::URI.heuristic_parse(potential_url) - end - + addressable_uri = to_addressable_uri(potential_url) raise Twingly::Error::ParseError if addressable_uri.nil? public_suffix_domain = PublicSuffix.parse(addressable_uri.display_uri.host) + raise Twingly::Error::ParseError if public_suffix_domain.nil? self.new(addressable_uri, public_suffix_domain) rescue Addressable::URI::InvalidURIError, PublicSuffix::DomainInvalid => error @@ -40,6 +36,14 @@ def self.internal_parse(potential_url) raise end + def self.to_addressable_uri(potential_url) + if potential_url.is_a?(Addressable::URI) + potential_url + else + Addressable::URI.heuristic_parse(potential_url) + end + end + def initialize(addressable_uri, public_suffix_domain) unless addressable_uri.is_a?(Addressable::URI) raise ArgumentError, "First parameter must be an Addressable::URI" @@ -123,7 +127,7 @@ def normalized_path end def valid? - addressable_uri && public_suffix_domain && SCHEMES.include?(normalized_scheme) + SCHEMES.include?(normalized_scheme) end def <=>(other) diff --git a/spec/lib/twingly/url_spec.rb b/spec/lib/twingly/url_spec.rb index ba2fcc5..6d0dd6a 100644 --- a/spec/lib/twingly/url_spec.rb +++ b/spec/lib/twingly/url_spec.rb @@ -104,6 +104,20 @@ def valid_urls end end + describe ".to_addressable_uri" do + let(:adressable_uri) { Addressable::URI.parse(test_url) } + + context "when given Addressable::URI" do + subject { described_class.to_addressable_uri(adressable_uri) } + it { is_expected.to eq(adressable_uri) } + end + + context "when given string" do + subject { described_class.to_addressable_uri(test_url) } + it { is_expected.to eq(adressable_uri) } + end + end + describe "#initialize" do context "when given input parameters of wrong types" do it "raises an error" do