Class: UrlParser::Domain
- Inherits:
-
Object
- Object
- UrlParser::Domain
- Extended by:
- Forwardable
- Defined in:
- lib/url_parser/domain.rb
Constant Summary collapse
- VALID_LABEL =
/^(?!\-)[a-z0-9\-]*(?!\-)$/i
- SUFFIX_DEFAULTS =
{ subdomain: nil, domain: nil, tld: nil, sld: nil, trd: nil, to_s: '' }
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Domain
constructor
A new instance of Domain.
- #labels ⇒ Object
- #suffix ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, options = {}) ⇒ Domain
Returns a new instance of Domain.
26 27 28 29 30 31 |
# File 'lib/url_parser/domain.rb', line 26 def initialize(name, = {}) @original = name.to_s.downcase.chomp('.') @name = normalize @errors = [] @validated = false end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
22 23 24 |
# File 'lib/url_parser/domain.rb', line 22 def errors @errors end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/url_parser/domain.rb', line 20 def name @name end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
20 21 22 |
# File 'lib/url_parser/domain.rb', line 20 def original @original end |
Instance Method Details
#labels ⇒ Object
33 34 35 |
# File 'lib/url_parser/domain.rb', line 33 def labels PublicSuffix::Domain.domain_to_labels(name) end |
#suffix ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/url_parser/domain.rb', line 37 def suffix @suffix = begin PublicSuffix.parse(name) rescue self.errors << "'#{original}' is not a valid domain" OpenStruct.new(SUFFIX_DEFAULTS).tap do |os| os.instance_eval('undef to_s') end end end |
#valid? ⇒ Boolean
48 49 50 51 |
# File 'lib/url_parser/domain.rb', line 48 def valid? validate unless @validated errors.empty? end |