Class: Rack::NotFound

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/not_found.rb

Overview

Rack::NotFound is a default endpoint. Optionally initialize with the path to a custom 404 page, to override the standard response body.

Examples:

Serve default 404 response:

run Rack::NotFound.new

Serve a custom 404 page:

run Rack::NotFound.new('path/to/your/404.html')

Constant Summary collapse

F =
::File

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, content_type = 'text/html') ⇒ NotFound

Returns a new instance of NotFound.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/contrib/not_found.rb', line 16

def initialize(path = nil, content_type = 'text/html')
  if path.nil?
    @content = "Not found\n"
  else
    @content = F.read(path)
  end
  @length = @content.size.to_s

  @content_type = content_type
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
# File 'lib/rack/contrib/not_found.rb', line 27

def call(env)
  [404, {'Content-Type' => @content_type, 'Content-Length' => @length}, [@content]]
end