diff options
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 |