Skip to content

Commit c8d7d15

Browse files
committed
Clean up deprecated code and simplify validations
- Remove deprecated rack_response method from DSL - Remove deprecated exception aliases (MissingGroupTypeError, UnsupportedGroupTypeError) - Remove deprecated Grape::Validations::Base class - Simplify params_scope.rb by removing deprecated excepts parameter - Remove deprecated_class_examples.rb shared spec file - Update specs to remove references to deprecated classes Remove except_values test since already tested in except_values_spec.rb
1 parent be8f382 commit c8d7d15

File tree

13 files changed

+20
-257
lines changed

13 files changed

+20
-257
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* [#2572](https://github.com/ruby-grape/grape/pull/2572): Drop support ruby 2.7 and active_support 6.1 - [@ericproulx](https://github.com/ericproulx).
6+
* [#2573](https://github.com/ruby-grape/grape/pull/2573): Clean up deprecated code - [@ericproulx](https://github.com/ericproulx).
67
* Your contribution here.
78

89
#### Fixes
@@ -1176,3 +1177,4 @@
11761177
### 0.1.0 (2010/11/13)
11771178

11781179
* Initial public release - [@mbleigh](https://github.com/mbleigh).
1180+

UPGRADING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 3.0.0
5+
6+
#### Old Deprecations Clean Up
7+
8+
- `rack_response` has been removed in favor of using `error!`.
9+
- `Grape::Exceptions::MissingGroupType` and `Grape::Exceptions::UnsupportedGroupType` aliases `MissingGroupTypeError and `UnsupportedGroupType` have been removed.
10+
- `Grape::Validations::Base` has been removed in favor of `Grape::Validations::Validators::Base`.
11+
12+
See [2573](https://github.com/ruby-grape/grape/pull/2573) for more information.
13+
414
### Upgrading to >= 2.4.0
515

616
#### Grape::Middleware::Auth::Base

lib/grape/dsl/inside_route.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,6 @@ def error!(message, status = nil, additional_headers = nil, backtrace = nil, ori
178178
original_exception: original_exception
179179
end
180180

181-
# Creates a Rack response based on the provided message, status, and headers.
182-
# The content type in the headers is set to the default content type unless provided.
183-
# The message is HTML-escaped if the content type is 'text/html'.
184-
#
185-
# @param message [String] The content of the response.
186-
# @param status [Integer] The HTTP status code.
187-
# @params headers [Hash] (optional) Headers for the response
188-
# (default: {Rack::CONTENT_TYPE => content_type}).
189-
#
190-
# Returns:
191-
# A Rack::Response object containing the specified message, status, and headers.
192-
#
193-
def rack_response(message, status = 200, headers = { Rack::CONTENT_TYPE => content_type })
194-
Grape.deprecator.warn('The rack_response method has been deprecated, use error! instead.')
195-
message = Rack::Utils.escape_html(message) if headers[Rack::CONTENT_TYPE] == 'text/html'
196-
Rack::Response.new(Array.wrap(message), Rack::Utils.status_code(status), headers)
197-
end
198-
199181
# Redirect to a new url.
200182
#
201183
# @param url [String] The url to be redirect.

lib/grape/exceptions/missing_group_type.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ def initialize
99
end
1010
end
1111
end
12-
13-
Grape::Exceptions::MissingGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::MissingGroupTypeError', 'Grape::Exceptions::MissingGroupType', Grape.deprecator)

lib/grape/exceptions/unsupported_group_type.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ def initialize
99
end
1010
end
1111
end
12-
13-
Grape::Exceptions::UnsupportedGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::UnsupportedGroupTypeError', 'Grape::Exceptions::UnsupportedGroupType', Grape.deprecator)

lib/grape/validations/params_scope.rb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,30 +331,23 @@ def validates(attrs, validations)
331331
doc.type = coerce_type
332332

333333
default = validations[:default]
334-
335-
if (values_hash = validations[:values]).is_a? Hash
336-
values = values_hash[:value]
337-
# NB: excepts is deprecated
338-
excepts = values_hash[:except]
339-
else
340-
values = validations[:values]
341-
end
334+
values = validations[:values].is_a?(Hash) ? validations.dig(:values, :value) : validations[:values]
342335

343336
doc.values = values
344337

345-
except_values = options_key?(:except_values, :value, validations) ? validations[:except_values][:value] : validations[:except_values]
338+
except_values = validations[:except_values].is_a?(Hash) ? validations.dig(:except_values, :value) : validations[:except_values]
346339

347340
# NB. values and excepts should be nil, Proc, Array, or Range.
348341
# Specifically, values should NOT be a Hash
349342

350343
# use values or excepts to guess coerce type when stated type is Array
351-
coerce_type = guess_coerce_type(coerce_type, values, except_values, excepts)
344+
coerce_type = guess_coerce_type(coerce_type, values, except_values)
352345

353346
# default value should be present in values array, if both exist and are not procs
354-
check_incompatible_option_values(default, values, except_values, excepts)
347+
check_incompatible_option_values(default, values, except_values)
355348

356349
# type should be compatible with values array, if both exist
357-
validate_value_coercion(coerce_type, values, except_values, excepts)
350+
validate_value_coercion(coerce_type, values, except_values)
358351

359352
doc.document attrs
360353

@@ -462,18 +455,14 @@ def guess_coerce_type(coerce_type, *values_list)
462455
coerce_type
463456
end
464457

465-
def check_incompatible_option_values(default, values, except_values, excepts)
458+
def check_incompatible_option_values(default, values, except_values)
466459
return unless default && !default.is_a?(Proc)
467460

468461
raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values) if values && !values.is_a?(Proc) && !Array(default).all? { |def_val| values.include?(def_val) }
469462

470-
if except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) }
471-
raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, except_values)
472-
end
463+
return unless except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) }
473464

474-
return unless excepts && !excepts.is_a?(Proc)
475-
raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, excepts) \
476-
unless Array(default).none? { |def_val| excepts.include?(def_val) }
465+
raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, except_values)
477466
end
478467

479468
def validate(type, options, attrs, doc, opts)

lib/grape/validations/validators/base.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,3 @@ def fail_fast?
7979
end
8080
end
8181
end
82-
83-
Grape::Validations::Base = Class.new(Grape::Validations::Validators::Base) do
84-
def self.inherited(*)
85-
Grape.deprecator.warn 'Grape::Validations::Base is deprecated! Use Grape::Validations::Validators::Base instead.'
86-
super
87-
end
88-
end

spec/grape/api/custom_validations_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'shared/deprecated_class_examples'
4-
53
describe Grape::Validations do
6-
describe 'Grape::Validations::Base' do
7-
let(:deprecated_class) do
8-
Class.new(Grape::Validations::Base)
9-
end
10-
11-
it_behaves_like 'deprecated class'
12-
end
13-
144
describe 'using a custom length validator' do
155
subject do
166
Class.new(Grape::API) do

spec/grape/api_spec.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,26 +4679,6 @@ def uniqe_id_route
46794679
end
46804680
end
46814681

4682-
context 'rack_response deprecated' do
4683-
let(:app) do
4684-
Class.new(described_class) do
4685-
rescue_from :all do
4686-
rack_response('deprecated', 500, 'Content-Type' => 'text/plain')
4687-
end
4688-
4689-
get 'test' do
4690-
raise ArgumentError
4691-
end
4692-
end
4693-
end
4694-
4695-
it 'raises a deprecation' do
4696-
expect(Grape.deprecator).to receive(:warn).with('The rack_response method has been deprecated, use error! instead.')
4697-
get 'test'
4698-
expect(last_response.body).to eq('deprecated')
4699-
end
4700-
end
4701-
47024682
context 'rescue_from context' do
47034683
subject { last_response }
47044684

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
# frozen_string_literal: true
22

3-
require 'shared/deprecated_class_examples'
4-
53
RSpec.describe Grape::Exceptions::MissingGroupType do
64
describe '#message' do
75
subject { described_class.new.message }
86

97
it { is_expected.to include 'group type is required' }
108
end
11-
12-
describe 'Grape::Exceptions::MissingGroupTypeError' do
13-
let(:deprecated_class) { Grape::Exceptions::MissingGroupTypeError }
14-
15-
it_behaves_like 'deprecated class'
16-
end
179
end
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'shared/deprecated_class_examples'
4-
53
RSpec.describe Grape::Exceptions::UnsupportedGroupType do
64
subject { described_class.new }
75

@@ -10,10 +8,4 @@
108

119
it { is_expected.to include 'group type must be Array, Hash, JSON or Array[JSON]' }
1210
end
13-
14-
describe 'Grape::Exceptions::UnsupportedGroupTypeError' do
15-
let(:deprecated_class) { Grape::Exceptions::UnsupportedGroupTypeError }
16-
17-
it_behaves_like 'deprecated class'
18-
end
1911
end

0 commit comments

Comments
 (0)