Sha256: 847d0425d7ec49daa95132b0aea476bb754102a4c4f26b1e574fe738c504d706

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

require 'multi_json'
require 'rack/utils'

module Goliath
  module Rack
    URL_ENCODED = %r{^application/x-www-form-urlencoded}
    JSON_ENCODED = %r{^application/json}

    # A middle ware to parse params. This will parse both the
    # query string parameters and the body and place them into
    # the _params_ hash of the Goliath::Env for the request.
    #
    # @example
    #  use Goliath::Rack::Params
    #
    class Params
      module Parser
        def retrieve_params(env)
          params = env['params'] || {}
          params.merge!(::Rack::Utils.parse_nested_query(env['QUERY_STRING']))

          if env['rack.input']
            post_params = ::Rack::Utils::Multipart.parse_multipart(env)
            unless post_params
              body = env['rack.input'].read
              env['rack.input'].rewind

              unless body.empty?
                begin
                  post_params = case(env['CONTENT_TYPE'])
                  when URL_ENCODED then
                    ::Rack::Utils.parse_nested_query(body)
                  when JSON_ENCODED then
                    MultiJson.load(body)
                  else
                    {}
                  end
                rescue StandardError => e
                  raise Goliath::Validation::BadRequestError, "Invalid parameters: #{e.class.to_s}"
                end
              else
                post_params = {}
              end
            end
            params.merge!(post_params)
          end
          params
        end
      end

      include Goliath::Rack::Validator
      include Parser

      def initialize(app)
        @app = app
      end

      def call(env)
        Goliath::Rack::Validator.safely(env) do
          env['params'] = retrieve_params(env)
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
goliath-1.0.1 lib/goliath/rack/params.rb
goliath-1.0.0 lib/goliath/rack/params.rb
goliath-1.0.0.beta.1 lib/goliath/rack/params.rb