Class: Howl::Router

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

Direct Known Subclasses

Padrino::Router

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



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

def initialize
  reset!
end

Instance Attribute Details

#current_orderObject (readonly)

Returns the value of attribute current_order.



3
4
5
# File 'lib/howl-router/router.rb', line 3

def current_order
  @current_order
end

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/howl-router/router.rb', line 3

def routes
  @routes
end

Instance Method Details

#compileObject



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

def compile
  return if @current_order.zero?
  @routes.sort!{|a, b| a.order <=> b.order }
end

#increment_orderObject



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

def increment_order
  @current_order += 1
end

#recognize(request) ⇒ Object

Raises:

  • (NotFound)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/howl-router/router.rb', line 9

def recognize(request)
  path_info, verb, request_params = parse_request(request)
  ignore_slash_path_info = path_info
  ignore_slash_path_info = path_info[0..-2] if path_info != "/" and path_info[-1] == "/"
  matched_routes = scan_routes(path_info, ignore_slash_path_info)
  raise NotFound if matched_routes.empty?
  raise_method_not_allowed(request, matched_routes) unless matched_routes.find{|r|r.verb == verb}
  result = matched_routes.map do |route|
    next unless verb == route.verb
    params, matcher = {}, route.matcher
    match_data = matcher.match(matcher.mustermann? ? ignore_slash_path_info : path_info)
    if match_data.names.empty?
      params[:captures] = match_data.captures
    else
      params.merge!(match_data.names.inject({}){|result, name|
        result[name.to_sym] = match_data[name] ? Rack::Utils.unescape(match_data[name]) : nil
        result
      }).merge!(request_params){|key, self_value, new_value| self_value || new_value }
    end
    [route, params]
  end.compact
  result.empty? ? raise_method_not_allowed(request, matched_routes) : result
end

#reset!Object



42
43
44
45
# File 'lib/howl-router/router.rb', line 42

def reset!
  @routes            = []
  @current_order     = 0
end