Class: Adomain

Inherits:
Object
  • Object
show all
Defined in:
lib/adomain.rb,
lib/adomain/version.rb

Constant Summary collapse

VERSION =
"0.2.4"

Class Method Summary collapse

Class Method Details

.[](string, domain = false, www = false) ⇒ Object

is a convenience method to subdomain the URL,

or optionally domain or subdomain_www.

Adomain["http://abc.xyz.com"]               # => "abc.xyz.com"
Adomain["http://abc.xyz.com", true]         # => "xyz.com"
Adomain["http://www.xyz.com", false, true]  # => "www.xyz.com"
Adomain["http://abc.xyz.com", false, false] # => "abc.xyz.com"


13
14
15
# File 'lib/adomain.rb', line 13

def [](string, domain = false, www = false)
  domain(string, domain, www)
end

.domain(string, domain = true, www = false) ⇒ Object

domain is the base method for only the domain from parse_for_domain

Adomain.domain "http://www.xyz.com" # => "xyz.com"
Adomain.domain "http://abc.xyz.com" # => "xyz.com"
Adomain.domain "http://xyz.com"     # => "xyz.com"


21
22
23
24
# File 'lib/adomain.rb', line 21

def domain(string, domain = true, www = false)
  opts = { :keep_www => www, :strip_subdomain => domain }
  parse_for_domain(string, opts)
end

.path(string) ⇒ Object

path is a wrapper around Addressable::URI’s path with a major difference – it removes the domain components from the string, if the domain is parseable. This is because Addressable leaves it in, for reasons I can’t understand.



56
57
58
59
60
61
62
63
64
65
# File 'lib/adomain.rb', line 56

def path(string)
  if self.subdomain_www(string)
    substring = /\A#{Regexp.quote(self.subdomain_www(string))}/
    Addressable::URI.parse(string).path.gsub(substring, '')
  else
    Addressable::URI.parse(string).path
  end
rescue Addressable::URI::InvalidURIError => e
  nil
end

.query_values(string) ⇒ Object

query_values is a wrapper around Addressable::URI’s query_values it is only included for convenience



69
70
71
72
73
# File 'lib/adomain.rb', line 69

def query_values(string)
  Addressable::URI.parse(string).query_values
rescue Addressable::URI::InvalidURIError => e
  nil
end

.scheme(string) ⇒ Object

scheme is a wrapper around Addressable::URI’s scheme it is only included for convenience



46
47
48
49
50
# File 'lib/adomain.rb', line 46

def scheme(string)
  Addressable::URI.parse(string).scheme
rescue Addressable::URI::InvalidURIError => e
  nil
end

.subdomain(string, www = false) ⇒ Object

subdomain is a convenience method for the domain with any subdomain except “www” from parse_for_domain

Adomain.subdomain "http://www.xyz.com" # => "xyz.com"
Adomain.subdomain "http://abc.xyz.com" # => "abc.xyz.com"
Adomain.subdomain "http://xyz.com"     # => "xyz.com"


31
32
33
# File 'lib/adomain.rb', line 31

def subdomain(string, www = false)
  domain(string, false, www)
end

.subdomain_www(string) ⇒ Object

subdomain_www is a convenience method for the domain with any subdomain including “www” from parse_for_domain

Adomain.subdomain_www "http://www.xyz.com" # => "www.xyz.com"
Adomain.subdomain_www "http://abc.xyz.com" # => "abc.xyz.com"
Adomain.subdomain_www "http://xyz.com"     # => "xyz.com"


40
41
42
# File 'lib/adomain.rb', line 40

def subdomain_www(string)
  subdomain(string, true)
end