Permalink
Browse files

Added Request#domain (returns string) and Request#subdomains (returns…

… array).

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@213 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent b098293 commit a7532f9d5f47cf17c18a17dccff2a6d317d8254e @dhh dhh committed Dec 18, 2004
Showing with 42 additions and 0 deletions.
  1. +2 −0 actionpack/CHANGELOG
  2. +14 −0 actionpack/lib/action_controller/request.rb
  3. +26 −0 actionpack/test/controller/request_test.rb
@@ -1,5 +1,7 @@
*SVN*
+* Added Request#domain (returns string) and Request#subdomains (returns array).
+
* Added POST support for the breakpoint retries, so form processing that raises an exception can be retried with the original request [Florian Gross]
@@ -50,6 +50,20 @@ def remote_ip
return env['REMOTE_ADDR']
end
+ # Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify
+ # a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".
+ def domain(tld_length = 1)
+ host.split(".").last(1 + tld_length).join(".")
+ end
+
+ # Returns all the subdomains as an array, so ["dev", "www"] would be returned for "dev.www.rubyonrails.org".
+ # You can specify a different <tt>tld_length</tt>, such as 2 to catch ["www"] instead of ["www", "rubyonrails"]
+ # in "www.rubyonrails.co.uk".
+ def subdomains(tld_length = 1)
+ parts = host.split(".")
+ parts - parts.last(1 + tld_length)
+ end
+
def request_uri
env["REQUEST_URI"]
end
@@ -0,0 +1,26 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+class RequestTest < Test::Unit::TestCase
+ def setup
+ @request = ActionController::TestRequest.new
+ end
+
+ def test_domains
+ @request.host = "www.rubyonrails.org"
+ assert_equal "rubyonrails.org", @request.domain
+
+ @request.host = "www.rubyonrails.co.uk"
+ assert_equal "rubyonrails.co.uk", @request.domain(2)
+ end
+
+ def test_subdomains
+ @request.host = "www.rubyonrails.org"
+ assert_equal %w( www ), @request.subdomains
+
+ @request.host = "www.rubyonrails.co.uk"
+ assert_equal %w( www ), @request.subdomains(2)
+
+ @request.host = "dev.www.rubyonrails.co.uk"
+ assert_equal %w( dev www ), @request.subdomains(2)
+ end
+end

0 comments on commit a7532f9

Please sign in to comment.