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

#routes_with_verbsObject (readonly)

Returns the value of attribute routes_with_verbs.



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

def routes_with_verbs
  @routes_with_verbs
end

Instance Method Details

#compileObject



51
52
53
54
55
56
57
# File 'lib/howl-router/router.rb', line 51

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

#increment_orderObject



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

def increment_order
  @current_order += 1
end

#raise_method_not_allowed(request, matched_routes) ⇒ Object

Raises:

  • (MethodNotAllowed)


65
66
67
68
# File 'lib/howl-router/router.rb', line 65

def raise_method_not_allowed(request, matched_routes)
  request.acceptable_methods = matched_routes.map(&:verb)
  raise MethodNotAllowed
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/howl-router/router.rb', line 9

def recognize(request)
  path_info, verb, request_params = request.is_a?(Hash) ? [request['PATH_INFO'], request['REQUEST_METHOD'], {}] :
                                                          [request.path_info, request.request_method, request.params]
  verb = verb.downcase.to_sym
  ignore_slash_path_info = path_info
  ignore_slash_path_info = path_info[0..-2] if path_info != "/" and path_info[-1] == "/"

  # Convert hash key into symbol.
  request_params = request_params.inject({}) do |result, entry|
    result[entry[0].to_sym] = entry[1]
    result
  end

  all_matched_routes = @routes.select do |route|
    matcher = route.matcher
    matcher.match(matcher.mustermann? ? ignore_slash_path_info : path_info)
  end
  raise NotFound if all_matched_routes.empty? 

  raise_method_not_allowed(request, all_matched_routes) unless routes_with_verbs.has_key?(verb)
  result = all_matched_routes.map{|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!(route.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]
  }.compact
  raise_method_not_allowed(request, all_matched_routes) if result.empty?
  result
end

#reset!Object



59
60
61
62
63
# File 'lib/howl-router/router.rb', line 59

def reset!
  @routes            = []
  @routes_with_verbs = {}
  @current_order     = 0
end