Class: UrlParser::Domain

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @original   = name.to_s.downcase.chomp('.')
  @name       = normalize
  @errors     = []
  @validated  = false
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



22
23
24
# File 'lib/url_parser/domain.rb', line 22

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/url_parser/domain.rb', line 20

def name
  @name
end

#originalObject (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

#labelsObject



33
34
35
# File 'lib/url_parser/domain.rb', line 33

def labels
  PublicSuffix::Domain.domain_to_labels(name)
end

#suffixObject



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

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/url_parser/domain.rb', line 48

def valid?
  validate unless @validated
  errors.empty?
end