forked from pauldix/domainatrix
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the basic url canonicalization and representation code
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Domainatrix | ||
class Url | ||
attr_reader :tld, :domain, :subdomain, :path | ||
|
||
def initialize(attrs = {}) | ||
@tld = attrs[:tld] | ||
@domain = attrs[:domain] | ||
@subdomain = attrs[:subdomain] | ||
@path = attrs[:path] | ||
end | ||
|
||
def canonical(options = {}) | ||
url = "#{@tld}.#{@domain}" | ||
if @subdomain | ||
if options[:include_www] | ||
url << ".#{@subdomain}" | ||
else | ||
sub_without_www = @subdomain.gsub(/^www/, "") | ||
url << (sub_without_www.start_with?(".") ? sub_without_www : ".#{sub_without_www}") unless sub_without_www.empty? | ||
end | ||
end | ||
url << @path if @path | ||
|
||
url | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe "url" do | ||
it "has the tld" do | ||
Domainatrix::Url.new(:tld => "net").tld.should == "net" | ||
end | ||
|
||
it "has the domain" do | ||
Domainatrix::Url.new(:domain => "pauldix").domain.should == "pauldix" | ||
end | ||
|
||
it "has the subdomain" do | ||
Domainatrix::Url.new(:subdomain => "foo").subdomain.should == "foo" | ||
end | ||
|
||
it "has the path" do | ||
Domainatrix::Url.new(:path => "/asdf.html").path.should == "/asdf.html" | ||
end | ||
|
||
it "canonicalizes the url without www" do | ||
Domainatrix::Url.new(:subdomain => "www", :domain => "pauldix", :tld => "net").canonical.should == "net.pauldix" | ||
Domainatrix::Url.new(:subdomain => "www.foo", :domain => "pauldix", :tld => "net").canonical.should == "net.pauldix.foo" | ||
end | ||
|
||
it "canonicalizes the url with www" do | ||
Domainatrix::Url.new(:subdomain => "www", :domain => "pauldix", :tld => "net").canonical(:include_www => true).should == "net.pauldix.www" | ||
end | ||
|
||
it "canonicalizes the url with the path" do | ||
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :tld => "net", :path => "/hello").canonical.should == "net.pauldix.foo/hello" | ||
end | ||
|
||
it "canonicalizes the url without the path" do | ||
Domainatrix::Url.new(:subdomain => "foo", :domain => "pauldix", :tld => "net").canonical(:include_path => false).should == "net.pauldix.foo" | ||
end | ||
end |