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/attribute_translator.rb

Defined Under Namespace

Classes: Any, AttributeTranslator, Pattern, Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



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

def initialize
  @neutral_map = []
  @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

Instance Method Details

#append(route) ⇒ Object



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

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

#associate_routes(pattern, options = {}) ⇒ Object



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

def associate_routes(pattern, options = {})
  regexp = /(?<_#{@neutral_map.length}>)#{pattern.to_regexp}/
  @neutral_map << Any.new(pattern, options.merge(regexp: regexp, index: @neutral_map.length))
end

#call(env) ⇒ Object



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

def call(env)
  with_optimization do
    identity(env) || rotation(env) { |route| route.exec(env) }
  end
end

#compile!Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grape/router.rb', line 20

def compile!
  return if compiled
  @union = Regexp.union(@neutral_map.map(&:regexp))
  map.each do |method, routes|
    @optimized_map[method] = routes.map.with_index do |route, index|
      route.index = index
      route.regexp = /(?<_#{index}>#{route.pattern.to_regexp})/
    end
    @optimized_map[method] = Regexp.union(@optimized_map[method])
  end
  @compiled = true
end

#recognize_path(input) ⇒ Object



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

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