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

Changeset 9242

Show
Ignore:
Timestamp:
04/08/08 05:05:54 (3 months ago)
Author:
rick
Message:

Automatically parse posted JSON content for Mime::JSON requests. [rick]

Files:

Legend:

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

    r9241 r9242  
    11*SVN* 
     2 
     3* Automatically parse posted JSON content for Mime::JSON requests.  [rick] 
     4 
     5  POST /posts 
     6  {"post": {"title": "Breaking News"}} 
     7 
     8  def create 
     9    @post = Post.create params[:post] 
     10    # ... 
     11  end 
    212 
    313* add json_escape ERB util to escape html entities in json strings that are output in HTML pages. [rick] 
  • trunk/actionpack/lib/action_controller/base.rb

    r9123 r9242  
    317317    # 
    318318    #   ActionController::Base.param_parsers[Mime::YAML] = :yaml 
    319     @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form, 
     319    @@param_parsers = { Mime::MULTIPART_FORM   => :multipart_form, 
    320320                        Mime::URL_ENCODED_FORM => :url_encoded_form, 
    321                         Mime::XML => :xml_simple } 
     321                        Mime::XML              => :xml_simple, 
     322                        Mime::JSON             => :json } 
    322323    cattr_accessor :param_parsers 
    323324 
  • trunk/actionpack/lib/action_controller/request.rb

    r9124 r9242  
    403403          when :yaml 
    404404            YAML.load(body) 
     405          when :json 
     406            if body.blank? 
     407              {} 
     408            else 
     409              data = ActiveSupport::JSON.decode(body) 
     410              data = {:_json => data} unless data.is_a?(Hash) 
     411              data.with_indifferent_access 
     412            end 
    405413          else 
    406414            {} 
     
    508516        end 
    509517 
    510  
    511518        MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n 
    512519 
     
    605612          raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/ 
    606613 
    607          begin 
     614          begin 
    608615            body.rewind if body.respond_to?(:rewind) 
    609          rescue Errno::ESPIPE 
     616          rescue Errno::ESPIPE 
    610617            # Handles exceptions raised by input streams that cannot be rewound 
    611618            # such as when using plain CGI under Apache 
    612          end 
     619          end 
    613620 
    614621          params 
  • trunk/actionpack/lib/action_view/template_handlers/erb.rb

    r9241 r9242  
    44  module Util 
    55    HTML_ESCAPE = { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' } 
    6     JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'
     6    JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'
    77 
    88    # A utility method for escaping HTML tag characters. 
  • trunk/actionpack/test/controller/request_test.rb

    r9194 r9242  
    852852end 
    853853 
    854  
    855854class XmlParamsParsingTest < Test::Unit::TestCase 
     855  def test_hash_params 
     856    person = parse_body("<person><name>David</name></person>")[:person] 
     857    assert_kind_of Hash, person 
     858    assert_equal 'David', person['name'] 
     859  end 
     860 
    856861  def test_single_file 
    857862    person = parse_body("<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>") 
     
    900905    end 
    901906end 
     907 
     908class JsonParamsParsingTest < Test::Unit::TestCase 
     909  def test_hash_params 
     910    person = parse_body({:person => {:name => "David"}}.to_json)[:person] 
     911    assert_kind_of Hash, person 
     912    assert_equal 'David', person['name'] 
     913  end 
     914 
     915  private 
     916    def parse_body(body) 
     917      env = { 'CONTENT_TYPE'   => 'application/json', 
     918              'CONTENT_LENGTH' => body.size.to_s } 
     919      cgi = ActionController::Integration::Session::StubCGI.new(env, body) 
     920      ActionController::CgiRequest.new(cgi).request_parameters 
     921    end 
     922end