-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add public_suffix method to allow users to access the PublicSuffix::Domain object easily. #510
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1155,6 +1155,7 @@ def host=(new_host) | |||||
# Reset dependent values | ||||||
@authority = nil | ||||||
@normalized_host = nil | ||||||
@public_suffix = nil | ||||||
remove_composite_values | ||||||
|
||||||
# Ensure we haven't created an invalid URI | ||||||
|
@@ -1198,7 +1199,7 @@ def hostname=(new_hostname) | |||||
# @example | ||||||
# Addressable::URI.parse("http://www.example.co.uk").tld # => "co.uk" | ||||||
def tld | ||||||
PublicSuffix.parse(self.host, ignore_private: true).tld | ||||||
public_suffix.tld | ||||||
end | ||||||
|
||||||
## | ||||||
|
@@ -1216,7 +1217,25 @@ def tld=(new_tld) | |||||
# @example | ||||||
# Addressable::URI.parse("http://www.example.co.uk").domain # => "example.co.uk" | ||||||
def domain | ||||||
PublicSuffix.domain(self.host, ignore_private: true) | ||||||
public_suffix.domain | ||||||
end | ||||||
|
||||||
## | ||||||
# Returns the first subdomain for this host. | ||||||
# | ||||||
# @example | ||||||
# Addressable::URI.parse("http://www.test.example.co.uk").domain # => "www" | ||||||
def subdomain | ||||||
subdomains.first | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still think there's ambiguity around what's a subdomain and what a method like this is expected to return.
This method would return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was modeling this off of Rails which returns the first subdomain, but let me know what you'd like this to do. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with that in Rails, can you link the source for method? When is it typically used? I'm not sure what we want here, if the method should exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multi-tenant applications or anything that uses subdomains (think Shopify) use this. I've always been surprised there's no way to get the subdomain(s) from URIs in Ruby. Rails will do it for the current request, but that's it and it's not Public Suffix aware. You can only specify a single TLD for your entire Rails app, so you need to reach for something else like addressable + public_suffix. https://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html#method-i-subdomain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any thoughts on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... if we should add this method, is it not better if it matches the
For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about not adding the |
||||||
end | ||||||
|
||||||
## | ||||||
# Returns the subdomains for this host. | ||||||
# | ||||||
# @example | ||||||
# Addressable::URI.parse("http://www.test.example.co.uk").domain # => ["www", "test"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
def subdomains | ||||||
public_suffix.trd.to_s.split(".") | ||||||
end | ||||||
|
||||||
## | ||||||
|
@@ -2540,6 +2559,14 @@ def force_utf8_encoding_if_needed(str) | |||||
|
||||||
private | ||||||
|
||||||
## | ||||||
# Returns a PublicSuffix::Domain object for this host. | ||||||
# | ||||||
# @api private | ||||||
def public_suffix | ||||||
@public_suffix ||= PublicSuffix.parse(self.host, ignore_private: true) | ||||||
end | ||||||
|
||||||
## | ||||||
# Resets instance variables | ||||||
# | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6799,3 +6799,23 @@ def to_str | |
expect(res).to be nil | ||
end | ||
end | ||
|
||
describe Addressable::URI, "subdomain" do | ||
it "should return the first subdomain" do | ||
expect(Addressable::URI.parse("http://www.test.example.org").subdomain).to eq("www") | ||
end | ||
|
||
it "should return nil if no subdomain" do | ||
expect(Addressable::URI.parse("http://example.co.uk").subdomain).to be_nil | ||
end | ||
end | ||
|
||
describe Addressable::URI, "subdomains" do | ||
it "should return the subdomains" do | ||
expect(Addressable::URI.parse("http://www.test.example.org").subdomains).to eq(["www", "test"]) | ||
end | ||
|
||
it "should return an empty array if no subdomain" do | ||
expect(Addressable::URI.parse("http://example.co.uk").subdomains).to eq([]) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the terminating newline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.