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

Changeset 7518

Show
Ignore:
Timestamp:
09/20/07 23:18:05 (10 months ago)
Author:
david
Message:

Added ActiveResource.format= which defaults to :xml but can also be set to :json [DHH]. Added one-off declarations of mock behavior [DHH]

Files:

Legend:

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

    r7082 r7518  
    11*SVN* 
     2 
     3* Added one-off declarations of mock behavior [DHH]. Example: 
     4 
     5    Before: 
     6      ActiveResource::HttpMock.respond_to do |mock| 
     7        mock.get "/people/1.xml", {}, "<person><name>David</name></person>" 
     8      end 
     9       
     10    Now: 
     11      ActiveResource::HttpMock.respond_to.get "/people/1.xml", {}, "<person><name>David</name></person>" 
     12 
     13* Added ActiveResource.format= which defaults to :xml but can also be set to :json [DHH]. Example: 
     14 
     15    class Person < ActiveResource::Base 
     16      self.site   = "http://app/" 
     17      self.format = :json 
     18    end 
     19     
     20    person = Person.find(1) # => GET http://app/people/1.json 
     21    person.name = "David" 
     22    person.save             # => PUT http://app/people/1.json {name: "David"} 
     23     
     24    Person.format = :xml 
     25    person.name = "Mary" 
     26    person.save             # => PUT http://app/people/1.json <person><name>Mary</name></person>     
    227 
    328* Fix reload error when path prefix is used.  #8727 [Ian Warshak] 
  • trunk/activeresource/lib/active_resource.rb

    r6980 r7518  
    3535end 
    3636 
     37require 'active_resource/formats' 
    3738require 'active_resource/base' 
    3839require 'active_resource/validations' 
  • trunk/activeresource/lib/active_resource/base.rb

    r7143 r7518  
    156156        if defined?(@site) 
    157157          @site 
    158         elsif superclass != Object and superclass.site 
     158        elsif superclass != Object && superclass.site 
    159159          superclass.site.dup.freeze 
    160160        end 
     
    168168      end 
    169169 
     170      # Sets the format that attributes are sent and received in from a mime type reference. Example: 
     171      # 
     172      #   Person.format = :json 
     173      #   Person.find(1) # => GET /people/1.json 
     174      # 
     175      #   Person.format = ActiveResource::Formats::XmlFormat 
     176      #   Person.find(1) # => GET /people/1.xml 
     177      # 
     178      # Default format is :xml. 
     179      def format=(mime_type_reference_or_format) 
     180        format = mime_type_reference_or_format.is_a?(Symbol) ?  
     181          ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format 
     182 
     183        write_inheritable_attribute("format", format) 
     184        connection.format = format 
     185      end 
     186       
     187      # Returns the current format, default is ActiveResource::Formats::XmlFormat 
     188      def format # :nodoc: 
     189        read_inheritable_attribute("format") || ActiveResource::Formats[:xml] 
     190      end 
     191 
    170192      # An instance of ActiveResource::Connection that is the base connection to the remote service. 
    171193      # The +refresh+ parameter toggles whether or not the connection is refreshed at every request 
    172194      # or not (defaults to +false+). 
    173195      def connection(refresh = false) 
    174         if defined?(@connection) or superclass == Object 
    175           @connection = Connection.new(site) if refresh || @connection.nil? 
     196        if defined?(@connection) || superclass == Object 
     197          @connection = Connection.new(site, format) if refresh || @connection.nil? 
    176198          @connection 
    177199        else 
     
    253275      def element_path(id, prefix_options = {}, query_options = nil) 
    254276        prefix_options, query_options = split_options(prefix_options) if query_options.nil? 
    255         "#{prefix(prefix_options)}#{collection_name}/#{id}.xml#{query_string(query_options)}" 
     277        "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}" 
    256278      end 
    257279 
     
    279301      def collection_path(prefix_options = {}, query_options = nil) 
    280302        prefix_options, query_options = split_options(prefix_options) if query_options.nil? 
    281         "#{prefix(prefix_options)}#{collection_name}.xml#{query_string(query_options)}" 
     303        "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}" 
    282304      end 
    283305 
     
    782804      def load_attributes_from_response(response) 
    783805        if response['Content-size'] != "0" && response.body.strip.size > 0 
    784           load(connection.xml_from_response(response)) 
     806          load(self.class.format.decode(response.body)) 
    785807        end 
    786808      end 
  • trunk/activeresource/lib/active_resource/connection.rb

    r7297 r7518  
    4848  class Connection 
    4949    attr_reader :site 
     50    attr_accessor :format 
    5051 
    5152    class << self 
     
    5354        @@requests ||= [] 
    5455      end 
    55        
    56       def default_header 
    57         class << self ; attr_reader :default_header end 
    58         @default_header = { 'Content-Type' => 'application/xml' } 
    59       end 
    6056    end 
    6157 
    6258    # The +site+ parameter is required and will set the +site+ 
    6359    # attribute to the URI for the remote resource service. 
    64     def initialize(site
     60    def initialize(site, format = ActiveResource::Formats[:xml]
    6561      raise ArgumentError, 'Missing site URI' unless site 
    6662      self.site = site 
     63      self.format = format 
    6764    end 
    6865 
     
    7572    # Used to get (find) resources. 
    7673    def get(path, headers = {}) 
    77       xml_from_response(request(:get, path, build_request_headers(headers))
     74      format.decode(request(:get, path, build_request_headers(headers)).body
    7875    end 
    7976 
     
    9693    end 
    9794 
    98     def xml_from_response(response) 
    99       from_xml_data(Hash.from_xml(response.body)) 
    100     end 
    10195 
    10296    private 
     
    145139        @http 
    146140      end 
     141 
     142      def default_header 
     143        @default_header ||= { 'Content-Type' => format.mime_type } 
     144      end 
    147145       
    148146      # Builds headers for request to remote service. 
    149147      def build_request_headers(headers) 
    150         authorization_header.update(self.class.default_header).update(headers) 
     148        authorization_header.update(default_header).update(headers) 
    151149      end 
    152150       
     
    159157        ActiveResource::Base.logger 
    160158      end 
    161  
    162       # Manipulate from_xml Hash, because xml_simple is not exactly what we 
    163       # want for ActiveResource. 
    164       def from_xml_data(data) 
    165         if data.is_a?(Hash) && data.keys.size == 1 
    166           data.values.first 
    167         else 
    168           data 
    169         end 
    170       end 
    171159  end 
    172160end 
  • trunk/activeresource/lib/active_resource/http_mock.rb

    r6584 r7518  
    3333          responses[path] = response 
    3434        end 
    35         yield Responder.new(responses) if block_given? 
     35 
     36        if block_given? 
     37          yield Responder.new(responses)  
     38        else 
     39          Responder.new(responses) 
     40        end 
    3641      end 
    3742