Skip to content

Commit 6deb8f4

Browse files
authored
Fix Ruby 2.7 keyword deprecations (#2091)
1 parent 470f80c commit 6deb8f4

File tree

18 files changed

+51
-46
lines changed

18 files changed

+51
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [#2089](https://github.com/ruby-grape/grape/pull/2089): Specify order of mounting Grape with Rack::Cascade in README - [@jonmchan](https://github.com/jonmchan).
1111
* [#2083](https://github.com/ruby-grape/grape/pull/2083): Set `Cache-Control` header only for streamed responses - [@stanhu](https://github.com/stanhu).
1212
* [#2092](https://github.com/ruby-grape/grape/pull/2092): Correct an example params in Include Missing doc - [@huyvohcmc](https://github.com/huyvohcmc).
13+
* [#2091](https://github.com/ruby-grape/grape/pull/2091): Fix ruby 2.7 keyword deprecations - [@dim](https://github.com/dim).
1314

1415
### 1.4.0 (2020/07/10)
1516

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def const_missing(*args)
9090
# For instance, a descripcion could be done using: `desc configuration[:description]` if it may vary
9191
# depending on where the endpoint is mounted. Use with care, if you find yourself using configuration
9292
# too much, you may actually want to provide a new API rather than remount it.
93-
def mount_instance(opts = {})
93+
def mount_instance(**opts)
9494
instance = Class.new(@base_parent)
9595
instance.configuration = Grape::Util::EndpointConfiguration.new(opts[:configuration] || {})
9696
instance.base = self

lib/grape/dsl/routing.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def do_not_route_options!
7979
namespace_inheritable(:do_not_route_options, true)
8080
end
8181

82-
def mount(mounts, opts = {})
82+
def mount(mounts, **opts)
8383
mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
8484
mounts.each_pair do |app, path|
8585
if app.respond_to?(:mount_instance)
@@ -170,9 +170,7 @@ def namespace(space = nil, options = {}, &block)
170170
previous_namespace_description = @namespace_description
171171
@namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
172172
nest(block) do
173-
if space
174-
namespace_stackable(:namespace, Namespace.new(space, **options))
175-
end
173+
namespace_stackable(:namespace, Namespace.new(space, **options)) if space
176174
end
177175
@namespace_description = previous_namespace_description
178176
end

lib/grape/middleware/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Base
1414

1515
# @param [Rack Application] app The standard argument for a Rack middleware.
1616
# @param [Hash] options A hash of options, simply stored for use by subclasses.
17-
def initialize(app, options = {})
17+
def initialize(app, **options)
1818
@app = app
1919
@options = default_options.merge(options)
2020
@app_response = nil

lib/grape/middleware/error.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def default_options
1919
rescue_subclasses: true, # rescue subclasses of exceptions listed
2020
rescue_options: {
2121
backtrace: false, # true to display backtrace, true to let Grape handle Grape::Exceptions
22-
original_exception: false, # true to display exception
22+
original_exception: false # true to display exception
2323
},
2424
rescue_handlers: {}, # rescue handler blocks
2525
base_only_rescue_handlers: {}, # rescue handler blocks rescuing only the base class
26-
all_rescue_handler: nil, # rescue handler block to rescue from all exceptions
26+
all_rescue_handler: nil # rescue handler block to rescue from all exceptions
2727
}
2828
end
2929

@@ -38,15 +38,15 @@ def call!(env)
3838
error_response(catch(:error) do
3939
return @app.call(@env)
4040
end)
41-
rescue Exception => error # rubocop:disable Lint/RescueException
41+
rescue Exception => e # rubocop:disable Lint/RescueException
4242
handler =
43-
rescue_handler_for_base_only_class(error.class) ||
44-
rescue_handler_for_class_or_its_ancestor(error.class) ||
45-
rescue_handler_for_grape_exception(error.class) ||
46-
rescue_handler_for_any_class(error.class) ||
43+
rescue_handler_for_base_only_class(e.class) ||
44+
rescue_handler_for_class_or_its_ancestor(e.class) ||
45+
rescue_handler_for_grape_exception(e.class) ||
46+
rescue_handler_for_any_class(e.class) ||
4747
raise
4848

49-
run_rescue_handler(handler, error)
49+
run_rescue_handler(handler, e)
5050
end
5151
end
5252

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

7373
def rack_response(message, status = options[:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type })
74-
if headers[Grape::Http::Headers::CONTENT_TYPE] == TEXT_HTML
75-
message = ERB::Util.html_escape(message)
76-
end
74+
message = ERB::Util.html_escape(message) if headers[Grape::Http::Headers::CONTENT_TYPE] == TEXT_HTML
7775
Rack::Response.new([message], status, headers)
7876
end
7977

lib/grape/middleware/stack.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module Middleware
66
# It allows to insert and insert after
77
class Stack
88
class Middleware
9-
attr_reader :args, :block, :klass
9+
attr_reader :args, :opts, :block, :klass
1010

11-
def initialize(klass, *args, &block)
11+
def initialize(klass, *args, **opts, &block)
1212
@klass = klass
13-
@args = args
13+
@args = args
14+
@opts = opts
1415
@block = block
1516
end
1617

@@ -30,6 +31,18 @@ def ==(other)
3031
def inspect
3132
klass.to_s
3233
end
34+
35+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7')
36+
def use_in(builder)
37+
block ? builder.use(klass, *args, **opts, &block) : builder.use(klass, *args, **opts)
38+
end
39+
else
40+
def use_in(builder)
41+
args = self.args
42+
args += [opts] unless opts.empty?
43+
block ? builder.use(klass, *args, &block) : builder.use(klass, *args)
44+
end
45+
end
3346
end
3447

3548
include Enumerable
@@ -90,7 +103,7 @@ def merge_with(middleware_specs)
90103
def build(builder = Rack::Builder.new)
91104
others.shift(others.size).each(&method(:merge_with))
92105
middlewares.each do |m|
93-
m.block ? builder.use(m.klass, *m.args, &m.block) : builder.use(m.klass, *m.args)
106+
m.use_in(builder)
94107
end
95108
builder
96109
end

lib/grape/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Request < Rack::Request
88

99
alias rack_params params
1010

11-
def initialize(env, options = {})
11+
def initialize(env, **options)
1212
extend options[:build_params_with] || Grape.config.param_builder
1313
super(env)
1414
end

lib/grape/router.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def append(route)
4747

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

5353
def call(env)

lib/grape/router/attribute_translator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AttributeTranslator
2323

2424
ROUTER_ATTRIBUTES = %i[pattern index].freeze
2525

26-
def initialize(attributes = {})
26+
def initialize(**attributes)
2727
@attributes = attributes
2828
end
2929

@@ -37,7 +37,7 @@ def to_h
3737
attributes
3838
end
3939

40-
def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing
40+
def method_missing(method_name, *args)
4141
if setter?(method_name[-1])
4242
attributes[method_name[0..-1]] = *args
4343
else

lib/grape/util/base_inheritable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class BaseInheritable
99

1010
# @param inherited_values [Object] An object implementing an interface
1111
# of the Hash class.
12-
def initialize(inherited_values = {})
13-
@inherited_values = inherited_values
12+
def initialize(inherited_values = nil)
13+
@inherited_values = inherited_values || {}
1414
@new_values = {}
1515
end
1616

0 commit comments

Comments
 (0)