Module: DomainPrefix
Defined Under Namespace
Classes: Tree
Constant Summary collapse
- SEPARATOR =
Constants ============================================================
'.'.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.
104 105 106 107 108 109 110 |
# File 'lib/domain_prefix.rb', line 104 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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/domain_prefix.rb', line 76 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.
44 45 46 |
# File 'lib/domain_prefix.rb', line 44 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/domain_prefix.rb', line 50 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.
37 38 39 40 41 |
# File 'lib/domain_prefix.rb', line 37 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.
96 97 98 99 100 |
# File 'lib/domain_prefix.rb', line 96 def tld(fqdn) suffix = public_suffix(rfc3492_canonical_domain(fqdn)) suffix and suffix.split(SEPARATOR).last end |