Sha256: 00e23bd41c529bf6302cdf6ea1654f4219c5b70980a9027cc841aa2cc68834a3

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

require 'action_dispatch/http/request'
require 'active_support/core_ext/hash/indifferent_access'

module ActionDispatch
  class ParamsParser

    alias :call_old :call
    def call(env)
      request = Request.new(env)
      mime_type = content_type_from_legacy_post_data_format_header(env) ||
          request.content_mime_type

      case mime_type
      when Mime::JSON
        data = ActiveSupport::JSON.decode(request.body)
        request.body.rewind if request.body.respond_to?(:rewind)
        data = {:_json => data} unless data.is_a?(Hash)
        env["action_dispatch.request.request_parameters"] = {:json => data}.with_indifferent_access
        
        @app.call(env)
      else
        call_old(env)
      end
    end
  end
end

module ActionController
  class Base
    def from_java(map)
      result = map.dup
      map.each do |k,v|
        result.delete(k)
        result[k.underscore] = v.is_a?(Hash) ? from_java(v) : v
      end
      result
    end
  end
end

::ActionController::Base.send(:before_filter) do
  json = params.delete(:json)
  if json
    params[params[:controller].singularize] = from_java(json)
  end
  true
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
resty-generators-0.4.0 lib/generators/resty/setup/templates/monkey_patch.rb
resty-generators-0.3.3 lib/generators/resty/setup/templates/monkey_patch.rb
resty-generators-0.3.2 lib/generators/resty/setup/templates/monkey_patch.rb
resty-generators-0.3.1 lib/generators/resty/setup/templates/monkey_patch.rb
resty-generators-0.3.0 lib/generators/resty/setup/templates/monkey_patch.rb
resty-generators-0.2.0 lib/generators/resty/setup/templates/monkey_patch.rb