lib/grape/api/instance.rb in grape-1.5.3 vs lib/grape/api/instance.rb in grape-1.6.0
- old
+ new
@@ -8,16 +8,15 @@
# from this will represent a different API instance
class Instance
include Grape::DSL::API
class << self
- attr_reader :instance
- attr_reader :base
+ attr_reader :instance, :base
attr_accessor :configuration
def given(conditional_option, &block)
- evaluate_as_instance_with_configuration(block, lazy: true) if conditional_option && block_given?
+ evaluate_as_instance_with_configuration(block, lazy: true) if conditional_option && block
end
def mounted(&block)
evaluate_as_instance_with_configuration(block, lazy: true)
end
@@ -26,11 +25,11 @@
@base = grape_api
grape_api.instances << self
end
def to_s
- (base && base.to_s) || super
+ base&.to_s || super
end
def base_instance?
self == base.base_instance
end
@@ -80,10 +79,11 @@
end
end
def compile!
return if instance
+
LOCK.synchronize { compile unless instance }
end
# see Grape::Router#recognize_path
def recognize_path(path)
@@ -101,24 +101,22 @@
# block passed in. Allows for simple 'before' setups
# of settings stack pushes.
def nest(*blocks, &block)
blocks.reject!(&:nil?)
if blocks.any?
- evaluate_as_instance_with_configuration(block) if block_given?
+ evaluate_as_instance_with_configuration(block) if block
blocks.each { |b| evaluate_as_instance_with_configuration(b) }
reset_validations!
else
instance_eval(&block)
end
end
def evaluate_as_instance_with_configuration(block, lazy: false)
lazy_block = Grape::Util::LazyBlock.new do |configuration|
value_for_configuration = configuration
- if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy?
- self.configuration = value_for_configuration.evaluate
- end
+ 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
end
if base && base_instance? && lazy
@@ -177,11 +175,12 @@
# In some applications (e.g. mounting grape on rails), one might need to trap
# errors from reaching upstream. This is effectivelly done by unsetting
# X-Cascade. Default :cascade is true.
def cascade?
return self.class.namespace_inheritable(:cascade) if self.class.inheritable_setting.namespace_inheritable.key?(:cascade)
- return self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options) && self.class.namespace_inheritable(:version_options).key?(:cascade)
+ return self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options)&.key?(:cascade)
+
true
end
reset!
@@ -198,21 +197,19 @@
# Disable versioning so adding a route won't prepend versioning
# informations again.
without_root_prefix do
without_versioning do
versioned_route_configs.each do |config|
+ next if config[:options][:matching_wildchar]
+
allowed_methods = config[:methods].dup
- unless self.class.namespace_inheritable(:do_not_route_head)
- allowed_methods |= [Grape::Http::Headers::HEAD] if allowed_methods.include?(Grape::Http::Headers::GET)
- end
+ allowed_methods |= [Grape::Http::Headers::HEAD] if !self.class.namespace_inheritable(:do_not_route_head) && allowed_methods.include?(Grape::Http::Headers::GET)
allow_header = (self.class.namespace_inheritable(:do_not_route_options) ? allowed_methods : [Grape::Http::Headers::OPTIONS] | allowed_methods)
- unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Grape::Http::Headers::OPTIONS)
- config[:endpoint].options[:options_route_enabled] = true
- end
+ config[:endpoint].options[:options_route_enabled] = true unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Grape::Http::Headers::OPTIONS)
attributes = config.merge(allowed_methods: allowed_methods, allow_header: allow_header)
generate_not_allowed_method(config[:pattern], **attributes)
end
end
@@ -226,11 +223,11 @@
# 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 == '*' }
{
- options: {},
+ options: { matching_wildchar: matching_wildchar },
pattern: last_route.pattern,
requirements: last_route.requirements,
path: last_route.origin,
endpoint: last_route.app,
methods: matching_wildchar ? Grape::Http::Headers::SUPPORTED_METHODS : routes.map(&:request_method)
@@ -246,10 +243,9 @@
Grape::Http::Headers::SUPPORTED_METHODS
else
Grape::Http::Headers::SUPPORTED_METHODS_WITHOUT_OPTIONS
end
not_allowed_methods = supported_methods - allowed_methods
- return if not_allowed_methods.empty?
@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)
end
# Allows definition of endpoints that ignore the versioning configuration
# used by the rest of your API.