Class: Lennarb::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/request_handler.rb

Overview

Base class for the request handler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestHandler

Initialize the request handler.

Parameters:



11
12
13
# File 'lib/lennarb/request_handler.rb', line 11

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/lennarb/request_handler.rb', line 5

def app
  @app
end

Instance Method Details

#call(env) ⇒ Array

Call the app with the environment.

Parameters:

  • env (Hash)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lennarb/request_handler.rb', line 21

def call(env)
  http_method = env[Rack::REQUEST_METHOD].to_sym
  parts = env[Rack::PATH_INFO].split("/").reject(&:empty?)
  block, params = app.routes.match_route(parts, http_method)

  unless block
    return [404, {"content-type" => CONTENT_TYPE[:TEXT]}, ["Not Found"]]
  end

  req = Request.new(env, params)
  res = Response.new

  catch(:halt) do
    block.call(req, res)
    res.finish
  rescue Lennarb::Error => error
    [500, {"content-type" => CONTENT_TYPE[:TEXT]}, ["Internal Server Error (#{error.message})"]]
  end
end