Class: Grape::Router

Inherits:
Object
  • 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,
lib/grape/router/pattern/path.rb,
lib/grape/router/mustermann_pattern.rb

Defined Under Namespace

Classes: BaseRoute, GreedyRoute, MustermannPattern, Pattern, Route

Constant Summary collapse

DEFAULT_RESPONSE_HEADERS =
Grape::Util::Header.new.merge('X-Cascade' => 'pass').freeze
DEFAULT_RESPONSE_BODY =
['404 Not Found'].freeze

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



5
6
7
8
9
10
11
12
13
# File 'lib/grape/router.rb', line 5

def initialize
  @neutral_map = []
  @neutral_regexes = []
  # Plain hashes with no auto-vivifying default: a lookup for an HTTP method
  # that has no routes must not insert a key. `compile!` freezes both maps,
  # so request-time reads never mutate shared state (see #match? / #rotation).
  @map = {}
  @optimized_map = {}
end

Instance Method Details

#append(route) ⇒ Object



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

def append(route)
  (@map[route.request_method] ||= []) << route
end

#associate_routes(greedy_route) ⇒ Object



40
41
42
43
# File 'lib/grape/router.rb', line 40

def associate_routes(greedy_route)
  @neutral_regexes << greedy_route.to_regexp(@neutral_map.length)
  @neutral_map << greedy_route
end

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/grape/router.rb', line 45

def call(env)
  with_optimization do
    # Published on the env so the middleware the matched route runs -- the
    # path versioner above all -- reads the path this routed on instead of
    # normalizing PATH_INFO a second time.
    input = env[Grape::Env::GRAPE_NORMALIZED_PATH] = Grape::Util::PathNormalizer.call(env[Rack::PATH_INFO])
    transaction(input, env[Rack::REQUEST_METHOD], env)
  end
end

#compile!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/grape/router.rb', line 15

def compile!
  return if @compiled

  @union = resolve_capture_groups(Regexp.union(@neutral_regexes), @neutral_map)
  @neutral_regexes = nil
  # Compiled from the routes actually registered rather than from
  # Grape::HTTP_SUPPORTED_METHODS. A route declared with any other verb
  # (`route :purge, '/cache'`) is accepted at definition time and is
  # already collected into the resource's Allow header, so skipping it here
  # left it in @map but out of @optimized_map — the only map #match? reads.
  # The resource then advertised a method that could never be matched and
  # answered every request for it with 405.
  @map.each do |method, routes|
    optimized_map = routes.map.with_index { |route, index| route.to_regexp(index) }
    @optimized_map[method] = resolve_capture_groups(Regexp.union(optimized_map), routes)
  end
  @map.freeze
  @optimized_map.freeze
  @compiled = true
end

#recognize_path(input) ⇒ Object



55
56
57
58
59
60
# File 'lib/grape/router.rb', line 55

def recognize_path(input)
  any = with_optimization { greedy_match?(input) }
  return if any == default_response

  any.endpoint
end