Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/twingly/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ def internal_parse(input)
try_addressable_normalize(addressable_uri)

host = addressable_uri.host
public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL,
default_rule: nil)
raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?

raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
public_suffix_domain = get_public_suffix_domain(addressable_uri.host)

new(addressable_uri, public_suffix_domain)
rescue *ERRORS_TO_EXTEND => error
Expand Down Expand Up @@ -124,13 +120,21 @@ def valid_label?(label)
label.match?(LETTERS_DIGITS_HYPHEN)
end

def get_public_suffix_domain(host)
public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL, default_rule: nil)
raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?
raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
public_suffix_domain
end

private :new
private :internal_parse
private :clean_input
private :strip_whitespace
private :try_addressable_normalize
private :valid_hostname?
private :valid_label?
private :get_public_suffix_domain
end

def initialize(addressable_uri, public_suffix_domain)
Expand Down Expand Up @@ -189,7 +193,8 @@ def normalized
normalized_url.host = normalized_host
normalized_url.path = normalized_path

self.class.parse(normalized_url)
public_suffix_domain = self.class.send(:get_public_suffix_domain, normalized_url.host)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling this private class method here, then passing the result to the .new on the line below, we could make get_public_suffix_domain a private instance method instead and call it inside the initializer.

We still need to call the private .new method, but I don't see how we could get around that.

diff --git a/lib/twingly/url.rb b/lib/twingly/url.rb
index 83a734b..af99fcf 100644
--- a/lib/twingly/url.rb
+++ b/lib/twingly/url.rb
@@ -72,10 +72,7 @@ module Twingly
         # URLs that can't be normalized should not be valid
         try_addressable_normalize(addressable_uri)
 
-        host = addressable_uri.host
-        public_suffix_domain = get_public_suffix_domain(addressable_uri.host)
-
-        new(addressable_uri, public_suffix_domain)
+        new(addressable_uri)
       rescue *ERRORS_TO_EXTEND => error
         error.extend(Twingly::URL::Error)
         raise
@@ -120,13 +117,6 @@ module Twingly
         label.match?(LETTERS_DIGITS_HYPHEN)
       end
 
-      def get_public_suffix_domain(host)
-        public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL, default_rule: nil)
-        raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?
-        raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
-        public_suffix_domain
-      end
-
       private :new
       private :internal_parse
       private :clean_input
@@ -134,12 +124,11 @@ module Twingly
       private :try_addressable_normalize
       private :valid_hostname?
       private :valid_label?
-      private :get_public_suffix_domain
     end
 
-    def initialize(addressable_uri, public_suffix_domain)
+    def initialize(addressable_uri)
       @addressable_uri      = addressable_uri
-      @public_suffix_domain = public_suffix_domain
+      @public_suffix_domain = get_public_suffix_domain(addressable_uri.host)
     end
 
     def scheme
@@ -193,8 +182,7 @@ module Twingly
       normalized_url.host   = normalized_host
       normalized_url.path   = normalized_path
 
-      public_suffix_domain = self.class.send(:get_public_suffix_domain, normalized_url.host)
-      self.class.send(:new, normalized_url, public_suffix_domain)
+      self.class.send(:new, normalized_url)
     end
 
     def normalized_scheme
@@ -262,6 +250,13 @@ module Twingly
 
     attr_reader :addressable_uri, :public_suffix_domain
 
+    def get_public_suffix_domain(host)
+      public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL, default_rule: nil)
+      raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?
+      raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
+      public_suffix_domain
+    end
+
     def normalize_blogspot(host, domain)
       if domain.sld.downcase == "blogspot"
         host.sub(STARTS_WITH_WWW, "").sub(/#{domain.tld}\z/i, "com")

self.class.send(:new, normalized_url, public_suffix_domain)
end

def normalized_scheme
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end

config.order = :random
config.filter_run_when_matching :focus

Kernel.srand config.seed
end
Loading