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

Defined Under Namespace

Classes: BaseRoute, GreedyRoute, Pattern, Route

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



15
16
17
18
19
20
# File 'lib/grape/router.rb', line 15

def initialize
  @neutral_map = []
  @neutral_regexes = []
  @map = Hash.new { |hash, key| hash[key] = [] }
  @optimized_map = Hash.new { |hash, key| hash[key] = // }
end

Instance Attribute Details

#compiledObject (readonly)

Returns the value of attribute compiled.



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

def compiled
  @compiled
end

#mapObject (readonly)

Returns the value of attribute map.



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

def map
  @map
end

Class Method Details

.normalize_path(path) ⇒ Object



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

def self.normalize_path(path)
  path = +"/#{path}"
  path.squeeze!('/')
  path.sub!(%r{/+\Z}, '')
  path = '/' if path == ''
  path
end

Instance Method Details

#append(route) ⇒ Object



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

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

#associate_routes(pattern, **options) ⇒ Object



41
42
43
44
45
46
# File 'lib/grape/router.rb', line 41

def associate_routes(pattern, **options)
  Grape::Router::GreedyRoute.new(pattern: pattern, **options).then do |greedy_route|
    @neutral_regexes << greedy_route.to_regexp(@neutral_map.length)
    @neutral_map << greedy_route
  end
end

#call(env) ⇒ Object



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

def call(env)
  with_optimization do
    response, route = identity(env)
    response || rotation(env, route)
  end
end

#compile!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/grape/router.rb', line 22

def compile!
  return if compiled

  @union = Regexp.union(@neutral_regexes)
  @neutral_regexes = nil
  (Grape::Http::Headers::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



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