Skip to content

Commit

Permalink
Switch to Ruby-2.x+ syntax (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
namusyaka authored and dblock committed Sep 13, 2016
1 parent 552f6dd commit 220c345
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 67 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AllCops:
TargetRubyVersion: 2.1
Include:
- Dangerfile
- gemfiles/*.gemfile
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#1480](https://github.com/ruby-grape/grape/pull/1480): Use the ruby-grape-danger gem for PR linting - [@dblock](https://github.com/dblock).
* [#1486](https://github.com/ruby-grape/grape/pull/1486): Implemented except in values validator - [@jonmchan](https://github.com/jonmchan).
* [#1470](https://github.com/ruby-grape/grape/pull/1470): Drop support for ruby-2.0 - [@namusyaka](https://github.com/namusyaka).
* [#1490](https://github.com/ruby-grape/grape/pull/1490): Switch to Ruby-2.x+ syntax - [@namusyaka](https://github.com/namusyaka).
* Your contribution here.

#### Fixes
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ def add_head_not_allowed_methods_and_options_methods

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

return if not_allowed_methods.empty?

@router.associate_routes(pattern, attributes.merge(not_allowed_methods: not_allowed_methods))
@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)
end

# Allows definition of endpoints that ignore the versioning configuration
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def each(&block)
@cookies.each(&block)
end

def delete(name, opts = {})
def delete(name, **opts)
options = opts.merge(value: 'deleted', expires: Time.at(0))
self.[]=(name, options)
end
Expand Down
5 changes: 2 additions & 3 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ def error!(message, status = nil, headers = nil)
# @param options [Hash] The options used when redirect.
# :permanent, default false.
# :body, default a short message including the URL.
def redirect(url, options = {})
permanent = options.fetch(:permanent, false)
body_message = options.fetch(:body, nil)
def redirect(url, permanent: false, body: nil, **_options)
body_message = body
if permanent
status 301
body_message ||= "This resource has been moved permanently to #{url}."
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/request_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def default_error_status(new_status = nil)
# rescue_from CustomError
# end
#
# @overload rescue_from(*exception_classes, options = {})
# @overload rescue_from(*exception_classes, **options)
# @param [Array] exception_classes A list of classes that you want to rescue, or
# the symbol :all to rescue from all exceptions.
# @param [Block] block Execution block to handle the given exception.
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def prepare_version
version.length == 1 ? version.first.to_s : version
end

def merge_route_options(default = {})
options[:route_options].clone.reverse_merge(default)
def merge_route_options(**default)
options[:route_options].clone.reverse_merge(**default)
end

def map_routes
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/error_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def formatters(options)
builtin_formatters.merge(default_elements).merge(options[:error_formatters] || {})
end

def formatter_for(api_format, options = {})
spec = formatters(options)[api_format]
def formatter_for(api_format, **options)
spec = formatters(**options)[api_format]
case spec
when nil
options[:default_error_formatter] || Grape::ErrorFormatter::Txt
Expand Down
36 changes: 18 additions & 18 deletions lib/grape/exceptions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class Base < StandardError

attr_reader :status, :message, :headers

def initialize(args = {})
@status = args[:status] || nil
@message = args[:message] || nil
@headers = args[:headers] || nil
def initialize(status: nil, message: nil, headers: nil, **_options)
@status = status
@message = message
@headers = headers
end

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

def translate_attributes(keys, options = {})
def translate_attributes(keys, **options)
keys.map do |key|
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", options.reverse_merge(default: key))
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
end.join(', ')
end

def translate_attribute(key, options = {})
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", options.reverse_merge(default: key))
def translate_attribute(key, **options)
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
end

def translate_message(key, options = {})
def translate_message(key, **options)
case key
when Symbol
translate("#{BASE_MESSAGES_KEY}.#{key}", options.reverse_merge(default: ''))
translate("#{BASE_MESSAGES_KEY}.#{key}", default: '', **options)
when Proc
key.call
else
key
end
end

def translate(key, options = {})
message = ::I18n.translate(key, options)
message.present? ? message : ::I18n.translate(key, options.merge(locale: FALLBACK_LOCALE))
def translate(key, **options)
message = ::I18n.translate(key, **options)
message.present? ? message : ::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/grape/exceptions/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ class Validation < Grape::Exceptions::Base
attr_accessor :params
attr_accessor :message_key

def initialize(args = {})
raise 'Params are missing:' unless args.key? :params
@params = args[:params]
if args.key?(:message)
@message_key = args[:message] if args[:message].is_a?(Symbol)
args[:message] = translate_message(args[:message])
def initialize(params:, message: nil, **args)
@params = params
if message
@message_key = message if message.is_a?(Symbol)
args[:message] = translate_message(message)
end
super
super(args)
end

# remove all the unnecessary stuff from Grape::Exceptions::Base like status
Expand Down
10 changes: 5 additions & 5 deletions lib/grape/exceptions/validation_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class ValidationErrors < Grape::Exceptions::Base

attr_reader :errors

def initialize(args = {})
def initialize(errors: [], headers: {}, **_options)
@errors = {}
args[:errors].each do |validation_error|
errors.each do |validation_error|
@errors[validation_error.params] ||= []
@errors[validation_error.params] << validation_error
end

super message: full_messages.join(', '), status: 400, headers: args[:headers]
super message: full_messages.join(', '), status: 400, headers: headers
end

def each
Expand All @@ -25,7 +25,7 @@ def each
end
end

def as_json(_opts = {})
def as_json(**_opts)
errors.map do |k, v|
{
params: k,
Expand All @@ -34,7 +34,7 @@ def as_json(_opts = {})
end
end

def to_json(_opts = {})
def to_json(**_opts)
as_json.to_json
end

Expand Down
4 changes: 2 additions & 2 deletions lib/grape/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def formatters(options)
builtin_formmaters.merge(default_elements).merge(options[:formatters] || {})
end

def formatter_for(api_format, options = {})
spec = formatters(options)[api_format]
def formatter_for(api_format, **options)
spec = formatters(**options)[api_format]
case spec
when nil
->(obj, _env) { obj }
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/middleware/auth/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Auth
class Base
attr_accessor :options, :app, :env

def initialize(app, options = {})
def initialize(app, **options)
@app = app
@options = options || {}
@options = options
end

def context
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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)
@options = default_options.merge(**options)
end

def default_options
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def default_options
}
end

def initialize(app, options = {})
def initialize(app, **options)
super
self.class.send(:include, @options[:helpers]) if @options[:helpers]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Namespace
# @option options :requirements [Hash] param-regex pairs, all of which must
# be met by a request's params for all endpoints in this namespace, or
# validation will fail and return a 422.
def initialize(space, options = {})
def initialize(space, **options)
@space = space.to_s
@options = options
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def parsers(options)
builtin_parsers.merge(default_elements).merge(options[:parsers] || {})
end

def parser_for(api_format, options = {})
spec = parsers(options)[api_format]
def parser_for(api_format, **options)
spec = parsers(**options)[api_format]
case spec
when nil
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/presenters/presenter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Grape
module Presenters
class Presenter
def self.represent(object, _options = {})
def self.represent(object, **_options)
object
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Router
attr_reader :map, :compiled

class Any < AttributeTranslator
def initialize(pattern, attributes = {})
def initialize(pattern, **attributes)
@pattern = pattern
super(attributes)
end
Expand Down Expand Up @@ -42,9 +42,9 @@ def append(route)
map[route.request_method.to_s.upcase] << route
end

def associate_routes(pattern, options = {})
def associate_routes(pattern, **options)
regexp = /(?<_#{@neutral_map.length}>)#{pattern.to_regexp}/
@neutral_map << Any.new(pattern, options.merge(regexp: regexp, index: @neutral_map.length))
@neutral_map << Any.new(pattern, regexp: regexp, index: @neutral_map.length, **options)
end

def call(env)
Expand Down
14 changes: 7 additions & 7 deletions lib/grape/router/pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Pattern
def_delegators :@regexp, :===
alias match? ===

def initialize(pattern, options = {})
def initialize(pattern, **options)
@origin = pattern
@path = build_path(pattern, options)
@path = build_path(pattern, **options)
@capture = extract_capture(options)
@pattern = Mustermann.new(@path, pattern_options)
@regexp = to_regexp
Expand All @@ -34,13 +34,13 @@ def pattern_options
options
end

def build_path(pattern, options = {})
pattern << '*path' unless options[:anchor] || pattern.end_with?('*path')
pattern + options[:suffix].to_s
def build_path(pattern, anchor: false, suffix: nil, **_options)
pattern << '*path' unless anchor || pattern.end_with?('*path')
pattern + suffix.to_s
end

def extract_capture(options = {})
requirements = {}.merge(options[:requirements])
def extract_capture(requirements: {}, **options)
requirements = {}.merge(requirements)
supported_capture.each_with_object(requirements) do |field, capture|
option = Array(options[field])
capture[field] = option.map(&:to_s) if option.present?
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def route_path
pattern.path
end

def initialize(method, pattern, options = {})
def initialize(method, pattern, **options)
@suffix = options[:suffix]
@options = options.merge(method: method.to_s.upcase)
@pattern = Pattern.new(pattern, options)
@translator = AttributeTranslator.new(options.merge(request_method: method.to_s.upcase))
@pattern = Pattern.new(pattern, **options)
@translator = AttributeTranslator.new(**options, request_method: method.to_s.upcase)
end

def exec(env)
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/exceptions/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Grape::Exceptions::Validation do
it 'fails when params are missing' do
expect { Grape::Exceptions::Validation.new(message: 'presence') }.to raise_error(RuntimeError, 'Params are missing:')
expect { Grape::Exceptions::Validation.new(message: 'presence') }.to raise_error(ArgumentError, 'missing keyword: params')
end
context 'when message is a symbol' do
it 'stores message_key' do
Expand Down

0 comments on commit 220c345

Please sign in to comment.