Changeset 238

Show
Ignore:
Timestamp:
05/18/07 19:45:40 (2 years ago)
Author:
e.@brainspl.at
Message:

merb now accepts xml posts and parses them into params

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Rakefile

    r234 r238  
    1616 
    1717NAME = "merb" 
    18 VERS = "0.3.1
     18VERS = "0.3.2
    1919CLEAN.include ['**/.*.sw?', '*.gem', '.config'] 
    2020 
     
    6868  s.add_dependency('json') 
    6969  s.add_dependency('mime-types') 
     70  s.add_dependency('xml-simple') 
    7071  s.add_dependency('archive-tar-minitar')  
    7172  s.required_ruby_version = '>= 1.8.4' 
  • trunk/lib/merb.rb

    r226 r238  
    1616 
    1717module Merb 
    18   VERSION='0.3.1' unless defined?VERSION 
     18  VERSION='0.3.2' unless defined?VERSION 
    1919  class Server 
    2020    class << self 
  • trunk/lib/merb/core_ext/merb_hash.rb

    r194 r238  
    11class Hash 
     2 
     3  class << self 
     4    def from_xml(xml) 
     5      # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple 
     6      undasherize_keys(typecast_xml_value(XmlSimple.xml_in(xml, 
     7        'forcearray'   => false, 
     8        'forcecontent' => true, 
     9        'keeproot'     => true, 
     10        'contentkey'   => '__content__') 
     11      ))             
     12    end 
     13 
     14    private 
     15      def typecast_xml_value(value) 
     16        case value.class.to_s 
     17          when "Hash" 
     18            if value.has_key?("__content__") 
     19              content = translate_xml_entities(value["__content__"]) 
     20              case value["type"] 
     21                when "integer"  then content.to_i 
     22                when "boolean"  then content.strip == "true" 
     23                when "datetime" then ::Time.parse(content).utc 
     24                when "date"     then ::Date.parse(content) 
     25                else                 content 
     26              end 
     27            else 
     28              (value.blank? || value['type'] || value['nil'] == 'true') ? nil : value.inject({}) do |h,(k,v)| 
     29                h[k] = typecast_xml_value(v) 
     30                h 
     31              end 
     32            end 
     33          when "Array" 
     34            value.map! { |i| typecast_xml_value(i) } 
     35            case value.length 
     36              when 0 then nil 
     37              when 1 then value.first 
     38              else value 
     39            end 
     40          when "String" 
     41            value 
     42          else 
     43            raise "can't typecast #{value.inspect}" 
     44        end 
     45      end 
     46     
     47      def translate_xml_entities(value) 
     48        value.gsub(/&lt;/,   "<"). 
     49              gsub(/&gt;/,   ">"). 
     50              gsub(/&quot;/, '"'). 
     51              gsub(/&apos;/, "'"). 
     52              gsub(/&amp;/,  "&") 
     53      end 
     54     
     55      def undasherize_keys(params) 
     56          case params.class.to_s 
     57            when "Hash" 
     58              params.inject({}) do |h,(k,v)| 
     59                h[k.to_s.tr("-", "_")] = undasherize_keys(v) 
     60                h 
     61              end 
     62            when "Array" 
     63              params.map { |v| undasherize_keys(v) } 
     64            else 
     65              params 
     66            end 
     67     end 
     68  end 
     69   
     70     
    271  def with_indifferent_access 
    372    MerbHash.new(self) 
  • trunk/lib/merb/merb_controller.rb

    r234 r238  
    5252          json = MerbHash.new(json) if json.is_a? Hash 
    5353          querystring.update(json) 
     54        elsif ['application/xml'].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE]) 
     55          querystring.update(Hash.from_xml(request.read)) 
    5456        else 
    5557          querystring.update(query_parse(request.read)) 
  • trunk/lib/merb/merb_upload_handler.rb

    r237 r238  
    4343 
    4444  def request_begins(params) 
     45    puts "request begins: clen: #{params[Mongrel::Const::CONTENT_LENGTH]}" 
    4546    upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i) 
    4647  end