Skip to content

Commit

Permalink
Merge 89df3ec into e7e2501
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Jul 16, 2020
2 parents e7e2501 + 89df3ec commit be1c468
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/grape/api.rb
Expand Up @@ -90,7 +90,7 @@ def const_missing(*args)
# For instance, a descripcion could be done using: `desc configuration[:description]` if it may vary
# depending on where the endpoint is mounted. Use with care, if you find yourself using configuration
# too much, you may actually want to provide a new API rather than remount it.
def mount_instance(opts = {})
def mount_instance(**opts)
instance = Class.new(@base_parent)
instance.configuration = Grape::Util::EndpointConfiguration.new(opts[:configuration] || {})
instance.base = self
Expand Down
6 changes: 2 additions & 4 deletions lib/grape/dsl/routing.rb
Expand Up @@ -79,7 +79,7 @@ def do_not_route_options!
namespace_inheritable(:do_not_route_options, true)
end

def mount(mounts, opts = {})
def mount(mounts, **opts)
mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
if app.respond_to?(:mount_instance)
Expand Down Expand Up @@ -170,9 +170,7 @@ def namespace(space = nil, options = {}, &block)
previous_namespace_description = @namespace_description
@namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
nest(block) do
if space
namespace_stackable(:namespace, Namespace.new(space, **options))
end
namespace_stackable(:namespace, Namespace.new(space, **options)) if space
end
@namespace_description = previous_namespace_description
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/base.rb
Expand Up @@ -14,7 +14,7 @@ class Base

# @param [Rack Application] app The standard argument for a Rack middleware.
# @param [Hash] options A hash of options, simply stored for use by subclasses.
def initialize(app, options = {})
def initialize(app, **options)
@app = app
@options = default_options.merge(options)
@app_response = nil
Expand Down
22 changes: 10 additions & 12 deletions lib/grape/middleware/error.rb
Expand Up @@ -19,11 +19,11 @@ def default_options
rescue_subclasses: true, # rescue subclasses of exceptions listed
rescue_options: {
backtrace: false, # true to display backtrace, true to let Grape handle Grape::Exceptions
original_exception: false, # true to display exception
original_exception: false # true to display exception
},
rescue_handlers: {}, # rescue handler blocks
base_only_rescue_handlers: {}, # rescue handler blocks rescuing only the base class
all_rescue_handler: nil, # rescue handler block to rescue from all exceptions
all_rescue_handler: nil # rescue handler block to rescue from all exceptions
}
end

Expand All @@ -38,15 +38,15 @@ def call!(env)
error_response(catch(:error) do
return @app.call(@env)
end)
rescue Exception => error # rubocop:disable Lint/RescueException
rescue Exception => e # rubocop:disable Lint/RescueException
handler =
rescue_handler_for_base_only_class(error.class) ||
rescue_handler_for_class_or_its_ancestor(error.class) ||
rescue_handler_for_grape_exception(error.class) ||
rescue_handler_for_any_class(error.class) ||
rescue_handler_for_base_only_class(e.class) ||
rescue_handler_for_class_or_its_ancestor(e.class) ||
rescue_handler_for_grape_exception(e.class) ||
rescue_handler_for_any_class(e.class) ||
raise

run_rescue_handler(handler, error)
run_rescue_handler(handler, e)
end
end

Expand All @@ -65,15 +65,13 @@ def error_response(error = {})
message = error[:message] || options[:default_message]
headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }
headers.merge!(error[:headers]) if error[:headers].is_a?(Hash)
backtrace = error[:backtrace] || error[:original_exception] && error[:original_exception].backtrace || []
backtrace = error[:backtrace] || error[:original_exception]&.backtrace || []
original_exception = error.is_a?(Exception) ? error : error[:original_exception] || nil
rack_response(format_message(message, backtrace, original_exception), status, headers)
end

def rack_response(message, status = options[:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type })
if headers[Grape::Http::Headers::CONTENT_TYPE] == TEXT_HTML
message = ERB::Util.html_escape(message)
end
message = ERB::Util.html_escape(message) if headers[Grape::Http::Headers::CONTENT_TYPE] == TEXT_HTML
Rack::Response.new([message], status, headers)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/middleware/stack.rb
Expand Up @@ -90,7 +90,8 @@ def merge_with(middleware_specs)
def build(builder = Rack::Builder.new)
others.shift(others.size).each(&method(:merge_with))
middlewares.each do |m|
m.block ? builder.use(m.klass, *m.args, &m.block) : builder.use(m.klass, *m.args)
opts = m.args.extract_options!
m.block ? builder.use(m.klass, *m.args, **opts, &m.block) : builder.use(m.klass, *m.args, **opts)
end
builder
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/request.rb
Expand Up @@ -8,7 +8,7 @@ class Request < Rack::Request

alias rack_params params

def initialize(env, options = {})
def initialize(env, **options)
extend options[:build_params_with] || Grape.config.param_builder
super(env)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/router.rb
Expand Up @@ -47,7 +47,7 @@ def append(route)

