instance method subdomains_from

Ruby on Rails edge

Since v8.1.4

Available in: v8.1.4 edge

Signature

subdomains_from(host, tld_length)

Extracts the subdomain components from a host string as an Array.

Returns all the components that come before the domain and TLD parts. The tld_length parameter is used to determine where the domain begins so that everything before it is considered a subdomain.

Parameters
host

The host string to extract subdomains from.

tld_length

The number of domain components that make up the TLD. This affects where the domain boundary is calculated.

Examples
# Standard TLD (tld_length = 1)
DomainExtractor.subdomains_from("www.example.com", 1)
# => ["www"]

# Country-code TLD (tld_length = 2)
DomainExtractor.subdomains_from("api.staging.example.co.uk", 2)
# => ["api", "staging"]

# No subdomains
DomainExtractor.subdomains_from("example.com", 1)
# => []

# Single subdomain with complex TLD
DomainExtractor.subdomains_from("www.mysite.co.uk", 2)
# => ["www"]

# Multiple levels of subdomains
DomainExtractor.subdomains_from("dev.api.staging.example.com", 1)
# => ["dev", "api", "staging"]

Parameters

host req
tld_length req
Source
# File actionpack/lib/action_dispatch/http/url.rb, line 102
        def subdomains_from(host, tld_length)
          parts = host.split(".")
          parts[0..-(tld_length + 2)]
        end

Defined in actionpack/lib/action_dispatch/http/url.rb line 102 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Http::URL::DomainExtractor

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close