Skip to content

Commit 4c1f92b

Browse files
committed
Don't redo the parsing on when normalizing the url.
1 parent e20f5fc commit 4c1f92b

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

lib/twingly/url.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ def internal_parse(input)
7373
try_addressable_normalize(addressable_uri)
7474

7575
host = addressable_uri.host
76-
public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL,
77-
default_rule: nil)
78-
raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?
79-
80-
raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
76+
public_suffix_domain = get_public_suffix_domain(addressable_uri.host)
8177

8278
new(addressable_uri, public_suffix_domain)
8379
rescue *ERRORS_TO_EXTEND => error
@@ -124,13 +120,22 @@ def valid_label?(label)
124120
label.match?(LETTERS_DIGITS_HYPHEN)
125121
end
126122

127-
private :new
123+
def get_public_suffix_domain(host)
124+
public_suffix_domain = PublicSuffix.parse(host, list: CUSTOM_PSL,
125+
default_rule: nil)
126+
raise Twingly::URL::Error::ParseError if public_suffix_domain.nil?
127+
raise Twingly::URL::Error::ParseError if public_suffix_domain.sld.nil?
128+
public_suffix_domain
129+
end
130+
131+
protected :new
128132
private :internal_parse
129133
private :clean_input
130134
private :strip_whitespace
131135
private :try_addressable_normalize
132136
private :valid_hostname?
133137
private :valid_label?
138+
private :get_public_suffix_domain
134139
end
135140

136141
def initialize(addressable_uri, public_suffix_domain)
@@ -189,7 +194,8 @@ def normalized
189194
normalized_url.host = normalized_host
190195
normalized_url.path = normalized_path
191196

192-
self.class.parse(normalized_url)
197+
public_suffix_domain = self.class.send(:get_public_suffix_domain, normalized_url.host)
198+
self.class.send(:new, normalized_url, public_suffix_domain)
193199
end
194200

195201
def normalized_scheme

spec/lib/twingly/url_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def leading_and_trailing_whitespace
229229
context "when called from the outside" do
230230
it "raises an error" do
231231
expect { described_class.new("a", "b") }.
232-
to raise_error(NoMethodError, /private method `new' called for/)
232+
to raise_error(NoMethodError, /protected method `new' called for/)
233233
end
234234
end
235235
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
end
1111

1212
config.order = :random
13+
config.filter_run_when_matching :focus
1314

1415
Kernel.srand config.seed
1516
end

0 commit comments

Comments
 (0)