Skip to content

Commit

Permalink
Avoid exceptions when the host has fewer parts than the tld_size. Fix…
Browse files Browse the repository at this point in the history
…es issue #11. Also minor refactorings.
  • Loading branch information
danielmorrison committed May 13, 2010
1 parent 18511eb commit 9eb8b18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/subdomain-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def self.non_mirror_subdomain_from(host)

def self.host_without_subdomain(host)
parts = host.split('.')
parts[-(SubdomainFu.tld_size+1)..-1].join(".")
Array(parts[-(SubdomainFu.tld_size+1)..-1]).join(".")
end

# Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
Expand Down Expand Up @@ -99,11 +99,12 @@ def self.override_only_path?
end

def self.needs_rewrite?(subdomain, host)
return false if host.split('.').size <= tld_size

case subdomain
when nil
#rewrite when there is a preferred mirror set and there is no subdomain on the host
return true if self.preferred_mirror && subdomain_from(host).nil?
return false
return self.preferred_mirror && subdomain_from(host).nil?
when false
h = subdomain_from(host)
#if the host has a subdomain
Expand All @@ -116,14 +117,8 @@ def self.needs_rewrite?(subdomain, host)
#it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
return false if is_mirror?(h)
end
return self.crazy_rewrite_rule(subdomain, host)
else
return self.crazy_rewrite_rule(subdomain, host)
end
end

#This is a black box of crazy! So I split some of the simpler logic out into the case statement above to make my brain happy!
def self.crazy_rewrite_rule(subdomain, host)
(!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
!same_subdomain?(subdomain, host)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/subdomain_fu_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
end

it "#host_without_subdomain should chop of the subdomain and return the rest" do
SubdomainFu.host_without_subdomain("localhost:3000").should == "localhost:3000"
SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
end
Expand Down Expand Up @@ -96,6 +97,11 @@
SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
end

it "should not change the subdomain for a host the same or smaller than the tld size" do
SubdomainFu.tld_size = 1
SubdomainFu.rewrite_host_for_subdomains("cool","localhost").should == "localhost"
end

it "should remove the subdomain if passed false when it's not a mirror" do
SubdomainFu.rewrite_host_for_subdomains(false,"cool.localhost").should == "localhost"
end
Expand Down

0 comments on commit 9eb8b18

Please sign in to comment.