Class: Coppertone::Utils::DomainUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/coppertone/utils/domain_utils.rb

Overview

A utility class that includes methods for working with domain names.

Constant Summary collapse

NO_DASH_NONNUMERIC_REGEXP =
/\A[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*\z/
NO_DASH_REGEXP =
/\A[a-zA-Z0-9]+\z/
DASH_REGEXP =
/\A[a-zA-Z0-9]+\-[a-zA-Z0-9\-]*[a-zA-Z0-9]+\z/

Class Method Summary collapse

Class Method Details

.macro_expanded_domain(domain) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/coppertone/utils/domain_utils.rb', line 62

def self.macro_expanded_domain(domain)
  return nil if domain.blank?
  labels = to_ascii_labels(domain)
  domain = labels.join('.')
  while domain.length > 253
    labels = labels.drop(1)
    domain = labels.join('.')
  end
  domain
end

.normalized_domain(domain) ⇒ Object



33
34
35
# File 'lib/coppertone/utils/domain_utils.rb', line 33

def self.normalized_domain(domain)
  to_ascii_labels(domain).join('.')
end

.parent_domain(domain) ⇒ Object



22
23
24
25
26
27
# File 'lib/coppertone/utils/domain_utils.rb', line 22

def self.parent_domain(domain)
  labels = to_labels(domain)
  return '.' if labels.size == 1
  labels.shift
  labels.join('.')
end

.subdomain_of?(subdomain_candidate, domain) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/coppertone/utils/domain_utils.rb', line 73

def self.subdomain_of?(subdomain_candidate, domain)
  subdomain_labels = to_ascii_labels(subdomain_candidate)
  domain_labels = to_ascii_labels(domain)
  num_labels_in_domain = domain_labels.length
  return false if subdomain_labels.length <= domain_labels.length
  subdomain_labels.last(num_labels_in_domain) == domain_labels
end

.subdomain_or_same?(candidate, domain) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/coppertone/utils/domain_utils.rb', line 81

def self.subdomain_or_same?(candidate, domain)
  return false unless valid?(domain) && valid?(candidate)
  return true if normalized_domain(domain) == normalized_domain(candidate)
  subdomain_of?(candidate, domain)
end

.to_ascii_labels(domain) ⇒ Object



29
30
31
# File 'lib/coppertone/utils/domain_utils.rb', line 29

def self.to_ascii_labels(domain)
  Addressable::IDNA.to_ascii(domain).split('.').map(&:downcase)
end

.to_labels(domain) ⇒ Object



18
19
20
# File 'lib/coppertone/utils/domain_utils.rb', line 18

def self.to_labels(domain)
  domain.split('.')
end

.valid?(domain) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
# File 'lib/coppertone/utils/domain_utils.rb', line 8

def self.valid?(domain)
  return false if domain.blank?
  labels = to_ascii_labels(domain)
  return false if labels.length <= 1
  return false if domain.length > 253
  return false if labels.any? { |l| !valid_label?(l) }
  return false unless valid_tld?(labels.last)
  true
end

.valid_hostname_label?(l) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/coppertone/utils/domain_utils.rb', line 41

def self.valid_hostname_label?(l)
  return false unless valid_label?(l)
  NO_DASH_REGEXP.match(l) || DASH_REGEXP.match(l)
end

.valid_label?(l) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/coppertone/utils/domain_utils.rb', line 58

def self.valid_label?(l)
  !l.empty? && (l.length <= 63) && !l.match(/\s/)
end

.valid_ldh_domain?(domain) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/coppertone/utils/domain_utils.rb', line 51

def self.valid_ldh_domain?(domain)
  return false unless valid?(domain)
  labels = to_ascii_labels(domain)
  return false unless valid_tld?(labels.last)
  labels.all? { |l| valid_hostname_label?(l) }
end

.valid_tld?(l) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/coppertone/utils/domain_utils.rb', line 46

def self.valid_tld?(l)
  return false unless valid_label?(l)
  NO_DASH_NONNUMERIC_REGEXP.match(l) || DASH_REGEXP.match(l)
end