Sha256: d6fac566e7786263c1fb61a0bf83479aa648bce9d04803c11414d74bb5c537c0

Contents?: true

Size: 853 Bytes

Versions: 24

Compression:

Stored size: 853 Bytes

Contents

class Rack::App::Middlewares::SetPathParams

  def initialize(app, params_lookup_hash)
    @params_lookup_hash = params_lookup_hash
    @app = app
  end

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

  def call(env)
    populate_path_params(env)
    correct_last_value_from_extensions(env)
    @app.call(env)
  end

  protected

  def populate_path_params(env)
    @params_lookup_hash.each do |index, key|
      env[E::PATH_SEGMENTS_PARAMS][key] = env[E::SPLITTED_PATH_INFO][index]
    end
  end

  def correct_last_value_from_extensions(env)
    last_index = env[E::SPLITTED_PATH_INFO].length - 1
    return if @params_lookup_hash[last_index].nil?
    extless(env[E::PATH_SEGMENTS_PARAMS][@params_lookup_hash[last_index]])
  end

  def extless(value)
    extname = File.extname(value)
    value.slice!(/#{Regexp.escape(extname)}$/) unless extname.empty?
  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rack-app-7.5.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.5.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.4.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.3.2 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.3.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.3.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.2.2 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.2.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.2.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.1.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.0.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.8.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.7.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.7.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.6.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.5.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.4.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.4.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.3.3 lib/rack/app/middlewares/set_path_params.rb
rack-app-6.3.2 lib/rack/app/middlewares/set_path_params.rb