From bd50d82f701c55d89b891ebd216ec84008b486c1 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sun, 29 Apr 2007 03:14:36 +0000 Subject: Add support for setting custom headers per ActiveResource model [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6624 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/lib/active_resource/base.rb | 14 +++++++++----- activeresource/lib/active_resource/connection.rb | 20 ++++++++++---------- activeresource/lib/active_resource/custom_methods.rb | 18 +++++++++--------- 3 files changed, 28 insertions(+), 24 deletions(-) (limited to 'activeresource/lib/active_resource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index bcc1e8b497..5a2c54c5ec 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -29,6 +29,10 @@ module ActiveResource @connection end + def custom_headers + @custom_headers ||= {} + end + # Do not include any modules in the default element name. This makes it easier to seclude ARes objects # in a separate namespace without having to set element_name repeatedly. attr_accessor_with_default(:element_name) { to_s.split("::").last.underscore } #:nodoc: @@ -150,7 +154,7 @@ module ActiveResource prefix_options, query_options = split_options(options) from ||= collection_path(prefix_options, query_options) - instantiate_collection(connection.get(from) || []) + instantiate_collection(connection.get(from, custom_headers) || []) end def instantiate_collection(collection, prefix_options = {}) @@ -167,7 +171,7 @@ module ActiveResource prefix_options, query_options = split_options(options) from = scope.to_s.include?("/") ? scope : element_path(scope, prefix_options, query_options) - returning new(connection.get(from)) do |resource| + returning new(connection.get(from, custom_headers)) do |resource| resource.prefix_options = prefix_options end end @@ -254,7 +258,7 @@ module ActiveResource # Delete the resource. def destroy - connection.delete(element_path) + connection.delete(element_path, self.class.custom_headers) end # Evaluates to true if this resource is found. @@ -300,14 +304,14 @@ module ActiveResource # Update the resource on the remote service. def update - returning connection.put(element_path(prefix_options), to_xml) do |response| + returning connection.put(element_path(prefix_options), to_xml, self.class.custom_headers) do |response| load_attributes_from_response(response) end end # Create (i.e., save to the remote service) the new resource. def create - returning connection.post(collection_path, to_xml) do |response| + returning connection.post(collection_path, to_xml, self.class.custom_headers) do |response| self.id = id_from_response(response) load_attributes_from_response(response) end diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index c37bb73834..bb3993512e 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -51,26 +51,26 @@ module ActiveResource # Execute a GET request. # Used to get (find) resources. - def get(path) - xml_from_response(request(:get, path, build_request_headers)) + def get(path, headers = {}) + xml_from_response(request(:get, path, build_request_headers(headers))) end # Execute a DELETE request (see HTTP protocol documentation if unfamiliar). # Used to delete resources. - def delete(path) - request(:delete, path, build_request_headers) + def delete(path, headers = {}) + request(:delete, path, build_request_headers(headers)) end # Execute a PUT request (see HTTP protocol documentation if unfamiliar). # Used to update resources. - def put(path, body = '') - request(:put, path, body, build_request_headers) + def put(path, body = '', headers = {}) + request(:put, path, body, build_request_headers(headers)) end # Execute a POST request. # Used to create new resources. - def post(path, body = '') - request(:post, path, body, build_request_headers) + def post(path, body = '', headers = {}) + request(:post, path, body, build_request_headers(headers)) end def xml_from_response(response) @@ -125,8 +125,8 @@ module ActiveResource end # Builds headers for request to remote service. - def build_request_headers - authorization_header.update(self.class.default_header) + def build_request_headers(headers) + authorization_header.update(self.class.default_header).update(headers) end # Sets authorization header; authentication information is pulled from credentials provided with site URI. diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index de99305b93..f4e21714a3 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -36,21 +36,21 @@ module ActiveResource alias :orig_delete :delete def get(method_name, options = {}) - connection.get(custom_method_collection_url(method_name, options)) + connection.get(custom_method_collection_url(method_name, options), custom_headers) end def post(method_name, options = {}, body = nil) - connection.post(custom_method_collection_url(method_name, options), body) + connection.post(custom_method_collection_url(method_name, options), body, custom_headers) end def put(method_name, options = {}, body = nil) - connection.put(custom_method_collection_url(method_name, options), body) + connection.put(custom_method_collection_url(method_name, options), body, custom_headers) end # Need to jump through some hoops to retain the original class 'delete' method def delete(custom_method_name, options = {}) if (custom_method_name.is_a?(Symbol)) - connection.delete(custom_method_collection_url(custom_method_name, options)) + connection.delete(custom_method_collection_url(custom_method_name, options), custom_headers) else orig_delete(custom_method_name, options) end @@ -71,23 +71,23 @@ module ActiveResource module InstanceMethods def get(method_name, options = {}) - connection.get(custom_method_element_url(method_name, options)) + connection.get(custom_method_element_url(method_name, options), self.class.custom_headers) end def post(method_name, options = {}, body = nil) if new? - connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body)) + connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body), self.class.custom_headers) else - connection.post(custom_method_element_url(method_name, options), body) + connection.post(custom_method_element_url(method_name, options), body, self.class.custom_headers) end end def put(method_name, options = {}, body = nil) - connection.put(custom_method_element_url(method_name, options), body) + connection.put(custom_method_element_url(method_name, options), body, self.class.custom_headers) end def delete(method_name, options = {}) - connection.delete(custom_method_element_url(method_name, options)) + connection.delete(custom_method_element_url(method_name, options), self.class.custom_headers) end -- cgit v1.2.3