Skip to content

Commit 220c345

Browse files
namusyakadblock
authored andcommitted
Switch to Ruby-2.x+ syntax (#1490)
1 parent 552f6dd commit 220c345

22 files changed

+67
-67
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
AllCops:
2+
TargetRubyVersion: 2.1
23
Include:
34
- Dangerfile
45
- gemfiles/*.gemfile

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [#1480](https://github.com/ruby-grape/grape/pull/1480): Use the ruby-grape-danger gem for PR linting - [@dblock](https://github.com/dblock).
55
* [#1486](https://github.com/ruby-grape/grape/pull/1486): Implemented except in values validator - [@jonmchan](https://github.com/jonmchan).
66
* [#1470](https://github.com/ruby-grape/grape/pull/1470): Drop support for ruby-2.0 - [@namusyaka](https://github.com/namusyaka).
7+
* [#1490](https://github.com/ruby-grape/grape/pull/1490): Switch to Ruby-2.x+ syntax - [@namusyaka](https://github.com/namusyaka).
78
* Your contribution here.
89

910
#### Fixes

lib/grape/api.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ def add_head_not_allowed_methods_and_options_methods
194194

195195
# Generate a route that returns an HTTP 405 response for a user defined
196196
# path on methods not specified
197-
def generate_not_allowed_method(pattern, attributes = {})
198-
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - attributes[:allowed_methods]
197+
def generate_not_allowed_method(pattern, allowed_methods: [], **attributes)
198+
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - allowed_methods
199199
not_allowed_methods << Grape::Http::Headers::OPTIONS if self.class.namespace_inheritable(:do_not_route_options)
200200

201201
return if not_allowed_methods.empty?
202202

203-
@router.associate_routes(pattern, attributes.merge(not_allowed_methods: not_allowed_methods))
203+
@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)
204204
end
205205

206206
# Allows definition of endpoints that ignore the versioning configuration

lib/grape/cookies.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def each(&block)
3131
@cookies.each(&block)
3232
end
3333

34-
def delete(name, opts = {})
34+
def delete(name, **opts)
3535
options = opts.merge(value: 'deleted', expires: Time.at(0))
3636
self.[]=(name, options)
3737
end

lib/grape/dsl/inside_route.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ def error!(message, status = nil, headers = nil)
9797
# @param options [Hash] The options used when redirect.
9898
# :permanent, default false.
9999
# :body, default a short message including the URL.
100-
def redirect(url, options = {})
101-
permanent = options.fetch(:permanent, false)
102-
body_message = options.fetch(:body, nil)
100+
def redirect(url, permanent: false, body: nil, **_options)
101+
body_message = body
103102
if permanent
104103
status 301
105104
body_message ||= "This resource has been moved permanently to #{url}."

lib/grape/dsl/request_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def default_error_status(new_status = nil)
8888
# rescue_from CustomError
8989
# end
9090
#
91-
# @overload rescue_from(*exception_classes, options = {})
91+
# @overload rescue_from(*exception_classes, **options)
9292
# @param [Array] exception_classes A list of classes that you want to rescue, or
9393
# the symbol :all to rescue from all exceptions.
9494
# @param [Block] block Execution block to handle the given exception.

lib/grape/endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ def prepare_version
194194
version.length == 1 ? version.first.to_s : version
195195
end
196196

197-
def merge_route_options(default = {})
198-
options[:route_options].clone.reverse_merge(default)
197+
def merge_route_options(**default)
198+
options[:route_options].clone.reverse_merge(**default)
199199
end
200200

201201
def map_routes

lib/grape/error_formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def formatters(options)
1717
builtin_formatters.merge(default_elements).merge(options[:error_formatters] || {})
1818
end
1919

20-
def formatter_for(api_format, options = {})
21-
spec = formatters(options)[api_format]
20+
def formatter_for(api_format, **options)
21+
spec = formatters(**options)[api_format]
2222
case spec
2323
when nil
2424
options[:default_error_formatter] || Grape::ErrorFormatter::Txt

lib/grape/exceptions/base.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class Base < StandardError
77

88
attr_reader :status, :message, :headers
99

10-
def initialize(args = {})
11-
@status = args[:status] || nil
12-
@message = args[:message] || nil
13-
@headers = args[:headers] || nil
10+
def initialize(status: nil, message: nil, headers: nil, **_options)
11+
@status = status
12+
@message = message
13+
@headers = headers
1414
end
1515

1616
def [](index)
@@ -22,12 +22,12 @@ def [](index)
2222
# TODO: translate attribute first
2323
# if BASE_ATTRIBUTES_KEY.key respond to a string message, then short_message is returned
2424
# if BASE_ATTRIBUTES_KEY.key respond to a Hash, means it may have problem , summary and resolution
25-
def compose_message(key, attributes = {})
26-
short_message = translate_message(key, attributes)
25+
def compose_message(key, **attributes)
26+
short_message = translate_message(key, **attributes)
2727
if short_message.is_a? Hash
28-
@problem = problem(key, attributes)
29-
@summary = summary(key, attributes)
30-
@resolution = resolution(key, attributes)
28+
@problem = problem(key, **attributes)
29+
@summary = summary(key, **attributes)
30+
@resolution = resolution(key, **attributes)
3131
[['Problem', @problem], ['Summary', @summary], ['Resolution', @resolution]].reduce('') do |message, detail_array|
3232
message << "\n#{detail_array[0]}:\n #{detail_array[1]}" unless detail_array[1].blank?
3333
message
@@ -49,30 +49,30 @@ def resolution(key, attributes)
4949
translate_message("#{key}.resolution".to_sym, attributes)
5050
end
5151

52-
def translate_attributes(keys, options = {})
52+
def translate_attributes(keys, **options)
5353
keys.map do |key|
54-
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", options.reverse_merge(default: key))
54+
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
5555
end.join(', ')
5656
end
5757

58-
def translate_attribute(key, options = {})
59-
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", options.reverse_merge(default: key))
58+
def translate_attribute(key, **options)
59+
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
6060
end
6161

62-
def translate_message(key, options = {})
62+
def translate_message(key, **options)
6363
case key
6464
when Symbol
65-
translate("#{BASE_MESSAGES_KEY}.#{key}", options.reverse_merge(default: ''))
65+
translate("#{BASE_MESSAGES_KEY}.#{key}", default: '', **options)
6666
when Proc
6767
key.call
6868
else
6969
key
7070
end
7171
end
7272

73-
def translate(key, options = {})
74-
message = ::I18n.translate(key, options)
75-
message.present? ? message : ::I18n.translate(key, options.merge(locale: FALLBACK_LOCALE))
73+
def translate(key, **options)
74+
message = ::I18n.translate(key, **options)
75+
message.present? ? message : ::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
7676
end
7777
end
7878
end

lib/grape/exceptions/validation.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ class Validation < Grape::Exceptions::Base
66
attr_accessor :params
77
attr_accessor :message_key
88

9-
def initialize(args = {})
10-
raise 'Params are missing:' unless args.key? :params
11-
@params = args[:params]
12-
if args.key?(:message)
13-
@message_key = args[:message] if args[:message].is_a?(Symbol)
14-
args[:message] = translate_message(args[:message])
9+
def initialize(params:, message: nil, **args)
10+
@params = params
11+
if message
12+
@message_key = message if message.is_a?(Symbol)
13+
args[:message] = translate_message(message)
1514
end
16-
super
15+
super(args)
1716
end
1817

1918
# remove all the unnecessary stuff from Grape::Exceptions::Base like status

0 commit comments

Comments
 (0)