Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit bd50d82f701c55d89b891ebd216ec84008b486c1 1 parent 1162c29
risk danger olson technoweenie authored
9 activeresource/CHANGELOG
View
@@ -1,5 +1,14 @@
*SVN*
+* Add support for setting custom headers per ActiveResource model [Rick]
+
+ class Project
+ custom_headers['X-Token'] = 'foo'
+ end
+
+ # makes the GET request with the custom X-Token header
+ Project.find(:all)
+
* Added find-by-path options to ActiveResource::Base.find [DHH]. Examples:
employees = Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml
14 activeresource/lib/active_resource/base.rb
View
@@ -29,6 +29,10 @@ def connection(refresh = false)
@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 @@ def find_every(options)
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 @@ def find_single(scope, options)
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 @@ def save
# Delete the resource.
def destroy
- connection.delete(element_path)
+ connection.delete(element_path, self.class.custom_headers)
end
# Evaluates to <tt>true</tt> if this resource is found.
@@ -300,14 +304,14 @@ def connection(refresh = false)
# 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
20 activeresource/lib/active_resource/connection.rb
View
@@ -51,26 +51,26 @@ def site=(site)
# 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 @@ def http
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.
18 activeresource/lib/active_resource/custom_methods.rb
View
@@ -36,21 +36,21 @@ class << self
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 @@ def custom_method_collection_url(method_name, options = {})
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
8 activeresource/test/base_test.rb
View
@@ -13,6 +13,7 @@ def setup
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, @matz
mock.get "/people/2.xml", {}, @david
+ mock.get "/people/3.xml", {'key' => 'value'}, nil, 404
mock.put "/people/1.xml", {}, nil, 204
mock.delete "/people/1.xml", {}, nil, 200
mock.delete "/people/2.xml", {}, nil, 400
@@ -198,6 +199,13 @@ def test_find_first
assert_equal "Matz", matz.name
end
+ def test_custom_header
+ Person.custom_headers['key'] = 'value'
+ assert_raises(ActiveResource::ResourceNotFound) { Person.find(3) }
+ ensure
+ Person.custom_headers.delete('key')
+ end
+
def test_find_by_id_not_found
assert_raises(ActiveResource::ResourceNotFound) { Person.find(99) }
assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
25 activeresource/test/connection_test.rb
View
@@ -13,16 +13,21 @@ def setup
@people_empty = [ ].to_xml(:root => 'people-empty-elements')
@matz = @matz.to_xml(:root => 'person')
@david = @david.to_xml(:root => 'person')
+ @header = {'key' => 'value'}
@default_request_headers = { 'Content-Type' => 'application/xml' }
ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/2.xml", @header, @david
mock.get "/people.xml", {}, @people
mock.get "/people_single_elements.xml", {}, @people_single
mock.get "/people_empty_elements.xml", {}, @people_empty
mock.get "/people/1.xml", {}, @matz
mock.put "/people/1.xml", {}, nil, 204
+ mock.put "/people/2.xml", {}, @header, 204
mock.delete "/people/1.xml", {}, nil, 200
+ mock.delete "/people/2.xml", @header, nil, 200
mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'
+ mock.post "/members.xml", {}, @header, 201, 'Location' => '/people/6.xml'
end
end
@@ -79,6 +84,11 @@ def test_get
assert_equal "Matz", matz["name"]
end
+ def test_get_with_header
+ david = @conn.get("/people/2.xml", @header)
+ assert_equal "David", david["name"]
+ end
+
def test_get_collection
people = @conn.get("/people.xml")
assert_equal "Matz", people[0]["name"]
@@ -100,16 +110,31 @@ def test_post
assert_equal "/people/5.xml", response["Location"]
end
+ def test_post_with_header
+ response = @conn.post("/members.xml", @header)
+ assert_equal "/people/6.xml", response["Location"]
+ end
+
def test_put
response = @conn.put("/people/1.xml")
assert_equal 204, response.code
end
+ def test_put_with_header
+ response = @conn.put("/people/2.xml", @header)
+ assert_equal 204, response.code
+ end
+
def test_delete
response = @conn.delete("/people/1.xml")
assert_equal 200, response.code
end
+ def test_delete_with_header
+ response = @conn.delete("/people/2.xml", @header)
+ assert_equal 200, response.code
+ end
+
protected
def assert_response_raises(klass, code)
assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
Please sign in to comment.
Something went wrong with that request. Please try again.