lib/grape/api/instance.rb in grape-2.0.0 vs lib/grape/api/instance.rb in grape-2.1.0

- old
+ new

@@ -1,9 +1,7 @@ # frozen_string_literal: true -require 'grape/router' - module Grape class API # The API Instance class, is the engine behind Grape::API. Each class that inherits # from this will represent a different API instance class Instance @@ -110,11 +108,11 @@ instance_eval(&block) end end def evaluate_as_instance_with_configuration(block, lazy: false) - lazy_block = Grape::Util::LazyBlock.new do |configuration| + lazy_block = Grape::Util::Lazy::Block.new do |configuration| value_for_configuration = configuration self.configuration = value_for_configuration.evaluate if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy? response = instance_eval(&block) self.configuration = value_for_configuration response @@ -125,10 +123,11 @@ lazy_block.evaluate_from(configuration) end end def inherited(subclass) + super subclass.reset! subclass.logger = logger.clone end def inherit_settings(other_settings) @@ -160,13 +159,17 @@ @router.freeze end # Handle a request. See Rack documentation for what `env` is. def call(env) - result = @router.call(env) - result[1].delete(Grape::Http::Headers::X_CASCADE) unless cascade? - result + status, headers, response = @router.call(env) + unless cascade? + headers = Grape::Util::Header.new.merge(headers) + headers.delete(Grape::Http::Headers::X_CASCADE) + end + + [status, headers, response] end # Some requests may return a HTTP 404 error if grape cannot find a matching # route. In this case, Grape::Router adds a X-Cascade header to the response # and sets it to 'pass', indicating to grape's parents they should keep @@ -201,25 +204,25 @@ versioned_route_configs.each do |config| next if config[:options][:matching_wildchar] allowed_methods = config[:methods].dup - allowed_methods |= [Grape::Http::Headers::HEAD] if !self.class.namespace_inheritable(:do_not_route_head) && allowed_methods.include?(Grape::Http::Headers::GET) + allowed_methods |= [Rack::HEAD] if !self.class.namespace_inheritable(:do_not_route_head) && allowed_methods.include?(Rack::GET) - allow_header = (self.class.namespace_inheritable(:do_not_route_options) ? allowed_methods : [Grape::Http::Headers::OPTIONS] | allowed_methods) + allow_header = (self.class.namespace_inheritable(:do_not_route_options) ? allowed_methods : [Rack::OPTIONS] | allowed_methods) - config[:endpoint].options[:options_route_enabled] = true unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Grape::Http::Headers::OPTIONS) + config[:endpoint].options[:options_route_enabled] = true unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Rack::OPTIONS) attributes = config.merge(allowed_methods: allowed_methods, allow_header: allow_header) generate_not_allowed_method(config[:pattern], **attributes) end end end end def collect_route_config_per_pattern all_routes = self.class.endpoints.map(&:routes).flatten - routes_by_regexp = all_routes.group_by { |route| route.pattern.to_regexp } + routes_by_regexp = all_routes.group_by(&:pattern_regexp) # Build the configuration based on the first endpoint and the collection of methods supported. routes_by_regexp.values.map do |routes| last_route = routes.last # Most of the configuration is taken from the last endpoint matching_wildchar = routes.any? { |route| route.request_method == '*' }