Class: UniformResourceIdentifier::Host

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsable

parse

Constructor Details

#initialize(host = nil) ⇒ Host

Returns a new instance of Host.



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

def initialize(host=nil)
  if host.respond_to?(:to_str) 
    begin
      pss = PublicSuffixService.parse(host)
      
      @subdomain = pss.trd
      @domain = Domain.new(:sld => pss.sld, :tld => pss.tld, :valid => true)
    rescue PublicSuffixService::DomainInvalid
      # We couldn't parse your tld (public suffix) =(
      @domain = Domain.new(:sld => nil, :tld => host, :valid => false) # TODO: is this proper? see Domain#to_s
    end
  elsif host.respond_to?(:to_hash)
    host = host.to_hash.symbolize_keys
    
    @subdomain = host[:subdomain]
    @domain = Domain.new(host[:domain])
  else
    raise(TypeError, "host must either be a String or a Hash") unless host.blank?
  end
end

Instance Attribute Details

#subdomainObject

#

Attributes = #

#


51
52
53
# File 'lib/uniform_resource_identifier/host.rb', line 51

def subdomain
  @subdomain
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/uniform_resource_identifier/host.rb', line 43

def blank?
  @subdomain.blank? && @domain.blank?
end

#domainObject



57
58
59
# File 'lib/uniform_resource_identifier/host.rb', line 57

def domain
  @domain ||= Domain.new
end

#domain=(domain) ⇒ Object



61
62
63
# File 'lib/uniform_resource_identifier/host.rb', line 61

def domain=(domain)
  @domain = Domain.parse(domain)
end

#sldObject

#

Delegates = #

#


69
70
71
# File 'lib/uniform_resource_identifier/host.rb', line 69

def sld
  self.domain.sld
end

#sld=(sld) ⇒ Object



73
74
75
# File 'lib/uniform_resource_identifier/host.rb', line 73

def sld=(sld)
  self.domain.sld = sld
end

#tldObject



77
78
79
# File 'lib/uniform_resource_identifier/host.rb', line 77

def tld
  self.domain.tld
end

#tld=(tld) ⇒ Object



81
82
83
# File 'lib/uniform_resource_identifier/host.rb', line 81

def tld=(tld)
  self.domain.tld = tld
end

#to_hObject



36
37
38
39
40
41
# File 'lib/uniform_resource_identifier/host.rb', line 36

def to_h
  {
    :subdomain => @subdomain,
    :domain    => @domain.nil? ? nil : @domain.to_h
  }
end

#to_sObject



31
32
33
34
# File 'lib/uniform_resource_identifier/host.rb', line 31

def to_s
  subdomain = "#{@subdomain}." unless @subdomain.nil?
  "#{subdomain}#{@domain}"
end