Class: Aho::WebServer::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/aho/web_server/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



8
9
10
# File 'lib/aho/web_server/router.rb', line 8

def initialize
  @routes = Hash.new { |hash, key| hash[key] = {} }
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



6
7
8
# File 'lib/aho/web_server/router.rb', line 6

def routes
  @routes
end

Instance Method Details

#route_for(env) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/aho/web_server/router.rb', line 12

def route_for(env)
  path = env['PATH_INFO']
  method = env['REQUEST_METHOD'].downcase
  destination = routes.dig(method.to_sym, path)
  return if destination.nil?

  controller = Module.const_get(destination[:controller]).new(env)
  controller.public_send(destination[:action])
end