Class: Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



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

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

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



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

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

#draw(&proc) ⇒ Object



37
38
39
# File 'lib/router.rb', line 37

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

#match(req) ⇒ Object



47
48
49
# File 'lib/router.rb', line 47

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

#run(req, res) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/router.rb', line 51

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

  if route
    route.run(req, res)
  else
    res.status = 404
    res.write("Not Found")
  end
end