Class: UniformResourceIdentifier::Domain

Inherits:
Object
  • Object
show all
Extended by:
Parsable
Defined in:
lib/uniform_resource_identifier/domain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsable

parse

Constructor Details

#initialize(domain = nil) ⇒ Domain

Returns a new instance of Domain.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/uniform_resource_identifier/domain.rb', line 9

def initialize(domain=nil)
  if domain.respond_to?(:to_str)
    begin
      pss = PublicSuffix.parse(domain)
      
      @sld, @tld = pss.sld, pss.tld
      @valid_public_suffix = true
    rescue PublicSuffix::DomainInvalid
      # We couldn't parse your tld (public suffix) =(
      @tld = domain
      @valid_public_suffix = false
    end
  elsif domain.respond_to?(:to_hash)
    domain.to_hash.symbolize_keys
    @sld, @tld = domain.values_at(:sld, :tld)
    @valid_public_suffix = domain[:valid]
  else
    raise(TypeError, "domain must either be a String or a Hash") unless domain.nil?
  end
end

Instance Attribute Details

#sldObject

#

Attributes = #

#


47
48
49
# File 'lib/uniform_resource_identifier/domain.rb', line 47

def sld
  @sld
end

#tldObject

Returns the value of attribute tld.



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

def tld
  @tld
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/uniform_resource_identifier/domain.rb', line 39

def blank?
  @sld.blank? && @tld.blank?
end

#to_hObject



35
36
37
# File 'lib/uniform_resource_identifier/domain.rb', line 35

def to_h
  { :sld => @sld, :tld => @tld }
end

#to_sObject



30
31
32
33
# File 'lib/uniform_resource_identifier/domain.rb', line 30

def to_s
  sld = "#{@sld}." unless @sld.nil?
  "#{sld}#{@tld}"
end

#valid_public_suffix?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/uniform_resource_identifier/domain.rb', line 50

def valid_public_suffix?
  @valid_public_suffix
end