diff options
author | Ahmed El-Daly <aeldaly@developergurus.com> | 2009-01-21 22:18:10 -0500 |
---|---|---|
committer | Ahmed El-Daly <aeldaly@developergurus.com> | 2009-01-21 22:18:10 -0500 |
commit | f08a78a057782577d5efbc82662d7898e26dcb8c (patch) | |
tree | d0638cf1d180ed3713ecafcf36ea1df9bc5eeb7a /actionpack/lib/action_controller/url_encoded_pair_parser.rb | |
parent | c5069bd4951419ea02aea35ac5c121bc0c311940 (diff) | |
parent | e8f7da6118936af2d145b3c025db4b4dcd0b3308 (diff) | |
download | rails-f08a78a057782577d5efbc82662d7898e26dcb8c.tar.gz rails-f08a78a057782577d5efbc82662d7898e26dcb8c.tar.bz2 rails-f08a78a057782577d5efbc82662d7898e26dcb8c.zip |
Merge branch 'master' of git@github.com:lifo/docrails
Diffstat (limited to 'actionpack/lib/action_controller/url_encoded_pair_parser.rb')
-rw-r--r-- | actionpack/lib/action_controller/url_encoded_pair_parser.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/url_encoded_pair_parser.rb b/actionpack/lib/action_controller/url_encoded_pair_parser.rb index 9883ad0d85..b63dca987d 100644 --- a/actionpack/lib/action_controller/url_encoded_pair_parser.rb +++ b/actionpack/lib/action_controller/url_encoded_pair_parser.rb @@ -1,5 +1,66 @@ module ActionController class UrlEncodedPairParser < StringScanner #:nodoc: + class << self + def parse_query_parameters(query_string) + return {} if query_string.blank? + + pairs = query_string.split('&').collect do |chunk| + next if chunk.empty? + key, value = chunk.split('=', 2) + next if key.empty? + value = value.nil? ? nil : CGI.unescape(value) + [ CGI.unescape(key), value ] + end.compact + + new(pairs).result + end + + def parse_hash_parameters(params) + parser = new + + params = params.dup + until params.empty? + for key, value in params + if key.blank? + params.delete(key) + elsif value.is_a?(Array) + parser.parse(key, get_typed_value(value.shift)) + params.delete(key) if value.empty? + else + parser.parse(key, get_typed_value(value)) + params.delete(key) + end + end + end + + parser.result + end + + private + def get_typed_value(value) + case value + when String + value + when NilClass + '' + when Array + value.map { |v| get_typed_value(v) } + when Hash + if value.has_key?(:tempfile) + upload = value[:tempfile] + upload.extend(UploadedFile) + upload.original_path = value[:filename] + upload.content_type = value[:type] + upload + else + nil + end + else + raise "Unknown form value: #{value.inspect}" + end + end + end + attr_reader :top, :parent, :result def initialize(pairs = []) |