Class: Utopia::Middleware::DirectoryIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/directory_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ DirectoryIndex

Returns a new instance of DirectoryIndex.



12
13
14
15
16
17
18
19
# File 'lib/utopia/middleware/directory_index.rb', line 12

def initialize(app, options = {})
	@app = app
	@root = options[:root] || Utopia::Middleware::default_root

	@files = ["index.html"]

	@default = "index"
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/utopia/middleware/directory_index.rb', line 21

def call(env)
	path = Path.create(env["PATH_INFO"])
	
	if path.directory?
		# Check to see if one of the files exists in the requested directory
		@files.each do |file|
			if File.exist?(File.join(@root, path.components, file))
				return [307, {"Location" => (path + file).to_s}, []]
			end
		end
	
		# Use the default path
		return [307, {"Location" => (path + @default).to_s}, []]
	else
		return @app.call(env)
	end
end