Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/joelvh/domainatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvh committed May 12, 2013
2 parents ddf9bef + c52e8df commit 14671ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/domainatrix/domain_parser.rb
Expand Up @@ -40,17 +40,21 @@ def parse(url)
path = uri.path
end

localhost_re = /(\A|\.)localhost\z/
localhost_re = /(\A|\.)localhost\z/
ip_re = /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/

if uri.host =~ localhost_re
uri_hash = { :public_suffix => '', :domain => 'localhost', :subdomain => uri.host.sub(localhost_re, '') }
uri_hash = { :public_suffix => '', :domain => 'localhost', :subdomain => uri.host.sub(localhost_re, ''), :localhost => true }
elsif uri.host =~ ip_re
uri_hash = { :public_suffix => '', :domain => uri.host, :subdomain => '', :ip => true }
else
uri_hash = parse_domains_from_host(uri.host || uri.basename)
end

uri_hash.merge({
:scheme => uri.scheme,
:host => uri.host,
:port => (uri.port == uri.default_port) ? nil : uri.port,
:path => path,
:url => url
})
Expand Down
32 changes: 29 additions & 3 deletions lib/domainatrix/url.rb
@@ -1,15 +1,18 @@
module Domainatrix
class Url
attr_reader :public_suffix, :domain, :subdomain, :path, :url, :scheme, :host
attr_reader :public_suffix, :domain, :subdomain, :path, :url, :scheme, :host, :port

def initialize(attrs = {})
@scheme = attrs[:scheme] || ''
@host = attrs[:host] || ''
@port = attrs[:port].to_s
@url = attrs[:url] || ''
@public_suffix = attrs[:public_suffix] || ''
@domain = attrs[:domain] || ''
@subdomain = attrs[:subdomain] || ''
@path = attrs[:path] || ''
@localhost = (attrs[:localhost] == true)
@ip = (attrs[:ip] == true)
end

def canonical(options = {})
Expand All @@ -23,11 +26,34 @@ def canonical(options = {})

url
end

def host_with_port
if !@port.empty?
"#{@host}:#{@port}"
else
host
end
end

def domain_with_port
if !@port.empty?
"#{domain_with_public_suffix}:#{@port}"
else
domain_with_public_suffix
end
end

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


def localhost?
@localhost
end

def ip?
@ip
end
end
end

0 comments on commit 14671ed

Please sign in to comment.