Class: Eksa::Application

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

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



8
9
10
# File 'lib/eksa.rb', line 8

def initialize
  @routes = {}
end

Instance Method Details

#add_route(path, controller_class, action) ⇒ Object



12
13
14
# File 'lib/eksa.rb', line 12

def add_route(path, controller_class, action)
  @routes[path] = { controller: controller_class, action: action }
end

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eksa.rb', line 16

def call(env)
  request = Rack::Request.new(env)
  flash_message = request.cookies['eksa_flash']
  route = @routes[request.path_info]

  if route
    controller_instance = route[:controller].new(request)
    controller_instance.flash[:notice] = flash_message if flash_message
    
    response_body = controller_instance.send(route[:action])
    
    response = Rack::Response.new

    if controller_instance.status == 302
      response.redirect(controller_instance.redirect_url, 302)
    else
      response.write(response_body)
      response['content-type'] = 'text/html'
    end

    response.delete_cookie('eksa_flash') if flash_message
    response.finish
  else
    [404, { 'content-type' => 'text/html' }, ["<h1>404 - Not Found</h1>"]]
  end
end