Module: PublicSuffix
- Defined in:
- lib/public_suffix.rb,
lib/public_suffix/list.rb,
lib/public_suffix/rule.rb,
lib/public_suffix/domain.rb,
lib/public_suffix/errors.rb,
lib/public_suffix/version.rb
Overview
Public Suffix
Domain name parser based on the Public Suffix List.
Copyright © 2009-2017 Simone Carletti <[email protected]>
Defined Under Namespace
Modules: Rule Classes: Domain, DomainInvalid, DomainNotAllowed, Error, List
Constant Summary collapse
- DOT =
".".freeze
- BANG =
"!".freeze
- STAR =
"*".freeze
- VERSION =
The current library version.
"2.0.5".freeze
Class Method Summary collapse
-
.decompose(name, rule) ⇒ Object
private.
-
.domain(name, **options) ⇒ String
Attempt to parse the name and returns the domain, if valid.
-
.normalize(name) ⇒ Object
Pretend we know how to deal with user input.
-
.parse(name, list: List.default, default_rule: list.default_rule, ignore_private: false) ⇒ PublicSuffix::Domain
Parses
nameand returns the Domain instance. -
.valid?(name, list: List.default, default_rule: list.default_rule, ignore_private: false) ⇒ Boolean
Checks whether
domainis assigned and allowed, without actually parsing it.
Class Method Details
.decompose(name, rule) ⇒ Object
private
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/public_suffix.rb', line 144 def self.decompose(name, rule) left, right = rule.decompose(name) parts = left.split(DOT) # If we have 0 parts left, there is just a tld and no domain or subdomain # If we have 1 part left, there is just a tld, domain and not subdomain # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain tld = right sld = parts.empty? ? nil : parts.pop trd = parts.empty? ? nil : parts.join(DOT) Domain.new(tld, sld, trd) end |
.domain(name, **options) ⇒ String
Attempt to parse the name and returns the domain, if valid.
This method doesn’t raise. Instead, it returns nil if the domain is not valid for whatever reason.
135 136 137 138 139 |
# File 'lib/public_suffix.rb', line 135 def self.domain(name, **) parse(name, **).domain rescue PublicSuffix::Error nil end |
.normalize(name) ⇒ Object
Pretend we know how to deal with user input.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/public_suffix.rb', line 159 def self.normalize(name) name = name.to_s.dup name.strip! name.chomp!(DOT) name.downcase! return DomainInvalid.new("Name is blank") if name.empty? return DomainInvalid.new("Name starts with a dot") if name.start_with?(DOT) return DomainInvalid.new("%s is not expected to contain a scheme" % name) if name.include?("://") name end |
.parse(name, list: List.default, default_rule: list.default_rule, ignore_private: false) ⇒ PublicSuffix::Domain
Parses name and returns the Domain instance.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/public_suffix.rb', line 63 def self.parse(name, list: List.default, default_rule: list.default_rule, ignore_private: false) what = normalize(name) raise what if what.is_a?(DomainInvalid) rule = list.find(what, default: default_rule, ignore_private: ignore_private) # rubocop:disable Style/IfUnlessModifier if rule.nil? raise DomainInvalid, "`#{what}` is not a valid domain" end if rule.decompose(what).last.nil? raise DomainNotAllowed, "`#{what}` is not allowed according to Registry policy" end # rubocop:enable Style/IfUnlessModifier decompose(what, rule) end |
.valid?(name, list: List.default, default_rule: list.default_rule, ignore_private: false) ⇒ Boolean
Checks whether domain is assigned and allowed, without actually parsing it.
This method doesn’t care whether domain is a domain or subdomain. The validation is performed using the default List.
118 119 120 121 122 123 124 125 |
# File 'lib/public_suffix.rb', line 118 def self.valid?(name, list: List.default, default_rule: list.default_rule, ignore_private: false) what = normalize(name) return false if what.is_a?(DomainInvalid) rule = list.find(what, default: default_rule, ignore_private: ignore_private) !rule.nil? && !rule.decompose(what).last.nil? end |