Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/domainatrix/domain_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def parse(url)
:scheme => uri.scheme,
:host => uri.host,
:path => path,
:url => url
:url => url,
:port => uri.port || 80
})
end

Expand Down
4 changes: 3 additions & 1 deletion lib/domainatrix/url.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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] || ''
Expand All @@ -10,6 +10,7 @@ def initialize(attrs = {})
@domain = attrs[:domain] || ''
@subdomain = attrs[:subdomain] || ''
@path = attrs[:path] || ''
@port = attrs[:port]
end

def canonical(options = {})
Expand All @@ -19,6 +20,7 @@ def canonical(options = {})
subdomain_parts = @subdomain.split(".")
url << ".#{subdomain_parts.reverse.join(".")}"
end
url << ":#{@port}" if @port && @port != 80
url << @path if @path

url
Expand Down
5 changes: 5 additions & 0 deletions spec/domainatrix/domain_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
@domain_parser.parse("http://www.pauldix.net")[:host].should == "www.pauldix.net"
end

it "includes the original port" do
@domain_parser.parse("http://www.pauldix.net")[:port].should == 80
@domain_parser.parse("http://www.pauldix.net:8000")[:port].should == 8000
end

it "parses out the path" do
@domain_parser.parse("http://pauldix.net/foo.html?asdf=foo")[:path].should == "/foo.html?asdf=foo"
@domain_parser.parse("http://pauldix.net?asdf=foo")[:path].should == "?asdf=foo"
Expand Down
87 changes: 70 additions & 17 deletions spec/domainatrix/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,87 @@
Domainatrix::Url.new(:path => "/asdf.html").path.should == "/asdf.html"
end

it "has the port" do
Domainatrix::Url.new(:port => 8000).port.should == 8000
end

def should_canonicalize_url_for_port(given_port = nil, resulting_port = nil)
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net", :port => given_port).canonical.should == "net.pauldix#{resulting_port}"
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net", :port => given_port).canonical.should == "net.pauldix.foo#{resulting_port}"
Domainatrix::Url.new(:subdomain => "foo.bar", :domain => "pauldix", :public_suffix => "net", :port => given_port).canonical.should == "net.pauldix.bar.foo#{resulting_port}"
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "co.uk", :port => given_port).canonical.should == "uk.co.pauldix#{resulting_port}"
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "co.uk", :port => given_port).canonical.should == "uk.co.pauldix.foo#{resulting_port}"
Domainatrix::Url.new(:subdomain => "foo.bar", :domain => "pauldix", :public_suffix => "co.uk", :port => given_port).canonical.should == "uk.co.pauldix.bar.foo#{resulting_port}"
Domainatrix::Url.new(:subdomain => "", :domain => "pauldix", :public_suffix => "co.uk", :port => given_port).canonical.should == "uk.co.pauldix#{resulting_port}"
end

def should_canonicalize_url_without_port
should_canonicalize_url_for_port
end

it "canonicalizes the url" do
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net").canonical.should == "net.pauldix"
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net").canonical.should == "net.pauldix.foo"
Domainatrix::Url.new(:subdomain => "foo.bar", :domain => "pauldix", :public_suffix => "net").canonical.should == "net.pauldix.bar.foo"
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "co.uk").canonical.should == "uk.co.pauldix"
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "co.uk").canonical.should == "uk.co.pauldix.foo"
Domainatrix::Url.new(:subdomain => "foo.bar", :domain => "pauldix", :public_suffix => "co.uk").canonical.should == "uk.co.pauldix.bar.foo"
Domainatrix::Url.new(:subdomain => "", :domain => "pauldix", :public_suffix => "co.uk").canonical.should == "uk.co.pauldix"
should_canonicalize_url_without_port
should_canonicalize_url_for_port 80
should_canonicalize_url_for_port 8000, ":8000"
end

def should_canonicalize_url_with_path_for_port(given_port = nil, resulting_port = nil)
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net", :path => "/hello", :port => given_port).canonical.should == "net.pauldix.foo#{resulting_port}/hello"
end

def should_canonicalize_url_with_path_without_port
should_canonicalize_url_with_path_for_port
end

