Sha256: 7560b03da70c84226f7f058e20d95f534333d5ea6ed18573c37eaed05f835416

Contents?: true

Size: 935 Bytes

Versions: 9

Compression:

Stored size: 935 Bytes

Contents

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

  E = ::Rack::App::Constants::ENV

  def to_hash
    @env[E::MERGED_PARAMS] ||= query_string_params.merge(path_segments_params)
  end

  def query_string_params
    @env[E::QUERY_STRING_PARAMS] ||= generate_query_params
  end

  def path_segments_params
    @env[E::PATH_SEGMENTS_PARAMS]
  end

  def validated_params
    @env[E::VALIDATED_PARAMS]
  end

  protected

  def initialize(env)
    @env = env
  end

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

  def formatted_value(key, value)
    single_paramter_value?(value) && !key.end_with?("[]") ? value[0] : value
  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

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rack-app-6.5.0 lib/rack/app/params.rb
rack-app-6.4.1 lib/rack/app/params.rb
rack-app-6.4.0 lib/rack/app/params.rb
rack-app-6.3.3 lib/rack/app/params.rb
rack-app-6.3.2 lib/rack/app/params.rb
rack-app-6.3.1 lib/rack/app/params.rb
rack-app-6.3.0 lib/rack/app/params.rb
rack-app-6.2.0 lib/rack/app/params.rb
rack-app-6.1.0 lib/rack/app/params.rb