Class: Rack::FileNotFound

Inherits:
Object show all
Defined in:
lib/kiss/rack/file_not_found.rb

Overview

Rack::FileNotFound rescues file-not-found exceptions (raised when action template files are not found) and returns an HTTP 404 error response.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FileNotFound.



6
7
8
9
10
11
12
13
14
15
# File 'lib/kiss/rack/file_not_found.rb', line 6

def initialize(app, options = {})
  @_app = app
  
  path = options[:path]
  @_body = path ? (
    ::File.file?(path) ?
      ::File.read(path) :
      template('could not find specified FileNotFound error document')
    ) : template
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/kiss/rack/file_not_found.rb', line 17

def call(env)
  code, headers, body = @_app.call(env)
rescue Kiss::FileNotFoundError => e
  [ 404, {
    "Content-Type" => "text/html",
    "Content-Length" => @_body.content_length.to_s
  }, @_body ]
end

#template(error = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kiss/rack/file_not_found.rb', line 26

def template(error = nil)
  <<EOT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="robots" content="NONE,NOARCHIVE" />
  <title>File Not Found</title>
</head>
<body>
<h1>404 File Not Found</h1>

#{error ? "<p>Additionally, #{error}.</p>" : ''}
</body>
</html>
EOT
end