Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle localhost with a port

  • Loading branch information...
commit b5fb43c2f9f4a3501b9e0bf7198d93a9ac7b0f92 1 parent eac1fe9
Kevin Wang and Paul Elliott authored Hashrocket Workstation committed
View
23 lib/domainatrix/domain_parser.rb
@@ -38,7 +38,14 @@ def parse(url)
else
path = uri.path
end
- parse_domains_from_host(uri.host).merge({
+
+ if uri.host == 'localhost'
+ uri_hash = { :public_suffix => '', :domain => 'localhost', :subdomain => '' }
+ else
+ uri_hash = parse_domains_from_host(uri.host || uri.basename)
+ end
+
+ uri_hash.merge({
:scheme => uri.scheme,
:host => uri.host,
:path => path,
@@ -47,32 +54,32 @@ def parse(url)
end
def parse_domains_from_host(host)
+ return {} unless host
parts = host.split(".").reverse
public_suffix = []
domain = ""
subdomains = []
sub_hash = @public_suffixes
- parts.each_index do |i|
- part = parts[i]
- sub_parts = sub_hash[part]
- sub_hash = sub_parts
+ parts.each_with_index do |part, i|
+ sub_hash = sub_parts = sub_hash[part] || {}
if sub_parts.has_key? "*"
public_suffix << part
public_suffix << parts[i+1]
domain = parts[i+2]
- subdomains = parts.slice(i+3, parts.size)
+ subdomains = parts.slice(i+3, parts.size) || []
break
elsif sub_parts.empty? || !sub_parts.has_key?(parts[i+1])
public_suffix << part
domain = parts[i+1]
- subdomains = parts.slice(i+2, parts.size)
+ subdomains = parts.slice(i+2, parts.size) || []
break
else
public_suffix << part
end
end
+
{:public_suffix => public_suffix.reverse.join("."), :domain => domain, :subdomain => subdomains.reverse.join(".")}
end
end
-end
+end
View
2  lib/domainatrix/url.rb
@@ -25,7 +25,7 @@ def canonical(options = {})
end
def domain_with_public_suffix
- "#{@domain}.#{@public_suffix}"
+ [@domain, @public_suffix].compact.reject{|s|s==''}.join('.')
end
alias domain_with_tld domain_with_public_suffix
View
12 spec/domainatrix_spec.rb
@@ -14,6 +14,18 @@
Domainatrix.parse("http://pauldix.co.uk").canonical.should == "uk.co.pauldix"
end
+ context 'localhost with a port' do
+ subject { Domainatrix.parse('localhost:3000') }
+ its(:scheme) { should == 'http' }
+ its(:host) { should == 'localhost' }
+ its(:url) { should == 'http://localhost:3000' }
+ its(:public_suffix) { should == '' }
+ its(:domain) { should == 'localhost' }
+ its(:subdomain) { should == '' }
+ its(:path) { should == '' }
+ its(:domain_with_tld) { should == 'localhost' }
+ end
+
context 'without a scheme' do
subject { Domainatrix.parse('www.pauldix.net') }
its(:scheme) { should == 'http' }
Please sign in to comment.
Something went wrong with that request. Please try again.