Class: Domainatrix::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/domainatrix/url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Url

Returns a new instance of Url.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/domainatrix/url.rb', line 6

def initialize(attrs = {})
  @scheme = attrs[:scheme] || ''
  @host = attrs[:host] || ''
  @url = attrs[:url] || ''
  @public_suffix = attrs[:public_suffix] || ''
  @domain = attrs[:domain] || ''
  @subdomain = attrs[:subdomain] || ''
  @path = attrs[:path] || ''
  @ip_address = attrs[:ip_address]

end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def domain
  @domain
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def host
  @host
end

#ip_addressObject

Returns the value of attribute ip_address.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def ip_address
  @ip_address
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def path
  @path
end

#public_suffixObject

Returns the value of attribute public_suffix.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def public_suffix
  @public_suffix
end

#schemeObject

Returns the value of attribute scheme.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def scheme
  @scheme
end

#subdomainObject

Returns the value of attribute subdomain.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def subdomain
  @subdomain
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/domainatrix/url.rb', line 4

def url
  @url
end

Instance Method Details

#canonical(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/domainatrix/url.rb', line 18

def canonical(options = {})
  public_suffix_parts = @public_suffix.split(".")
  url = "#{public_suffix_parts.reverse.join(".")}.#{@domain}"
  if @subdomain && !@subdomain.empty?
    subdomain_parts = @subdomain.split(".")
    url << ".#{subdomain_parts.reverse.join(".")}"
  end
  url << @path if @path

  url
end

#domain_with_public_suffixObject Also known as: domain_with_tld



30
31
32
# File 'lib/domainatrix/url.rb', line 30

def domain_with_public_suffix
  [@domain, @public_suffix].compact.reject{|s|s==''}.join('.')
end

#to_sObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/domainatrix/url.rb', line 35

def to_s
  if @scheme.nil? || @scheme.empty?
    scheme = ''
  else
    scheme = "#{@scheme}://"
  end

  parts = []
  parts << @subdomain if @subdomain and !@subdomain.empty?
  parts << @domain if @domain and !@domain.empty?
  parts << @public_suffix if @public_suffix and !@public_suffix.empty?

  "#{scheme}#{parts.join('.')}#{@path}"
end