Class: Dominion::DomainName
- Inherits:
-
Object
- Object
- Dominion::DomainName
- Defined in:
- lib/dominion/domain_name.rb
Instance Attribute Summary collapse
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Class Method Summary collapse
-
.load_rules_from_file(path) ⇒ Object
takes the path to a rule file in the format defined in publicsuffix.org/format/.
-
.rules(rules = nil) ⇒ Object
returns the current rule list or takes a new rule list; rules must be DomainSuffixRule instances.
Instance Method Summary collapse
-
#base ⇒ Object
returns the base domain; i.e.
-
#domain(non_tld_label_count = 1) ⇒ Object
return the domain with the specified number of extra labels over the TLD.
-
#initialize(s) ⇒ DomainName
constructor
takes a domain string as argument; i.e.
-
#tld ⇒ Object
returns the TLD part of the domain.
-
#tld? ⇒ Boolean
is this a TLD, ccTLD, ccSLD, etc.?.
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
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
6 7 8 |
# File 'lib/dominion/domain_name.rb', line 6 def labels @labels end |
#rule ⇒ Object (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/format/
15 16 17 |
# File 'lib/dominion/domain_name.rb', line 15 def self.load_rules_from_file(path) rules open(path).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 end |
Instance Method Details
#base ⇒ Object
returns the base domain; i.e. the registered domain
37 38 39 |
# File 'lib/dominion/domain_name.rb', line 37 def base domain(1) end |
#domain(non_tld_label_count = 1) ⇒ Object
return the domain with the specified number of extra labels over the TLD. Used internally.
42 43 44 |
# File 'lib/dominion/domain_name.rb', line 42 def domain(non_tld_label_count = 1) labels[0, rule.length + non_tld_label_count].reverse.join(".") end |
#tld ⇒ Object
returns the TLD part of the domain
32 33 34 |
# File 'lib/dominion/domain_name.rb', line 32 def tld domain(0) end |
#tld? ⇒ Boolean
is this a TLD, ccTLD, ccSLD, etc.?
27 28 29 |
# File 'lib/dominion/domain_name.rb', line 27 def tld? labels.length == rule.length end |