Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6624

Show
Ignore:
Timestamp:
04/29/07 03:14:36 (1 year ago)
Author:
rick
Message:

Add support for setting custom headers per ActiveResource model [Rick]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activeresource/CHANGELOG

    r6595 r6624  
    11*SVN* 
     2 
     3* Add support for setting custom headers per ActiveResource model [Rick] 
     4 
     5  class Project 
     6    custom_headers['X-Token'] = 'foo' 
     7  end 
     8   
     9  # makes the GET request with the custom X-Token header 
     10  Project.find(:all) 
    211 
    312* Added find-by-path options to ActiveResource::Base.find [DHH]. Examples: 
  • trunk/activeresource/lib/active_resource/base.rb

    r6595 r6624  
    2828        @connection = Connection.new(site) if refresh || @connection.nil? 
    2929        @connection 
     30      end 
     31 
     32      def custom_headers 
     33        @custom_headers ||= {} 
    3034      end 
    3135 
     
    151155          from ||= collection_path(prefix_options, query_options) 
    152156 
    153           instantiate_collection(connection.get(from) || []) 
     157          instantiate_collection(connection.get(from, custom_headers) || []) 
    154158        end 
    155159         
     
    168172          from = scope.to_s.include?("/") ? scope : element_path(scope, prefix_options, query_options) 
    169173 
    170           returning new(connection.get(from)) do |resource| 
     174          returning new(connection.get(from, custom_headers)) do |resource| 
    171175            resource.prefix_options = prefix_options 
    172176          end 
     
    255259    # Delete the resource. 
    256260    def destroy 
    257       connection.delete(element_path
     261      connection.delete(element_path, self.class.custom_headers
    258262    end 
    259263 
     
    301305      # Update the resource on the remote service. 
    302306      def update 
    303         returning connection.put(element_path(prefix_options), to_xml) do |response| 
     307        returning connection.put(element_path(prefix_options), to_xml, self.class.custom_headers) do |response| 
    304308          load_attributes_from_response(response) 
    305309        end 
     
    308312      # Create (i.e., save to the remote service) the new resource. 
    309313      def create 
    310         returning connection.post(collection_path, to_xml) do |response| 
     314        returning connection.post(collection_path, to_xml, self.class.custom_headers) do |response| 
    311315          self.id = id_from_response(response) 
    312316          load_attributes_from_response(response) 
  • trunk/activeresource/lib/active_resource/connection.rb

    r6539 r6624  
    5252    # Execute a GET request. 
    5353    # Used to get (find) resources. 
    54     def get(path
    55       xml_from_response(request(:get, path, build_request_headers)) 
     54    def get(path, headers = {}
     55      xml_from_response(request(:get, path, build_request_headers(headers))) 
    5656    end 
    5757 
    5858    # Execute a DELETE request (see HTTP protocol documentation if unfamiliar). 
    5959    # Used to delete resources. 
    60     def delete(path
    61       request(:delete, path, build_request_headers
     60    def delete(path, headers = {}
     61      request(:delete, path, build_request_headers(headers)
    6262    end 
    6363 
    6464    # Execute a PUT request (see HTTP protocol documentation if unfamiliar). 
    6565    # Used to update resources. 
    66     def put(path, body = ''
    67       request(:put, path, body, build_request_headers
     66    def put(path, body = '', headers = {}
     67      request(:put, path, body, build_request_headers(headers)
    6868    end 
    6969 
    7070    # Execute a POST request. 
    7171    # Used to create new resources. 
    72     def post(path, body = ''
    73       request(:post, path, body, build_request_headers
     72    def post(path, body = '', headers = {}
     73      request(:post, path, body, build_request_headers(headers)
    7474    end 
    7575 
     
    126126       
    127127      # Builds headers for request to remote service. 
    128       def build_request_headers 
    129         authorization_header.update(self.class.default_header) 
     128      def build_request_headers(headers) 
     129        authorization_header.update(self.class.default_header).update(headers) 
    130130      end 
    131131       
  • trunk/activeresource/lib/active_resource/custom_methods.rb

    r6584 r6624  
    3737           
    3838          def get(method_name, options = {}) 
    39             connection.get(custom_method_collection_url(method_name, options)
     39            connection.get(custom_method_collection_url(method_name, options), custom_headers
    4040          end 
    4141       
    4242          def post(method_name, options = {}, body = nil) 
    43             connection.post(custom_method_collection_url(method_name, options), body
     43            connection.post(custom_method_collection_url(method_name, options), body, custom_headers
    4444          end 
    4545       
    4646          def put(method_name, options = {}, body = nil) 
    47             connection.put(custom_method_collection_url(method_name, options), body
     47            connection.put(custom_method_collection_url(method_name, options), body, custom_headers
    4848          end 
    4949       
     
    5151          def delete(custom_method_name, options = {}) 
    5252            if (custom_method_name.is_a?(Symbol)) 
    53               connection.delete(custom_method_collection_url(custom_method_name, options)
     53              connection.delete(custom_method_collection_url(custom_method_name, options), custom_headers
    5454            else 
    5555              orig_delete(custom_method_name, options) 
     
    7272    module InstanceMethods 
    7373      def get(method_name, options = {}) 
    74         connection.get(custom_method_element_url(method_name, options)
     74        connection.get(custom_method_element_url(method_name, options), self.class.custom_headers
    7575      end 
    7676       
    7777      def post(method_name, options = {}, body = nil) 
    7878        if new? 
    79           connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body)
     79          connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body), self.class.custom_headers
    8080        else 
    81           connection.post(custom_method_element_url(method_name, options), body
     81          connection.post(custom_method_element_url(method_name, options), body, self.class.custom_headers
    8282        end 
    8383      end 
    8484       
    8585      def put(method_name, options = {}, body = nil) 
    86         connection.put(custom_method_element_url(method_name, options), body
     86        connection.put(custom_method_element_url(method_name, options), body, self.class.custom_headers
    8787      end 
    8888       
    8989      def delete(method_name, options = {}) 
    90         connection.delete(custom_method_element_url(method_name, options)
     90        connection.delete(custom_method_element_url(method_name, options), self.class.custom_headers
    9191      end 
    9292 
  • trunk/activeresource/test/base_test.rb

    r6595 r6624  
    1414      mock.get    "/people/1.xml",             {}, @matz 
    1515      mock.get    "/people/2.xml",             {}, @david 
     16      mock.get    "/people/3.xml",             {'key' => 'value'}, nil, 404 
    1617      mock.put    "/people/1.xml",             {}, nil, 204 
    1718      mock.delete "/people/1.xml",             {}, nil, 200 
     
    199200  end 
    200201 
     202  def test_custom_header 
     203    Person.custom_headers['key'] = 'value' 
     204    assert_raises(ActiveResource::ResourceNotFound) { Person.find(3) } 
     205  ensure 
     206    Person.custom_headers.delete('key') 
     207  end 
     208 
    201209  def test_find_by_id_not_found 
    202210    assert_raises(ActiveResource::ResourceNotFound) { Person.find(99) } 
  • trunk/activeresource/test/connection_test.rb

    r6539 r6624  
    1414    @matz = @matz.to_xml(:root => 'person') 
    1515    @david = @david.to_xml(:root => 'person') 
     16    @header = {'key' => 'value'} 
    1617 
    1718    @default_request_headers = { 'Content-Type' => 'application/xml' } 
    1819    ActiveResource::HttpMock.respond_to do |mock| 
     20      mock.get    "/people/2.xml", @header, @david 
    1921      mock.get    "/people.xml", {}, @people 
    2022      mock.get    "/people_single_elements.xml", {}, @people_single 
     
    2224      mock.get    "/people/1.xml", {}, @matz 
    2325      mock.put    "/people/1.xml", {}, nil, 204 
     26      mock.put    "/people/2.xml", {}, @header, 204 
    2427      mock.delete "/people/1.xml", {}, nil, 200 
     28      mock.delete "/people/2.xml", @header, nil, 200 
    2529      mock.post   "/people.xml",   {}, nil, 201, 'Location' => '/people/5.xml' 
     30      mock.post   "/members.xml",  {}, @header, 201, 'Location' => '/people/6.xml' 
    2631    end 
    2732  end 
     
    8085  end 
    8186 
     87  def test_get_with_header 
     88    david = @conn.get("/people/2.xml", @header) 
     89    assert_equal "David", david["name"] 
     90  end 
     91 
    8292  def test_get_collection 
    8393    people = @conn.get("/people.xml") 
     
    101111  end 
    102112 
     113  def test_post_with_header 
     114    response = @conn.post("/members.xml", @header) 
     115    assert_equal "/people/6.xml", response["Location"] 
     116  end 
     117 
    103118  def test_put 
    104119    response = @conn.put("/people/1.xml") 
     
    106121  end 
    107122 
     123  def test_put_with_header 
     124    response = @conn.put("/people/2.xml", @header) 
     125    assert_equal 204, response.code 
     126  end 
     127 
    108128  def test_delete 
    109129    response = @conn.delete("/people/1.xml") 
     130    assert_equal 200, response.code 
     131  end 
     132 
     133  def test_delete_with_header 
     134    response = @conn.delete("/people/2.xml", @header) 
    110135    assert_equal 200, response.code 
    111136  end