Module: DomainPrefix
Defined Under Namespace
Classes: Tree
Constant Summary collapse
- SEPARATOR =
'.'.freeze
- TLDIFIER_SOURCE_FILE =
File.(File.join('..', 'data', 'effective_tld_names.dat'), File.dirname(__FILE__))
- TLD_SET =
begin File.open(TLDIFIER_SOURCE_FILE, 'r:UTF-8') do |f| f.read.split(/\n/).collect do |line| line.sub(%r[//.*], '').sub(/\s+$/, '') end.reject(&:empty?).freeze end end
- TLD_NAMES =
TLD_SET.sort_by do |d| [ -d.length, d ] end.freeze
- TLD_TREE =
TLD_NAMES.inject(Tree.new) do |t, name| t.insert(name) end.freeze
- NONPUBLIC_TLD =
{ 'local' => true }.freeze
Instance Method Summary collapse
-
#name(fqdn) ⇒ Object
Returns the name component of a given domain or nil if one cannot be determined.
-
#public_suffix(domain) ⇒ Object
Returns the public suffix (e.g. “co.uk”) for a given domain or nil if one cannot be determined.
-
#public_tld?(tld) ⇒ Boolean
Returns true if the given tld is listed as public, false otherwise.
-
#registered_domain(fqdn, rules = :strict) ⇒ Object
Returns the registered domain name for a given FQDN or nil if one cannot be determined.
-
#rfc3492_canonical_domain(domain) ⇒ Object
Returns a cleaned up, canonical version of a domain name.
-
#tld(fqdn) ⇒ Object
Returns the very top-level domain for a given domain, or nil if one cannot be determined.
Instance Method Details
#name(fqdn) ⇒ Object
Returns the name component of a given domain or nil if one cannot be determined.
99 100 101 102 103 104 105 |
# File 'lib/domain_prefix.rb', line 99 def name(fqdn) if (fqdn = registered_domain(fqdn)) fqdn.split(SEPARATOR).first else nil end end |
#public_suffix(domain) ⇒ Object
Returns the public suffix (e.g. “co.uk”) for a given domain or nil if one cannot be determined.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/domain_prefix.rb', line 71 def public_suffix(domain) return unless (domain) components = rfc3492_canonical_domain(domain).split(SEPARATOR) return if (components.empty? or components.find(&:empty?)) return unless (public_tld?(components.last)) suffix = TLD_TREE.follow(components) return unless (suffix) suffix.shift suffix.join(SEPARATOR) end |
#public_tld?(tld) ⇒ Boolean
Returns true if the given tld is listed as public, false otherwise.
39 40 41 |
# File 'lib/domain_prefix.rb', line 39 def public_tld?(tld) !NONPUBLIC_TLD.key?(tld) end |
#registered_domain(fqdn, rules = :strict) ⇒ Object
Returns the registered domain name for a given FQDN or nil if one cannot be determined.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/domain_prefix.rb', line 45 def registered_domain(fqdn, rules = :strict) return unless (fqdn) components = rfc3492_canonical_domain(fqdn).split(SEPARATOR) return if (components.empty? or components.find(&:empty?)) if (rules == :strict) return unless (self.public_tld?(components.last)) end suffix = TLD_TREE.follow(components) unless (suffix) if (rules == :relaxed and components.length >= 2 and !TLD_TREE[components[-1]]) return components.last(2).join(SEPARATOR) else return end end suffix.join(SEPARATOR) end |
#rfc3492_canonical_domain(domain) ⇒ Object
Returns a cleaned up, canonical version of a domain name.
32 33 34 35 36 |
# File 'lib/domain_prefix.rb', line 32 def rfc3492_canonical_domain(domain) # FIX: Full implementation of RFC3429 required. # http://www.ietf.org/rfc/rfc3492.txt domain and domain.downcase end |
#tld(fqdn) ⇒ Object
Returns the very top-level domain for a given domain, or nil if one cannot be determined.
91 92 93 94 95 |
# File 'lib/domain_prefix.rb', line 91 def tld(fqdn) suffix = public_suffix(rfc3492_canonical_domain(fqdn)) suffix and suffix.split(SEPARATOR).last end |