Skip to content

Commit 48ebc25

Browse files
ericproulxdblock
andauthored
Use ActiveSupport try pattern (#2537)
* Replace several `if .. respond_to?` by simple `try` * Add CHANGELOG.md --------- Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent 6dfb610 commit 48ebc25

File tree

12 files changed

+17
-19
lines changed

12 files changed

+17
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#2532](https://github.com/ruby-grape/grape/pull/2532): Update RuboCop 1.71.2 - [@ericproulx](https://github.com/ericproulx).
66
* [#2535](https://github.com/ruby-grape/grape/pull/2535): Delegates calls to inner objects - [@ericproulx](https://github.com/ericproulx).
7+
* [#2537](https://github.com/ruby-grape/grape/pull/2537): Use activesupport `try` pattern - [@ericproulx](https://github.com/ericproulx).
78
* Your contribution here.
89

910
#### Fixes

lib/grape.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require 'active_support/core_ext/module/delegation'
2121
require 'active_support/core_ext/object/blank'
2222
require 'active_support/core_ext/object/deep_dup'
23+
require 'active_support/core_ext/object/try'
2324
require 'active_support/core_ext/object/duplicable'
2425
require 'active_support/core_ext/string/output_safety'
2526
require 'active_support/core_ext/string/exclude'

lib/grape/api.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ def skip_immediate_run?(instance, args)
148148
end
149149

150150
def any_lazy?(args)
151-
args.any? { |argument| argument.respond_to?(:lazy?) && argument.lazy? }
151+
args.any? { |argument| argument.try(:lazy?) }
152152
end
153153

154154
def evaluate_arguments(configuration, *args)
155155
args.map do |argument|
156-
if argument.respond_to?(:lazy?) && argument.lazy?
156+
if argument.try(:lazy?)
157157
argument.evaluate_from(configuration)
158158
elsif argument.is_a?(Hash)
159159
argument.transform_values { |value| evaluate_arguments(configuration, value).first }

lib/grape/api/instance.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def nest(*blocks, &block)
112112
def evaluate_as_instance_with_configuration(block, lazy: false)
113113
lazy_block = Grape::Util::Lazy::Block.new do |configuration|
114114
value_for_configuration = configuration
115-
self.configuration = value_for_configuration.evaluate if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy?
115+
self.configuration = value_for_configuration.evaluate if value_for_configuration.try(:lazy?)
116116
response = instance_eval(&block)
117117
self.configuration = value_for_configuration
118118
response

lib/grape/endpoint.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def before_each(new_setup = false, &block)
3333

3434
def run_before_each(endpoint)
3535
superclass.run_before_each(endpoint) unless self == Endpoint
36-
before_each.each { |blk| blk.call(endpoint) if blk.respond_to?(:call) }
36+
before_each.each { |blk| blk.try(:call, endpoint) }
3737
end
3838

3939
# @api private
@@ -138,7 +138,7 @@ def method_name
138138
end
139139

140140
def routes
141-
@routes ||= endpoints ? endpoints.collect(&:routes).flatten : to_routes
141+
@routes ||= endpoints&.collect(&:routes)&.flatten || to_routes
142142
end
143143

144144
def reset_routes!
@@ -228,7 +228,7 @@ def call!(env)
228228
# Return the collection of endpoints within this endpoint.
229229
# This is the case when an Grape::API mounts another Grape::API.
230230
def endpoints
231-
options[:app].endpoints if options[:app].respond_to?(:endpoints)
231+
@endpoints ||= options[:app].try(:endpoints)
232232
end
233233

234234
def equals?(endpoint)

lib/grape/middleware/formatter.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ def read_body_input
8383

8484
return unless (input = env[Rack::RACK_INPUT])
8585

86-
rewind_input input
86+
input.try(:rewind)
8787
body = env[Grape::Env::API_REQUEST_INPUT] = input.read
8888
begin
8989
read_rack_input(body) if body && !body.empty?
9090
ensure
91-
rewind_input input
91+
input.try(:rewind)
9292
end
9393
end
9494

@@ -173,10 +173,6 @@ def mime_array
173173
.sort_by { |_, quality_preference| -(quality_preference ? quality_preference.to_f : 1.0) }
174174
.flat_map { |mime, _| [mime, mime.sub(vendor_prefix_pattern, '')] }
175175
end
176-
177-
def rewind_input(input)
178-
input.rewind if input.respond_to?(:rewind)
179-
end
180176
end
181177
end
182178
end

lib/grape/router.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def transaction(env)
9393
return response unless cascade
9494

9595
# we need to close the body if possible before dismissing
96-
response[2].close if response[2].respond_to?(:close)
96+
response[2].try(:close)
9797
end
9898
end
9999
end

lib/grape/validations/validators/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def validate!(params)
4949
next if !@scope.required? && empty_val
5050
next unless @scope.meets_dependency?(val, params)
5151

52-
validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
52+
validate_param!(attr_name, val) if @required || val.try(:key?, attr_name)
5353
rescue Grape::Exceptions::Validation => e
5454
array_errors << e
5555
end
@@ -69,7 +69,7 @@ def message(default_key = nil)
6969

7070
def options_key?(key, options = nil)
7171
options = instance_variable_get(:@option) if options.nil?
72-
options.respond_to?(:key?) && options.key?(key) && !options[key].nil?
72+
options.try(:key?, key) && !options[key].nil?
7373
end
7474

7575
def fail_fast?

lib/grape/validations/validators/except_values_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(attrs, options, required, scope, opts)
1010
end
1111

1212
def validate_param!(attr_name, params)
13-
return unless params.respond_to?(:key?) && params.key?(attr_name)
13+
return unless params.try(:key?, attr_name)
1414

1515
excepts = @except.is_a?(Proc) ? @except.call : @except
1616
return if excepts.nil?

lib/grape/validations/validators/presence_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Validations
55
module Validators
66
class PresenceValidator < Base
77
def validate_param!(attr_name, params)
8-
return if params.respond_to?(:key?) && params.key?(attr_name)
8+
return if params.try(:key?, attr_name)
99

1010
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:presence))
1111
end

lib/grape/validations/validators/regexp_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Validations
55
module Validators
66
class RegexpValidator < Base
77
def validate_param!(attr_name, params)
8-
return unless params.respond_to?(:key?) && params.key?(attr_name)
8+
return unless params.try(:key?, attr_name)
99
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.scrub.match?((options_key?(:value) ? @option[:value] : @option)) }
1010

1111
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp))

spec/support/chunked_response.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def each(&block)
2929

3030
# Close the response body if the response body supports it.
3131
def close
32-
@body.close if @body.respond_to?(:close)
32+
@body.try(:close)
3333
end
3434

3535
private

0 commit comments

Comments
 (0)