diff --git a/CHANGELOG.md b/CHANGELOG.md index 8490e9cb3..ce9cd11fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * [#2532](https://github.com/ruby-grape/grape/pull/2532): Update RuboCop 1.71.2 - [@ericproulx](https://github.com/ericproulx). * [#2535](https://github.com/ruby-grape/grape/pull/2535): Delegates calls to inner objects - [@ericproulx](https://github.com/ericproulx). +* [#2537](https://github.com/ruby-grape/grape/pull/2537): Use activesupport `try` pattern - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/lib/grape.rb b/lib/grape.rb index ed6f9058a..58b9701be 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -20,6 +20,7 @@ require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/deep_dup' +require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/string/exclude' diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 4f7e97336..24d401745 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -148,12 +148,12 @@ def skip_immediate_run?(instance, args) end def any_lazy?(args) - args.any? { |argument| argument.respond_to?(:lazy?) && argument.lazy? } + args.any? { |argument| argument.try(:lazy?) } end def evaluate_arguments(configuration, *args) args.map do |argument| - if argument.respond_to?(:lazy?) && argument.lazy? + if argument.try(:lazy?) argument.evaluate_from(configuration) elsif argument.is_a?(Hash) argument.transform_values { |value| evaluate_arguments(configuration, value).first } diff --git a/lib/grape/api/instance.rb b/lib/grape/api/instance.rb index c6a608713..facd0f9f9 100644 --- a/lib/grape/api/instance.rb +++ b/lib/grape/api/instance.rb @@ -112,7 +112,7 @@ def nest(*blocks, &block) def evaluate_as_instance_with_configuration(block, lazy: false) lazy_block = Grape::Util::Lazy::Block.new do |configuration| value_for_configuration = configuration - self.configuration = value_for_configuration.evaluate if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy? + self.configuration = value_for_configuration.evaluate if value_for_configuration.try(:lazy?) response = instance_eval(&block) self.configuration = value_for_configuration response diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 23079b226..d7f60e9fe 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -33,7 +33,7 @@ def before_each(new_setup = false, &block) def run_before_each(endpoint) superclass.run_before_each(endpoint) unless self == Endpoint - before_each.each { |blk| blk.call(endpoint) if blk.respond_to?(:call) } + before_each.each { |blk| blk.try(:call, endpoint) } end # @api private @@ -138,7 +138,7 @@ def method_name end def routes - @routes ||= endpoints ? endpoints.collect(&:routes).flatten : to_routes + @routes ||= endpoints&.collect(&:routes)&.flatten || to_routes end def reset_routes! @@ -228,7 +228,7 @@ def call!(env) # Return the collection of endpoints within this endpoint. # This is the case when an Grape::API mounts another Grape::API. def endpoints - options[:app].endpoints if options[:app].respond_to?(:endpoints) + @endpoints ||= options[:app].try(:endpoints) end def equals?(endpoint) diff --git a/lib/grape/middleware/formatter.rb b/lib/grape/middleware/formatter.rb index 5cb761463..88a52afa5 100644 --- a/lib/grape/middleware/formatter.rb +++ b/lib/grape/middleware/formatter.rb @@ -83,12 +83,12 @@ def read_body_input return unless (input = env[Rack::RACK_INPUT]) - rewind_input input + input.try(:rewind) body = env[Grape::Env::API_REQUEST_INPUT] = input.read begin read_rack_input(body) if body && !body.empty? ensure - rewind_input input + input.try(:rewind) end end @@ -173,10 +173,6 @@ def mime_array .sort_by { |_, quality_preference| -(quality_preference ? quality_preference.to_f : 1.0) } .flat_map { |mime, _| [mime, mime.sub(vendor_prefix_pattern, '')] } end - - def rewind_input(input) - input.rewind if input.respond_to?(:rewind) - end end end end diff --git a/lib/grape/router.rb b/lib/grape/router.rb index 6889b4213..1b79324ae 100644 --- a/lib/grape/router.rb +++ b/lib/grape/router.rb @@ -93,7 +93,7 @@ def transaction(env) return response unless cascade # we need to close the body if possible before dismissing - response[2].close if response[2].respond_to?(:close) + response[2].try(:close) end end end diff --git a/lib/grape/validations/validators/base.rb b/lib/grape/validations/validators/base.rb index 890963d9b..beaba3502 100644 --- a/lib/grape/validations/validators/base.rb +++ b/lib/grape/validations/validators/base.rb @@ -49,7 +49,7 @@ def validate!(params) next if !@scope.required? && empty_val next unless @scope.meets_dependency?(val, params) - validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name)) + validate_param!(attr_name, val) if @required || val.try(:key?, attr_name) rescue Grape::Exceptions::Validation => e array_errors << e end @@ -69,7 +69,7 @@ def message(default_key = nil) def options_key?(key, options = nil) options = instance_variable_get(:@option) if options.nil? - options.respond_to?(:key?) && options.key?(key) && !options[key].nil? + options.try(:key?, key) && !options[key].nil? end def fail_fast? diff --git a/lib/grape/validations/validators/except_values_validator.rb b/lib/grape/validations/validators/except_values_validator.rb index 298eb0ab9..980226c1d 100644 --- a/lib/grape/validations/validators/except_values_validator.rb +++ b/lib/grape/validations/validators/except_values_validator.rb @@ -10,7 +10,7 @@ def initialize(attrs, options, required, scope, opts) end def validate_param!(attr_name, params) - return unless params.respond_to?(:key?) && params.key?(attr_name) + return unless params.try(:key?, attr_name) excepts = @except.is_a?(Proc) ? @except.call : @except return if excepts.nil? diff --git a/lib/grape/validations/validators/presence_validator.rb b/lib/grape/validations/validators/presence_validator.rb index ae31dc3fb..5961aa172 100644 --- a/lib/grape/validations/validators/presence_validator.rb +++ b/lib/grape/validations/validators/presence_validator.rb @@ -5,7 +5,7 @@ module Validations module Validators class PresenceValidator < Base def validate_param!(attr_name, params) - return if params.respond_to?(:key?) && params.key?(attr_name) + return if params.try(:key?, attr_name) raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:presence)) end diff --git a/lib/grape/validations/validators/regexp_validator.rb b/lib/grape/validations/validators/regexp_validator.rb index 86d3bbe0c..7b9b2864f 100644 --- a/lib/grape/validations/validators/regexp_validator.rb +++ b/lib/grape/validations/validators/regexp_validator.rb @@ -5,7 +5,7 @@ module Validations module Validators class RegexpValidator < Base def validate_param!(attr_name, params) - return unless params.respond_to?(:key?) && params.key?(attr_name) + return unless params.try(:key?, attr_name) return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.scrub.match?((options_key?(:value) ? @option[:value] : @option)) } raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp)) diff --git a/spec/support/chunked_response.rb b/spec/support/chunked_response.rb index 4e118d1dc..c74f18f09 100644 --- a/spec/support/chunked_response.rb +++ b/spec/support/chunked_response.rb @@ -29,7 +29,7 @@ def each(&block) # Close the response body if the response body supports it. def close - @body.close if @body.respond_to?(:close) + @body.try(:close) end private