Sha256: 186db77fb5da2dc89c4d881d950b13210ba670083afc89db82c0b3d505726367

Contents?: true

Size: 859 Bytes

Versions: 4

Compression:

Stored size: 859 Bytes

Contents

require 'cgi'
class Rack::App::Params

  def to_hash
    if @env[::Rack::App::Constants::ENV::PARSED_PARAMS]
      @env[::Rack::App::Constants::ENV::PARSED_PARAMS]
    else
      query_params.merge(request_path_params)
    end
  end

  protected

  def initialize(env)
    @env = env
  end

  def query_params
    raw_cgi_params.reduce({}) do |params_collection, (k, v)|
      stripped_key = k.sub(/\[\]$/, '')

      if single_paramter_value?(v) && !k.end_with?("[]")
        params_collection[stripped_key]= v[0]
      else
        params_collection[stripped_key]= v
      end

      params_collection
    end
  end

  def single_paramter_value?(v)
    v.is_a?(Array) and v.length === 1
  end

  def raw_cgi_params
    CGI.parse(@env[::Rack::QUERY_STRING].to_s)
  end

  def request_path_params
    @env[::Rack::App::Constants::ENV::PATH_PARAMS]
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rack-app-6.0.0 lib/rack/app/params.rb
rack-app-5.12.0 lib/rack/app/params.rb
rack-app-5.11.1 lib/rack/app/params.rb
rack-app-5.11.0 lib/rack/app/params.rb