Sha256: 5a79a335e1f06b0a3339dc9e3cf902783a249fd178fabc84009b4931c970048b

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

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

  def to_hash
    query_params.merge(request_path_params)
  end

  protected

  def initialize(request_env)
    @request_env = request_env
  end

  def query_params
    raw_cgi_params.reduce({}) do |params_collection, (k, v)|

      if single_paramter_value?(v)
        params_collection[k]= v[0]

      else
        k = k.sub(/\[\]$/, '')
        params_collection[k]= 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(@request_env[::Rack::QUERY_STRING].to_s)
  end

  def request_path_params
    path_params = {}
    path_params.merge!(extract_path_params) unless path_params_matcher.empty?
    path_params
  end

  def extract_path_params
    request_path_parts.each.with_index.reduce({}) do |params_col, (path_part, index)|
      params_col[path_params_matcher[index]]= path_part if path_params_matcher[index]
      params_col
    end
  end

  def request_path_parts
    @request_env[::Rack::PATH_INFO].split('/')
  end

  def path_params_matcher
    @request_env[::Rack::App::Constants::PATH_PARAMS_MATCHER] || {}
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rack-app-3.6.0 lib/rack/app/params.rb
rack-app-3.5.2 lib/rack/app/params.rb
rack-app-3.5.1 lib/rack/app/params.rb
rack-app-3.5.0 lib/rack/app/params.rb
rack-app-3.4.0 lib/rack/app/params.rb
rack-app-3.3.0 lib/rack/app/params.rb
rack-app-3.2.1 lib/rack/app/params.rb
rack-app-3.2.0 lib/rack/app/params.rb
rack-app-3.1.0 lib/rack/app/params.rb
rack-app-3.0.0 lib/rack/app/params.rb
rack-app-3.0.0.delta lib/rack/app/params.rb
rack-app-3.0.0.gamma lib/rack/app/params.rb
rack-app-3.0.0.beta lib/rack/app/params.rb