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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
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 @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/presence_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/regexp_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion spec/support/chunked_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down