Class: Spyder::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



7
8
9
# File 'lib/spyder/router.rb', line 7

def initialize
  @routes = Hash.new { |k, v| k[v] = [] }
end

Instance Attribute Details

#fallback_routeObject

Returns the value of attribute fallback_route.



5
6
7
# File 'lib/spyder/router.rb', line 5

def fallback_route
  @fallback_route
end

Instance Method Details

#add_route(verb, matcher, &handler) ⇒ Object



11
12
13
14
15
# File 'lib/spyder/router.rb', line 11

def add_route(verb, matcher, &handler)
  matcher = Mustermann.new(matcher) if matcher.is_a?(String)

  @routes[verb.to_s.upcase] << [matcher, handler]
end

#call(ctx, request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spyder/router.rb', line 17

def call(ctx, request)
  only_path = request.path_info

  handler, match_data = nil
  @routes[request.verb].any? do |mt, h|
    md = mt.match(only_path)
    if md
      handler = h
      match_data = md
    end
  end

  match_data ? handler.call(request, match_data) : Response.make_generic(:not_found)
end