diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-09-29 16:25:49 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-09-29 16:25:49 +0000 |
commit | 7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4 (patch) | |
tree | 289f036d32d0af56aef052b43f60b7cecf5c350a /activeresource/lib | |
parent | d15d15b2c236a556f89536961adf2de7f1fd04dc (diff) | |
download | rails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.tar.gz rails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.tar.bz2 rails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.zip |
Add Basic HTTP Authentication to ActiveResource (closes #6305). [jonathan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5208 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 920e5a15b0..57471957e2 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -25,7 +25,7 @@ module ActiveResource class Connection - attr_accessor :site + attr_reader :site class << self def requests @@ -39,23 +39,27 @@ module ActiveResource end def initialize(site) - @site = site + self.site = site.is_a?(URI) ? site : URI.parse(site) + end + + def site=(site) + @site = site.is_a?(URI) ? site : URI.parse(site) end def get(path) - Hash.from_xml(request(:get, path).body) + Hash.from_xml(request(:get, path, build_request_headers).body) end def delete(path) - request(:delete, path, self.class.default_header) + request(:delete, path, build_request_headers) end def put(path, body = '') - request(:put, path, body, self.class.default_header) + request(:put, path, body, build_request_headers) end def post(path, body = '') - request(:post, path, body, self.class.default_header) + request(:post, path, body, build_request_headers) end private @@ -91,5 +95,13 @@ module ActiveResource @http end + + def build_request_headers + authorization_header.update(self.class.default_header) + end + + def authorization_header + (@site.user || @site.password ? { 'Authorization' => 'Basic ' + ["#{@site.user}:#{ @site.password}"].pack('m').delete("\r\n") } : {}) + end end end |