Sha256: aa434d6303baf98867933a751cd94c4561cb3f6f265e177525bd0515afcf7258

Contents?: true

Size: 971 Bytes

Versions: 17

Compression:

Stored size: 971 Bytes

Contents

class Rack::App::Middlewares::SetPathParams

  def initialize(app, build_env)
    @build_env = build_env
    @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)
    @build_env.params.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)
    return if @build_env.endpoint.config.serializer.extnames.empty?
    last_index = env[E::SPLITTED_PATH_INFO].length - 1
    return if @build_env.params[last_index].nil?
    extless(env[E::PATH_SEGMENTS_PARAMS][@build_env.params[last_index]])
  end

  def extless(value)
    extname = File.extname(value)
    return unless @build_env.endpoint.config.serializer.extnames.include?(extname)
    value.slice!(/#{Regexp.escape(extname)}$/) unless extname.empty?
  end

end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/middlewares/set_path_params.rb
rack-app-11.0.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-11.0.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-10.0.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-9.0.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-8.1.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-8.0.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-8.0.0 lib/rack/app/middlewares/set_path_params.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/middlewares/set_path_params.rb
rack-app-7.7.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.5 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.4 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.3 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.2 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.1 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.6.0 lib/rack/app/middlewares/set_path_params.rb
rack-app-7.5.2 lib/rack/app/middlewares/set_path_params.rb