Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Lint/EmptyBlock:
Style/Documentation:
Enabled: false

Style/HashSyntax:
EnforcedShorthandSyntax: always

Style/MultilineIfModifier:
Enabled: false

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#2657](https://github.com/ruby-grape/grape/pull/2657): Instantiate validators at definition time - [@ericproulx](https://github.com/ericproulx).
* [#2667](https://github.com/ruby-grape/grape/pull/2667): Skip instrumentation in run_validators when no validators present - [@ericproulx](https://github.com/ericproulx).
* [#2670](https://github.com/ruby-grape/grape/pull/2670): Added support for Rack 3.2.6 and better handling to rack exceptions - [@ericproulx](https://github.com/ericproulx).
* [#2671](https://github.com/ruby-grape/grape/pull/2671): Use ruby 3.1 shorthand kwargs syntax - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initial_setup(base_instance_parent)
def override_all_methods!
(base_instance.methods - Class.methods - NON_OVERRIDABLE).each do |method_override|
define_singleton_method(method_override) do |*args, **kwargs, &block|
add_setup(method: method_override, args: args, kwargs: kwargs, block: block)
add_setup(method: method_override, args:, kwargs:, block:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def collect_route_config_per_pattern(all_routes)
allow_header = namespace_inheritable[:do_not_route_options] ? allowed_methods : [Rack::OPTIONS] | allowed_methods
last_route.app.options[:options_route_enabled] = true unless namespace_inheritable[:do_not_route_options] || allowed_methods.include?(Rack::OPTIONS)

greedy_route = Grape::Router::GreedyRoute.new(last_route.pattern, endpoint: last_route.app, allow_header: allow_header)
greedy_route = Grape::Router::GreedyRoute.new(last_route.pattern, endpoint: last_route.app, allow_header:)
@router.associate_routes(greedy_route)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/declared_params_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def initialize(include_missing: true, evaluate_given: false, stringify: false, c
def call(passed_params, declared_params, route_params, renamed_params)
recursive_declared(
passed_params,
declared_params: declared_params,
route_params: route_params,
renamed_params: renamed_params
declared_params:,
route_params:,
renamed_params:
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/declared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def declared(passed_params, include_parent_namespaces: true, include_missing: tr
raise MethodNotYetAvailable unless before_filter_passed

contract_key_map = inheritable_setting.namespace_stackable[:contract_key_map]
handler = DeclaredParamsHandler.new(include_missing:, evaluate_given:, stringify:, contract_key_map: contract_key_map)
handler = DeclaredParamsHandler.new(include_missing:, evaluate_given:, stringify:, contract_key_map:)
declared_params = include_parent_namespaces ? inheritable_setting.route[:declared_params] : (inheritable_setting.namespace_stackable[:declared_params].last || [])
renamed_params = inheritable_setting.route[:renamed_params] || {}
route_params = options.dig(:route_options, :params) || {} # options = endpoint's option
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def desc(description, options = {}, &config_block)
endpoint_config = defined?(configuration) ? configuration : nil
Grape::Util::ApiDescription.new(description, endpoint_config, &config_block).settings
else
options.merge(description: description)
options.merge(description:)
end
inheritable_setting.namespace[:description] = settings
inheritable_setting.route[:description] = settings
Expand Down
12 changes: 6 additions & 6 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def error!(message, status = nil, additional_headers = nil, backtrace = nil, ori
status = self.status(status || inheritable_setting.namespace_inheritable[:default_error_status])
headers = additional_headers.present? ? header.merge(additional_headers) : header
throw :error,
message: message,
status: status,
headers: headers,
backtrace: backtrace,
original_exception: original_exception
message:,
status:,
headers:,
backtrace:,
original_exception:
end

# Redirect to a new url.
Expand Down Expand Up @@ -272,7 +272,7 @@ def entity_class_for_obj(object, options)
# @return the representation of the given object as done through
# the given entity_class.
def entity_representation_for(entity_class, object, options)
embeds = { env: env }
embeds = { env: }
embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION)
entity_class.represent(object, **embeds, **options)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def with(**opts, &)

%i[mutually_exclusive exactly_one_of at_least_one_of all_or_none_of].each do |validator|
define_method validator do |*attrs, message: nil|
validates(attrs, validator => { value: true, message: message })
validates(attrs, validator => { value: true, message: })
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/grape/dsl/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def mount(mounts, *opts)
endpoints << Grape::Endpoint.new(
in_setting,
method: :any,
path: path,
app: app,
path:,
app:,
route_options: { anchor: false },
forward_match: !app.respond_to?(:inheritable_setting),
for: self
Expand Down Expand Up @@ -167,7 +167,7 @@ def route(methods, paths = ['/'], route_options = {}, &)

new_endpoint = Grape::Endpoint.new(
inheritable_setting,
method: method,
method:,
path: paths,
for: self,
route_options: all_route_options,
Expand Down Expand Up @@ -203,7 +203,7 @@ def namespace(space = nil, requirements: nil, **options, &block)

within_namespace do
nest(block) do
inheritable_setting.namespace_stackable[:namespace] = Grape::Namespace.new(space, requirements: requirements, **options) if space
inheritable_setting.namespace_stackable[:namespace] = Grape::Namespace.new(space, requirements:, **options) if space
end
end
end
Expand All @@ -230,7 +230,7 @@ def route_param(param, requirements: nil, type: nil, **, &)
requires param, type: type
end if type

namespace(":#{param}", requirements: requirements, **, &)
namespace(":#{param}", requirements:, **, &)
end

# @return array of defined versions
Expand Down
22 changes: 11 additions & 11 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def inspect
protected

def run
ActiveSupport::Notifications.instrument('endpoint_run.grape', endpoint: self, env: env) do
ActiveSupport::Notifications.instrument('endpoint_run.grape', endpoint: self, env:) do
@request = Grape::Request.new(env, build_params_with: inheritable_setting.namespace_inheritable[:build_params_with])
begin
self.class.run_before_each(self)
Expand All @@ -174,7 +174,7 @@ def run
status 204
else
run_filters before_validations, :before_validation
run_validators request: request
run_validators(request:)
run_filters after_validations, :after_validation
response_object = execute
end
Expand Down Expand Up @@ -210,7 +210,7 @@ def run_validators(request:)
validation_errors = []

Grape::Validations::ParamScopeTracker.track do
ActiveSupport::Notifications.instrument('endpoint_run_validators.grape', endpoint: self, validators: validators, request:) do
ActiveSupport::Notifications.instrument('endpoint_run_validators.grape', endpoint: self, validators:, request:) do
validators.each do |validator|
validator.validate(request)
rescue Grape::Exceptions::Validation => e
Expand All @@ -229,7 +229,7 @@ def run_validators(request:)
def run_filters(filters, type = :other)
return unless filters

ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters: filters, type: type) do
ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters:, type:) do
filters.each { |filter| instance_eval(&filter) }
end
end
Expand Down Expand Up @@ -279,7 +279,7 @@ def to_routes

def prepare_default_route_attributes(route_options)
{
namespace: namespace,
namespace:,
version: prepare_version(inheritable_setting.namespace_inheritable[:version]),
requirements: prepare_routes_requirements(route_options[:requirements]),
prefix: inheritable_setting.namespace_inheritable[:root_prefix],
Expand Down Expand Up @@ -316,15 +316,15 @@ def build_stack
stack.use Rack::Head
stack.use Rack::Lint if lint?
stack.use Grape::Middleware::Error,
format: format,
content_types: content_types,
format:,
content_types:,
default_status: inheritable_setting.namespace_inheritable[:default_error_status],
rescue_all: inheritable_setting.namespace_inheritable[:rescue_all],
rescue_grape_exceptions: inheritable_setting.namespace_inheritable[:rescue_grape_exceptions],
default_error_formatter: inheritable_setting.namespace_inheritable[:default_error_formatter],
error_formatters: inheritable_setting.namespace_stackable_with_hash(:error_formatters),
rescue_options: inheritable_setting.namespace_stackable_with_hash(:rescue_options),
rescue_handlers: rescue_handlers,
rescue_handlers:,
base_only_rescue_handlers: inheritable_setting.namespace_stackable_with_hash(:base_only_rescue_handlers),
all_rescue_handler: inheritable_setting.namespace_inheritable[:all_rescue_handler],
grape_exceptions_rescue_handler: inheritable_setting.namespace_inheritable[:grape_exceptions_rescue_handler]
Expand All @@ -340,9 +340,9 @@ def build_stack
end

stack.use Grape::Middleware::Formatter,
format: format,
format:,
default_format: inheritable_setting.namespace_inheritable[:default_format] || :txt,
content_types: content_types,
content_types:,
formatters: inheritable_setting.namespace_stackable_with_hash(:formatters),
parsers: inheritable_setting.namespace_stackable_with_hash(:parsers)

Expand All @@ -360,7 +360,7 @@ def build_helpers

def build_response_cookies
response_cookies do |name, value|
cookie_value = value.is_a?(Hash) ? value : { value: value }
cookie_value = value.is_a?(Hash) ? value : { value: }
Rack::Utils.set_cookie_header! header, name, cookie_value
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/error_formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def present(message, env)
end

if presenter
embeds = { env: env }
embeds = { env: }
embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION)
presented_message = presenter.represent(presented_message, embeds).serializable_hash
end
Expand All @@ -51,7 +51,7 @@ def present(message, env)
def wrap_message(message)
return message if message.is_a?(Hash)

{ message: message }
{ message: }
end

def format_structured_message(_structured_message)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/incompatible_option_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class IncompatibleOptionValues < Base
def initialize(option1, value1, option2, value2)
super(message: compose_message(:incompatible_option_values, option1: option1, value1: value1, option2: option2, value2: value2))
super(message: compose_message(:incompatible_option_values, option1:, value1:, option2:, value2:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/invalid_accept_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class InvalidAcceptHeader < Base
def initialize(message, headers)
super(message: compose_message(:invalid_accept_header, message: message), status: 406, headers: headers)
super(message: compose_message(:invalid_accept_header, message:), status: 406, headers:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/invalid_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class InvalidFormatter < Base
def initialize(klass, to_format)
super(message: compose_message(:invalid_formatter, klass: klass, to_format: to_format))
super(message: compose_message(:invalid_formatter, klass:, to_format:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/invalid_message_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class InvalidMessageBody < Base
def initialize(body_format)
super(message: compose_message(:invalid_message_body, body_format: body_format), status: 400)
super(message: compose_message(:invalid_message_body, body_format:), status: 400)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/invalid_version_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class InvalidVersionHeader < Base
def initialize(message, headers)
super(message: compose_message(:invalid_version_header, message: message), status: 406, headers: headers)
super(message: compose_message(:invalid_version_header, message:), status: 406, headers:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/invalid_versioner_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class InvalidVersionerOption < Base
def initialize(strategy)
super(message: compose_message(:invalid_versioner_option, strategy: strategy))
super(message: compose_message(:invalid_versioner_option, strategy:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/method_not_allowed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class MethodNotAllowed < Base
def initialize(headers)
super(message: '405 Not Allowed', status: 405, headers: headers)
super(message: '405 Not Allowed', status: 405, headers:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/missing_mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class MissingMimeType < Base
def initialize(new_format)
super(message: compose_message(:missing_mime_type, new_format: new_format))
super(message: compose_message(:missing_mime_type, new_format:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/unknown_auth_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class UnknownAuthStrategy < Base
def initialize(strategy:)
super(message: compose_message(:unknown_auth_strategy, strategy: strategy))
super(message: compose_message(:unknown_auth_strategy, strategy:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/unknown_parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class UnknownParameter < Base
def initialize(param)
super(message: compose_message(:unknown_parameter, param: param))
super(message: compose_message(:unknown_parameter, param:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/unknown_params_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class UnknownParamsBuilder < Base
def initialize(params_builder_type)
super(message: compose_message(:unknown_params_builder, params_builder_type: params_builder_type))
super(message: compose_message(:unknown_params_builder, params_builder_type:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/unknown_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Exceptions
class UnknownValidator < Base
def initialize(validator_type)
super(message: compose_message(:unknown_validator, validator_type: validator_type))
super(message: compose_message(:unknown_validator, validator_type:))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(params:, message: nil, status: nil, headers: nil)
message = translate_message(message)
end

super(status: status, message: message, headers: headers)
super(status:, message:, headers:)
end

# Remove all the unnecessary stuff from Grape::Exceptions::Base like status
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/exceptions/validation_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ValidationErrors < Base

def initialize(errors: [], headers: {})
@errors = errors.group_by(&:params)
super(message: full_messages.join(', '), status: 400, headers: headers)
super(message: full_messages.join(', '), status: 400, headers:)
end

def each
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def format_message(message, backtrace, original_exception = nil)
throw :error,
status: 406,
message: "The requested format '#{format}' is not supported.",
backtrace: backtrace,
original_exception: original_exception
backtrace:,
original_exception:
end

def find_handler(klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def build_formatted_response(status, headers, bodies)
else
# Allow content-type to be explicitly overwritten
formatter = fetch_formatter(headers, options)
bodymap = ActiveSupport::Notifications.instrument('format_response.grape', formatter: formatter, env: env) do
bodymap = ActiveSupport::Notifications.instrument('format_response.grape', formatter:, env:) do
bodies.collect { |body| formatter.call(body, env) }
end
Rack::Response.new(bodymap, status, headers)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/versioner/accept_version_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def before
private

def not_acceptable!(message)
throw :error, status: 406, headers: error_headers, message: message
throw :error, status: 406, headers: error_headers, message:
end
end
end
Expand Down
Loading
Loading