Class: Adsf::Rack::AutoFileExtensions

Inherits:
Object
  • Object
show all
Defined in:
lib/adsf/rack/auto_file_extensions.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, root:, extensions:) ⇒ AutoFileExtensions

Returns a new instance of AutoFileExtensions.



5
6
7
8
9
10
# File 'lib/adsf/rack/auto_file_extensions.rb', line 5

def initialize(app, root:, extensions:)
  @app = app
  @root = root
  # Search list starts with '' so that we first look for file as requested
  @search_suffixes = [''] + Array(extensions).map { |ext| ".#{ext}" }
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/adsf/rack/auto_file_extensions.rb', line 12

def call(env)
  path_info = ::Rack::Utils.unescape(env['PATH_INFO'])
  path = ::File.join(@root, path_info)

  new_env = env
  @search_suffixes.each do |suffix|
    new_path = path + suffix
    next unless ::File.exist?(new_path)

    new_env = env.dup # only dup if needed
    new_env['PATH_INFO'] += suffix
    break
  end

  @app.call(new_env)
end