123456789_123456789_123456789_123456789_123456789_

Module: ActionDispatch::Http::URL::DomainExtractor

Relationships & Source Files
Defined in: actionpack/lib/action_dispatch/http/url.rb

Overview

DomainExtractor provides utility methods for extracting domain and subdomain information from host strings. This module is used internally by Action Dispatch to parse host names and separate the domain from subdomains based on the top-level domain (TLD) length.

The module assumes a standard domain structure where domains consist of:

  • Subdomains (optional, can be multiple levels)

  • Domain name

  • Top-level domain (TLD, can be multiple levels like .co.uk)

For example, in “api.staging.example.co.uk”:

  • Subdomains: [“api”, “staging”]

  • Domain: “example.co.uk” (with tld_length=2)

  • TLD: “co.uk”

Instance Method Summary

Instance Method Details

#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 ActionDispatch::Http::URL.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"
[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/url.rb', line 64

def domain_from(host, tld_length)
  host.split(".").last(1 + tld_length).join(".")
end

#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 ActionDispatch::Http::URL.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"]
[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/url.rb', line 104

def subdomains_from(host, tld_length)
  parts = host.split(".")
  parts[0..-(tld_length + 2)]
end