Skip to content

Commit 8391361

Browse files
authored
Merge pull request #2643 from ruby-grape/replace_try_by_respond_to
Replace all `try` to `respond_to`.
2 parents 886b13a + 93344fb commit 8391361

File tree

17 files changed

+35
-31
lines changed

17 files changed

+35
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#2637](https://github.com/ruby-grape/grape/pull/2637): Refactor declared method - [@ericproulx](https://github.com/ericproulx).
1010
* [#2639](https://github.com/ruby-grape/grape/pull/2639): Refactor mime_types_for - [@ericproulx](https://github.com/ericproulx).
1111
* [#2638](https://github.com/ruby-grape/grape/pull/2638): Remove unnecessary path string duplication - [@ericproulx](https://github.com/ericproulx).
12+
* [#2643](https://github.com/ruby-grape/grape/pull/2638): Remove `try` method in codebase - [@ericproulx](https://github.com/ericproulx).
1213
* Your contribution here.
1314

1415
#### Fixes

lib/grape.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
require 'active_support/core_ext/module/delegation'
2020
require 'active_support/core_ext/object/blank'
2121
require 'active_support/core_ext/object/deep_dup'
22-
require 'active_support/core_ext/object/try'
2322
require 'active_support/core_ext/object/duplicable'
2423
require 'active_support/core_ext/string/output_safety'
2524
require 'active_support/core_ext/string/exclude'

lib/grape/api.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ def skip_immediate_run?(instance, args, kwargs)
140140
end
141141

142142
def any_lazy?(args)
143-
args.any? { |argument| argument.try(:lazy?) }
143+
args.any? { |argument| argument_lazy?(argument) }
144144
end
145145

146146
def evaluate_arguments(configuration, *args)
147147
args.map do |argument|
148-
if argument.try(:lazy?)
148+
if argument_lazy?(argument)
149149
argument.evaluate_from(configuration)
150150
elsif argument.is_a?(Hash)
151151
argument.transform_values { |value| evaluate_arguments(configuration, value).first }
@@ -157,12 +157,8 @@ def evaluate_arguments(configuration, *args)
157157
end
158158
end
159159

160-
def never_mounted?
161-
mounted_instances.empty?
162-
end
163-
164-
def mounted_instances
165-
instances - [base_instance]
160+
def argument_lazy?(argument)
161+
argument.respond_to?(:lazy?) && argument.lazy?
166162
end
167163
end
168164
end

lib/grape/dsl/declared.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def declared_hash_attr(passed_params, options, declared_param, params_nested_pat
7575
else
7676
# If it is not a Hash then it does not have children.
7777
# Find its value or set it to nil.
78-
return unless options[:include_missing] || passed_params.try(:key?, declared_param)
78+
return unless options[:include_missing] || (passed_params.respond_to?(:key?) && passed_params.key?(declared_param))
7979

8080
rename_path = params_nested_path + [declared_param.to_s]
8181
renamed_param_name = renamed_params[rename_path]
@@ -103,7 +103,7 @@ def handle_passed_param(params_nested_path, has_passed_children: false, &_block)
103103

104104
if type == 'Hash' && !has_children
105105
{}
106-
elsif type == 'Array' || (type&.start_with?('[') && type.exclude?(','))
106+
elsif type == 'Array' || (type&.start_with?('[') && !type.include?(','))
107107
[]
108108
elsif type == 'Set' || type&.start_with?('#<Set')
109109
Set.new

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def nest(*blocks, &block)
276276
def evaluate_as_instance_with_configuration(block, lazy: false)
277277
lazy_block = Grape::Util::Lazy::Block.new do |configuration|
278278
value_for_configuration = configuration
279-
self.configuration = value_for_configuration.evaluate if value_for_configuration.try(:lazy?)
279+
self.configuration = value_for_configuration.evaluate if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy?
280280
response = instance_eval(&block)
281281
self.configuration = value_for_configuration
282282
response

lib/grape/endpoint.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def before_each(new_setup = false, &block)
3030

3131
def run_before_each(endpoint)
3232
superclass.run_before_each(endpoint) unless self == Endpoint
33-
before_each.each { |blk| blk.try(:call, endpoint) }
33+
before_each.each { |blk| blk.call(endpoint) }
3434
end
3535

3636
def block_to_unbound_method(block)
@@ -139,7 +139,7 @@ def call!(env)
139139
# Return the collection of endpoints within this endpoint.
140140
# This is the case when an Grape::API mounts another Grape::API.
141141
def endpoints
142-
@endpoints ||= options[:app].try(:endpoints)
142+
@endpoints ||= options[:app].respond_to?(:endpoints) ? options[:app].endpoints : nil
143143
end
144144

145145
def equals?(endpoint)
@@ -234,8 +234,10 @@ def run_validators(validators, request)
234234
end
235235

236236
def run_filters(filters, type = :other)
237+
return unless filters
238+
237239
ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters: filters, type: type) do
238-
filters&.each { |filter| instance_eval(&filter) }
240+
filters.each { |filter| instance_eval(&filter) }
239241
end
240242
end
241243

lib/grape/middleware/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def merge_default_options(options)
109109
options
110110
end
111111
end
112+
113+
def try_scrub(obj)
114+
(obj.respond_to?(:scrub) && obj.scrub) || obj
115+
end
112116
end
113117
end
114118
end

lib/grape/middleware/error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def error_response(error = {})
5858
h.merge!(error[:headers]) if error[:headers].is_a?(Hash)
5959
end
6060
backtrace = error[:backtrace] || error[:original_exception]&.backtrace || []
61-
original_exception = error.is_a?(Exception) ? error : error[:original_exception] || nil
61+
original_exception = error.is_a?(Exception) ? error : error[:original_exception]
6262
rack_response(status, headers, format_message(message, backtrace, original_exception))
6363
end
6464

lib/grape/middleware/formatter.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def read_body_input
6969
return if input.nil?
7070
return unless read_body_input?
7171

72-
input.try(:rewind)
72+
rewind = input.respond_to?(:rewind)
73+
74+
input.rewind if rewind
7375
body = env[Grape::Env::API_REQUEST_INPUT] = input.read
7476
begin
7577
read_rack_input(body)
7678
ensure
77-
input.try(:rewind)
79+
input.rewind if rewind
7880
end
7981
end
8082

@@ -130,7 +132,7 @@ def negotiate_content_type
130132
end
131133

132134
def format_from_extension
133-
request_path = rack_request.path.try(:scrub)
135+
request_path = try_scrub(rack_request.path)
134136
dot_pos = request_path.rindex('.')
135137
return unless dot_pos
136138

@@ -139,7 +141,7 @@ def format_from_extension
139141
end
140142

141143
def format_from_header
142-
accept_header = env['HTTP_ACCEPT'].try(:scrub)
144+
accept_header = try_scrub(env['HTTP_ACCEPT'])
143145
return if accept_header.blank? || accept_header == ALL_MEDIA_TYPES
144146

145147
media_type = Rack::Utils.best_q_match(accept_header, mime_types.keys)

lib/grape/middleware/versioner/accept_version_header.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Versioner
1818
# route.
1919
class AcceptVersionHeader < Base
2020
def before
21-
potential_version = env['HTTP_ACCEPT_VERSION'].try(:scrub)
21+
potential_version = try_scrub(env['HTTP_ACCEPT_VERSION'])
2222
not_acceptable!('Accept-Version header must be set.') if strict && potential_version.blank?
2323

2424
return if potential_version.blank?

0 commit comments

Comments
 (0)