Class: Dominion::DomainName

Inherits:
Object
  • Object
show all
Defined in:
lib/dominion/domain_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ DomainName

takes a domain string as argument; i.e. the hostname part of a URL



20
21
22
23
24
# File 'lib/dominion/domain_name.rb', line 20

def initialize(s)
  raise "No DomainSuffixRule rules loaded" unless self.class.rules
  @labels = s.strip.gsub(/\A\.|\.\z/, '').split(".").reverse
  @rule   = self.class.rules.find { |rule| rule =~ self } or raise "Domain #{s.inspect} didn't match any rule."
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



6
7
8
# File 'lib/dominion/domain_name.rb', line 6

def labels
  @labels
end

#ruleObject (readonly)

Returns the value of attribute rule.



6
7
8
# File 'lib/dominion/domain_name.rb', line 6

def rule
  @rule
end

Class Method Details

.load_rules_from_file(path) ⇒ Object

takes the path to a rule file in the format defined in publicsuffix.org/list/



15
16
17
# File 'lib/dominion/domain_name.rb', line 15

def self.load_rules_from_file(path)
  rules open(path, "r:UTF-8").lines.reject { |s| s =~ %r[\A\s*(//.*)?\Z\n?] }.map { |s| DomainSuffixRule.new(s) }.sort << DomainSuffixRule.new("*")
end

.rules(rules = nil) ⇒ Object

returns the current rule list or takes a new rule list; rules must be DomainSuffixRule instances.



9
10
11
12
# File 'lib/dominion/domain_name.rb', line 9

def self.rules(rules = nil)
  @rules = rules if rules
  @rules || (superclass.rules if superclass.respond_to?(:rules))
end

Instance Method Details

#baseObject Also known as: naked, root, apex

returns the base domain; i.e. the registered domain



45
46
47
# File 'lib/dominion/domain_name.rb', line 45

def base
  domain(1)
end

#base?Boolean Also known as: naked?, root?, apex?

is this the root domain?

Returns:

  • (Boolean)


32
33
34
# File 'lib/dominion/domain_name.rb', line 32

def base?
  labels.length == rule.length.succ
end

#domain(non_tld_label_count = 1) ⇒ Object

return the domain with the specified number of extra labels over the TLD. Used internally.



59
60
61
# File 'lib/dominion/domain_name.rb', line 59

def domain(non_tld_label_count = 1)
  labels[0, rule.length + non_tld_label_count].reverse.join(".")
end

#fullObject Also known as: to_s

returns the complete hostname



53
54
55
# File 'lib/dominion/domain_name.rb', line 53

def full
  domain(labels.length - rule.length)
end

#tldObject

returns the TLD part of the domain



40
41
42
# File 'lib/dominion/domain_name.rb', line 40

def tld
  domain(0)
end

#tld?Boolean

is this a TLD, ccTLD, ccSLD, etc.?

Returns:

  • (Boolean)


27
28
29
# File 'lib/dominion/domain_name.rb', line 27

def tld?
  labels.length == rule.length
end