diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-18 17:19:15 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-18 17:19:15 +0000 |
commit | a7532f9d5f47cf17c18a17dccff2a6d317d8254e (patch) | |
tree | 96c58c95a5977a45a67130227ff2ef82046a12bf /actionpack | |
parent | b09829354f4699941824f53903f4a8743fa98bd5 (diff) | |
download | rails-a7532f9d5f47cf17c18a17dccff2a6d317d8254e.tar.gz rails-a7532f9d5f47cf17c18a17dccff2a6d317d8254e.tar.bz2 rails-a7532f9d5f47cf17c18a17dccff2a6d317d8254e.zip |
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
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rwxr-xr-x | actionpack/lib/action_controller/request.rb | 14 | ||||
-rw-r--r-- | actionpack/test/controller/request_test.rb | 26 |
3 files changed, 42 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index ab4edf3ba6..4ce21927eb 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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] diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 9eb0b04ef0..e9ab3cad90 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -50,6 +50,20 @@ module ActionController 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 diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb new file mode 100644 index 0000000000..1f38d07a03 --- /dev/null +++ b/actionpack/test/controller/request_test.rb @@ -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
\ No newline at end of file |