def associate_routes(pattern, **options)
@neutral_regexes << Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
@neutral_map << Grape::Router::AttributeTranslator.new(options.merge(pattern: pattern, index: @neutral_map.length))
@neutral_map << Grape::Router::AttributeTranslator.new(**options, pattern: pattern, index: @neutral_map.length)
end

def call(env)
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/router/attribute_translator.rb
Expand Up @@ -23,7 +23,7 @@ class AttributeTranslator

ROUTER_ATTRIBUTES = %i[pattern index].freeze

def initialize(attributes = {})
def initialize(**attributes)
@attributes = attributes
end

Expand All @@ -37,7 +37,7 @@ def to_h
attributes
end

def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing
def method_missing(method_name, *args)
if setter?(method_name[-1])
attributes[method_name[0..-1]] = *args
else
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/util/base_inheritable.rb
Expand Up @@ -9,8 +9,8 @@ class BaseInheritable

# @param inherited_values [Object] An object implementing an interface
# of the Hash class.
def initialize(inherited_values = {})
@inherited_values = inherited_values
def initialize(inherited_values = nil)
@inherited_values = inherited_values || {}
@new_values = {}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/as.rb
Expand Up @@ -3,7 +3,7 @@
module Grape
module Validations
class AsValidator < Base
def initialize(attrs, options, required, scope, opts = {})
def initialize(attrs, options, required, scope, **opts)
@renamed_options = options
super
end
Expand Down
6 changes: 2 additions & 4 deletions lib/grape/validations/validators/base.rb
Expand Up @@ -13,7 +13,7 @@ class Base
# @param required [Boolean] attribute(s) are required or optional
# @param scope [ParamsScope] parent scope for this Validator
# @param opts [Hash] additional validation options
def initialize(attrs, options, required, scope, opts = {})
def initialize(attrs, options, required, scope, **opts)
@attrs = Array(attrs)
@option = options
@required = required
Expand Down Expand Up @@ -47,9 +47,7 @@ def validate!(params)
next if !@scope.required? && empty_val
next unless @scope.meets_dependency?(val, params)
begin
if @required || val.respond_to?(:key?) && val.key?(attr_name)
validate_param!(attr_name, val)
end
validate_param!(attr_name, val) if @required || val.respond_to?(:key?) && val.key?(attr_name)
rescue Grape::Exceptions::Validation => e
array_errors << e
end
Expand Down
6 changes: 2 additions & 4 deletions lib/grape/validations/validators/default.rb
Expand Up @@ -3,7 +3,7 @@
module Grape
module Validations
class DefaultValidator < Base
def initialize(attrs, options, required, scope, opts = {})
def initialize(attrs, options, required, scope, **opts)
@default = options
super
end
Expand All @@ -21,9 +21,7 @@ def validate_param!(attr_name, params)
def validate!(params)
attrs = SingleAttributeIterator.new(self, @scope, params)
attrs.each do |resource_params, attr_name|
if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
validate_param!(attr_name, resource_params)
end
validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/except_values.rb
Expand Up @@ -3,7 +3,7 @@
module Grape
module Validations
class ExceptValuesValidator < Base
def initialize(attrs, options, required, scope, opts = {})
def initialize(attrs, options, required, scope, **opts)
@except = options.is_a?(Hash) ? options[:value] : options
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/values.rb
Expand Up @@ -3,7 +3,7 @@
module Grape
module Validations
class ValuesValidator < Base
def initialize(attrs, options, required, scope, opts = {})
def initialize(attrs, options, required, scope, **opts)
if options.is_a?(Hash)
@excepts = options[:except]
@values = options[:value]
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/middleware/error_spec.rb
Expand Up @@ -30,7 +30,7 @@ def app
opts = options
Rack::Builder.app do
use Spec::Support::EndpointFaker
use Grape::Middleware::Error, opts
use Grape::Middleware::Error, **opts
run ErrorSpec::ErrApp
end
end
Expand Down
8 changes: 3 additions & 5 deletions spec/support/versioned_helpers.rb
Expand Up @@ -6,7 +6,7 @@ module Support
module Helpers
# Returns the path with options[:version] prefixed if options[:using] is :path.
# Returns normal path otherwise.
def versioned_path(options = {})
def versioned_path(**options)
case options[:using]
when :path
File.join('/', options[:prefix] || '', options[:version], options[:path])
Expand Down Expand Up @@ -43,13 +43,11 @@ def versioned_headers(**options)
end
end

def versioned_get(path, version_name, version_options = {})
def versioned_get(path, version_name, **version_options)
path = versioned_path(version_options.merge(version: version_name, path: path))
headers = versioned_headers(**version_options.merge(version: version_name))
params = {}
if version_options[:using] == :param
params = { version_options[:parameter] => version_name }
end
params = { version_options[:parameter] => version_name } if version_options[:using] == :param
get path, params, headers
end
end
Expand Down

0 comments on commit be1c468

Please sign in to comment.