Class: Grape::Router
- Inherits:
-
Object
show all
- Defined in:
- lib/grape/router.rb,
lib/grape/router/route.rb,
lib/grape/router/pattern.rb,
lib/grape/router/base_route.rb,
lib/grape/router/greedy_route.rb
Defined Under Namespace
Classes: BaseRoute, GreedyRoute, Pattern, Route
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
32
33
34
35
36
37
|
# File 'lib/grape/router.rb', line 32
def initialize
@neutral_map = []
@neutral_regexes = []
@map = Hash.new { |hash, key| hash[key] = [] }
@optimized_map = Hash.new { |hash, key| hash[key] = // }
end
|
Class Method Details
.normalize_path(path) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/grape/router.rb', line 12
def self.normalize_path(path)
return '/' unless path
return path if path == '/'
return path if path.start_with?('/') && !(path.end_with?('/') || path.match?(%r{%|//}))
encoding = path.encoding
path = "/#{path}"
path.squeeze!('/')
unless path == '/'
path.delete_suffix!('/')
path.gsub!(/(%[a-f0-9]{2})/) { ::Regexp.last_match(1).upcase }
end
path.force_encoding(encoding)
end
|
Instance Method Details
#append(route) ⇒ Object
54
55
56
|
# File 'lib/grape/router.rb', line 54
def append(route)
@map[route.request_method] << route
end
|
#associate_routes(greedy_route) ⇒ Object
58
59
60
61
|
# File 'lib/grape/router.rb', line 58
def associate_routes(greedy_route)
@neutral_regexes << greedy_route.to_regexp(@neutral_map.length)
@neutral_map << greedy_route
end
|
#call(env) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/grape/router.rb', line 63
def call(env)
with_optimization do
input = Router.normalize_path(env[Rack::PATH_INFO])
method = env[Rack::REQUEST_METHOD]
response, route = identity(input, method, env)
response || rotation(input, method, env, route)
end
end
|
#compile! ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/grape/router.rb', line 39
def compile!
return if @compiled
@union = Regexp.union(@neutral_regexes)
@neutral_regexes = nil
(Grape::HTTP_SUPPORTED_METHODS + ['*']).each do |method|
next unless @map.key?(method)
routes = @map[method]
optimized_map = routes.map.with_index { |route, index| route.to_regexp(index) }
@optimized_map[method] = Regexp.union(optimized_map)
end
@compiled = true
end
|
#recognize_path(input) ⇒ Object
72
73
74
75
76
77
|
# File 'lib/grape/router.rb', line 72
def recognize_path(input)
any = with_optimization { greedy_match?(input) }
return if any == default_response
any.endpoint
end
|