instance method
domain_from
Ruby on Rails edge
Since v8.1.4Available in: v8.1.4 edge
Signature
domain_from(host, tld_length)
Extracts the domain part from a host string, including the specified number of top-level domain components.
The domain includes the main domain name plus the TLD components. The tld_length parameter specifies how many components from the right should be considered part of the TLD.
Parameters
host-
The host string to extract the domain from.
tld_length-
The number of domain components that make up the TLD. For example, use 1 for “.com” or 2 for “.co.uk”.
Examples
# Standard TLD (tld_length = 1)
DomainExtractor.domain_from("www.example.com", 1)
# => "example.com"
# Country-code TLD (tld_length = 2)
DomainExtractor.domain_from("www.example.co.uk", 2)
# => "example.co.uk"
# Multiple subdomains
DomainExtractor.domain_from("api.staging.myapp.herokuapp.com", 1)
# => "herokuapp.com"
# Single component (returns the host itself)
DomainExtractor.domain_from("localhost", 1)
# => "localhost"
Parameters
-
hostreq -
tld_lengthreq
Source
# File actionpack/lib/action_dispatch/http/url.rb, line 62
def domain_from(host, tld_length)
host.split(".").last(1 + tld_length).join(".")
end
Defined in actionpack/lib/action_dispatch/http/url.rb line 62
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Http::URL::DomainExtractor