class Adsf::Rack::IndexFileFinder

Public Class Methods

new(app, root:, index_filenames: ['index.html']) click to toggle source
# File lib/adsf/rack/index_file_finder.rb, line 5
def initialize(app, root:, index_filenames: ['index.html'])
  @app = app
  @root = root
  @index_filenames = index_filenames
end

Public Instance Methods

call(env) click to toggle source
# File lib/adsf/rack/index_file_finder.rb, line 11
def call(env)
  # Get path
  path_info = ::Rack::Utils.unescape(env['PATH_INFO'])
  path = ::File.join(@root, path_info)

  # Redirect if necessary
  if ::File.directory?(path) && path_info !~ %r{/$}
    new_path_info = env['PATH_INFO'] + '/'
    return [
      302,
      { 'Location' => new_path_info, 'Content-Type' => 'text/html' },
      ["Redirecting you to #{new_path_info}…"],
    ]
  end

  # Add index file if necessary
  new_env = env.dup
  if ::File.directory?(path)
    index_filename = index_file_in(path)
    if index_filename
      new_env['PATH_INFO'] = ::File.join(path_info, index_filename)
    end
  end

  # Pass on
  @app.call(new_env)
end

Private Instance Methods

index_file_in(dir) click to toggle source
# File lib/adsf/rack/index_file_finder.rb, line 41
def index_file_in(dir)
  @index_filenames.find do |index_filename|
    ::File.file?(::File.join(dir, index_filename))
  end
end