it "canonicalizes the url with the path" do
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net", :path => "/hello").canonical.should == "net.pauldix.foo/hello"
should_canonicalize_url_with_path_without_port
should_canonicalize_url_with_path_for_port 80
should_canonicalize_url_with_path_for_port 8000, ":8000"
end

def should_canonicalize_url_without_path_for_port(given_port = nil, resulting_port = nil)
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net", :port => given_port).canonical(:include_path => false).should == "net.pauldix.foo#{resulting_port}"
end

def should_canonicalize_url_without_path_without_port
should_canonicalize_url_with_path_for_port
end

it "canonicalizes the url without the path" do
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :public_suffix => "net").canonical(:include_path => false).should == "net.pauldix.foo"
should_canonicalize_url_without_path_without_port
should_canonicalize_url_without_path_for_port 80
should_canonicalize_url_without_path_for_port 8000, ":8000"
end

def should_combine_domain_with_public_suffix_for_port(given_port = nil)
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net", :port => given_port).domain_with_public_suffix.should == "pauldix.net"
Domainatrix::Url.new(:domain => "foo", :public_suffix => "co.uk", :port => given_port).domain_with_public_suffix.should == "foo.co.uk"
Domainatrix::Url.new(:subdomain => "baz", :domain => "bar", :public_suffix => "com", :port => given_port).domain_with_public_suffix.should == "bar.com"
end

def should_combine_domain_with_public_suffix_without_port
should_combine_domain_with_public_suffix_for_port
end

it "combines the domain with the public_suffix" do
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net").domain_with_public_suffix.should == "pauldix.net"
Domainatrix::Url.new(:domain => "foo", :public_suffix => "co.uk" ).domain_with_public_suffix.should == "foo.co.uk"
Domainatrix::Url.new(:subdomain => "baz", :domain => "bar", :public_suffix => "com").domain_with_public_suffix.should == "bar.com"
should_combine_domain_with_public_suffix_without_port
should_combine_domain_with_public_suffix_for_port 80
should_combine_domain_with_public_suffix_for_port 8000
end

it "combines the domain with the public_suffix as an alias" do
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net").domain_with_tld.should == "pauldix.net"
Domainatrix::Url.new(:domain => "foo", :public_suffix => "co.uk" ).domain_with_tld.should == "foo.co.uk"
Domainatrix::Url.new(:subdomain => "baz", :domain => "bar", :public_suffix => "com").domain_with_tld.should == "bar.com"

def should_combine_domain_with_public_suffix_as_an_alias_for_port(given_port = nil)
Domainatrix::Url.new(:domain => "pauldix", :public_suffix => "net", :port => given_port).domain_with_tld.should == "pauldix.net"
Domainatrix::Url.new(:domain => "foo", :public_suffix => "co.uk", :port => given_port).domain_with_tld.should == "foo.co.uk"
Domainatrix::Url.new(:subdomain => "baz", :domain => "bar", :public_suffix => "com", :port => given_port).domain_with_tld.should == "bar.com"
end

def should_combine_domain_with_public_suffix_as_an_alias_without_port
should_combine_domain_with_public_suffix_as_an_alias_for_port
end

it "combines the domain with the public_suffix as an alias" do
should_combine_domain_with_public_suffix_as_an_alias_without_port
should_combine_domain_with_public_suffix_as_an_alias_for_port 80
should_combine_domain_with_public_suffix_as_an_alias_for_port 8000
end
end
4 changes: 3 additions & 1 deletion spec/domainatrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
its(:subdomain) { should == '' }
its(:path) { should == '' }
its(:domain_with_tld) { should == 'localhost' }
its(:port) { should == 3000 }
end

context 'without a scheme' do
Expand All @@ -36,6 +37,7 @@
its(:subdomain) { should == 'www' }
its(:path) { should == '' }
its(:domain_with_tld) { should == 'pauldix.net' }
its(:port) { should == 80 }
end

context 'with a blank url' do
Expand All @@ -48,6 +50,6 @@
its(:subdomain) { should == '' }
its(:path) { should == '' }
its(:domain_with_tld) { should == '' }
its(:port) { should == nil }
end

end