Class: Knot::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/knot/application.rb,
lib/knot/application/configuration.rb

Defined Under Namespace

Classes: Configuration

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_rack(app) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/knot/application.rb', line 15

def build_rack(app)
  builder = Rack::Builder.new
  builder.use Rack::Session::Cookie
  builder.use Rack::Protection
  builder.run app
  return builder
end

.configObject



28
29
30
# File 'lib/knot/application.rb', line 28

def config
  @config ||= Knot::Application::Configuration.new
end

.configure(&block) ⇒ Object



23
24
25
26
# File 'lib/knot/application.rb', line 23

def configure(&block)
  block.call(config)
  config.finalize!
end

.newObject



10
11
12
13
# File 'lib/knot/application.rb', line 10

def new
  app = new_without_middleware
  return build_rack(app).to_app
end

.new_without_middlewareObject



8
# File 'lib/knot/application.rb', line 8

alias :new_without_middleware :new

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/knot/application.rb', line 33

def call(env)
  request = Rack::Request.new(env)

  begin
    route_request(request)
  rescue => err
    handle_error(request, config._handlers[:internal_error], 500, err)
  end
end

#configObject



43
44
45
# File 'lib/knot/application.rb', line 43

def config
  self.class.config
end

#handle_error(request, handler, status, error = nil) ⇒ Object



60
61
62
63
64
# File 'lib/knot/application.rb', line 60

def handle_error(request, handler, status, error = nil)
  response = Knot::Router.new(request, handler)._call!(error)
  response.status = status
  return response.to_rack
end

#route_request(request) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/knot/application.rb', line 47

def route_request(request)
  config.routers.each do |router|
    begin
      response = router._call!(request)
      return response.to_rack
    rescue Knot::Dispatch::NoRouteError
      next
    end
  end

  handle_error(request, config._handlers[:not_found], 404)
end