Permalink
Browse files

added the basic url canonicalization and representation code

  • Loading branch information...
pauldix committed Dec 10, 2009
1 parent 7f7f16b commit f80c552e62f5f89c1d300f6fbc8d97e1ee5f1088
Showing with 64 additions and 0 deletions.
  1. +1 −0 lib/domainatrix.rb
  2. +27 −0 lib/domainatrix/url.rb
  3. +36 −0 spec/domainatrix/url_spec.rb
View
@@ -1,6 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirname(__FILE__))
require 'domainatrix/domain_parser.rb'
+require 'domainatrix/url.rb'
module Domainatrix
VERSION = "0.0.1"
View
@@ -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
@@ -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

0 comments on commit f80c552

Please sign in to comment.