lib/grape/router.rb in grape-1.3.3 vs lib/grape/router.rb in grape-1.4.0
- old
+ new
@@ -5,24 +5,16 @@
module Grape
class Router
attr_reader :map, :compiled
- class NormalizePathCache < Grape::Util::Cache
- def initialize
- @cache = Hash.new do |h, path|
- normalized_path = +"/#{path}"
- normalized_path.squeeze!('/')
- normalized_path.sub!(%r{/+\Z}, '')
- normalized_path = '/' if normalized_path.empty?
- h[path] = -normalized_path
- end
- end
- end
-
def self.normalize_path(path)
- NormalizePathCache[path]
+ path = +"/#{path}"
+ path.squeeze!('/')
+ path.sub!(%r{/+\Z}, '')
+ path = '/' if path == ''
+ path
end
def self.supported_methods
@supported_methods ||= Grape::Http::Headers::SUPPORTED_METHODS + ['*']
end
@@ -97,41 +89,38 @@
def transaction(env)
input, method = *extract_input_and_method(env)
response = yield(input, method)
return response if response && !(cascade = cascade?(response))
- neighbor = greedy_match?(input)
+ last_neighbor_route = greedy_match?(input)
- # If neighbor exists and request method is OPTIONS,
+ # If last_neighbor_route exists and request method is OPTIONS,
# return response by using #call_with_allow_headers.
- return call_with_allow_headers(
- env,
- neighbor.allow_header,
- neighbor.endpoint
- ) if neighbor && method == Grape::Http::Headers::OPTIONS && !cascade
+ return call_with_allow_headers(env, last_neighbor_route) if last_neighbor_route && method == Grape::Http::Headers::OPTIONS && !cascade
route = match?(input, '*')
- return neighbor.endpoint.call(env) if neighbor && cascade && route
+ return last_neighbor_route.endpoint.call(env) if last_neighbor_route && cascade && route
+
if route
response = process_route(route, env)
return response if response && !(cascade = cascade?(response))
end
- !cascade && neighbor ? call_with_allow_headers(env, neighbor.allow_header, neighbor.endpoint) : nil
+ return call_with_allow_headers(env, last_neighbor_route) if !cascade && last_neighbor_route
+
+ nil
end
def process_route(route, env)
- input, = *extract_input_and_method(env)
- routing_args = env[Grape::Env::GRAPE_ROUTING_ARGS]
- env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(routing_args, route, input)
+ prepare_env_from_route(env, route)
route.exec(env)
end
def make_routing_args(default_args, route, input)
args = default_args || { route_info: route }
- args.merge(route.params(input))
+ args.merge(route.params(input) || {})
end
def extract_input_and_method(env)
input = string_for(env[Grape::Http::Headers::PATH_INFO])
method = env[Grape::Http::Headers::REQUEST_METHOD]
@@ -158,12 +147,18 @@
return unless @union.match(input)
last_match = Regexp.last_match
@neutral_map.detect { |route| last_match["_#{route.index}"] }
end
- def call_with_allow_headers(env, methods, endpoint)
- env[Grape::Env::GRAPE_ALLOWED_METHODS] = methods.join(', ').freeze
- endpoint.call(env)
+ def call_with_allow_headers(env, route)
+ prepare_env_from_route(env, route)
+ env[Grape::Env::GRAPE_ALLOWED_METHODS] = route.allow_header.join(', ').freeze
+ route.endpoint.call(env)
+ end
+
+ def prepare_env_from_route(env, route)
+ input, = *extract_input_and_method(env)
+ env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(env[Grape::Env::GRAPE_ROUTING_ARGS], route, input)
end
def cascade?(response)
response && response[1][Grape::Http::Headers::X_CASCADE] == 'pass'
end