Class: Router

Inherits:
Object show all
Defined in:
lib/laris/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



26
27
28
# File 'lib/laris/router.rb', line 26

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



24
25
26
# File 'lib/laris/router.rb', line 24

def routes
  @routes
end

Instance Method Details

#add_route(pattern, http_method, controller_class, action_name) ⇒ Object



30
31
32
# File 'lib/laris/router.rb', line 30

def add_route(pattern, http_method, controller_class, action_name)
  @routes << Route.new(pattern, http_method, controller_class, action_name)
end

#draw(&proc) ⇒ Object



34
35
36
# File 'lib/laris/router.rb', line 34

def draw(&proc)
  instance_eval(&proc)
end

#match(req) ⇒ Object



44
45
46
# File 'lib/laris/router.rb', line 44

def match(req)
  @routes.find { |route| route.matches?(req) }
end

#run(req, res) ⇒ Object



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

def run(req, res)
  route = match(req)

  if route.nil?
    res.status = 404

    res.write("Oops! The requested URL #{req.path} was not not found!")
  else
    route.run(req, res)
  end
end