Class: Grape::Router
- Inherits:
-
Object
- Object
- Grape::Router
- 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
-
#compiled ⇒ Object
readonly
Returns the value of attribute compiled.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
Class Method Summary collapse
-
.normalize_path(path) ⇒ Object
Taken from Rails normalize_path(“/foo”) # => “/foo” normalize_path(“/foo/”) # => “/foo” normalize_path(“foo”) # => “/foo” normalize_path(“”) # => “/” normalize_path(“/%ab”) # => “/%AB” github.com/rails/rails/blob/00cc4ff0259c0185fe08baadaa40e63ea2534f6e/actionpack/lib/action_dispatch/journey/router/utils.rb#L19.
Instance Method Summary collapse
- #append(route) ⇒ Object
- #associate_routes(pattern, options) ⇒ Object
- #call(env) ⇒ Object
- #compile! ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #recognize_path(input) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
33 34 35 36 37 38 |
# File 'lib/grape/router.rb', line 33 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
#compiled ⇒ Object (readonly)
Returns the value of attribute compiled.
5 6 7 |
# File 'lib/grape/router.rb', line 5 def compiled @compiled end |
#map ⇒ Object (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
Taken from Rails
normalize_path("/foo") # => "/foo"
normalize_path("/foo/") # => "/foo"
normalize_path("foo") # => "/foo"
normalize_path("") # => "/"
normalize_path("/%ab") # => "/%AB"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/grape/router.rb', line 14 def self.normalize_path(path) return +'/' unless path # Fast path for the overwhelming majority of paths that don't need to be normalized return path.dup if path == '/' || (path.start_with?('/') && !(path.end_with?('/') || path.match?(%r{%|//}))) # Slow path 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
55 56 57 |
# File 'lib/grape/router.rb', line 55 def append(route) map[route.request_method] << route end |
#associate_routes(pattern, options) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/grape/router.rb', line 59 def associate_routes(pattern, ) Grape::Router::GreedyRoute.new(pattern, ).then do |greedy_route| @neutral_regexes << greedy_route.to_regexp(@neutral_map.length) @neutral_map << greedy_route end end |
#call(env) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/grape/router.rb', line 66 def call(env) with_optimization do response, route = identity(env) response || rotation(env, route) end end |
#compile! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/grape/router.rb', line 40 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
73 74 75 76 77 78 |
# File 'lib/grape/router.rb', line 73 def recognize_path(input) any = with_optimization { greedy_match?(input) } return if any == default_response any.endpoint end |