Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address some Ruby warnings #1995

Merged
merged 5 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Your contribution here.

#### Fixes

* [#1995](https://github.com/ruby-grape/grape/pull/1995): Address some Ruby warnings (undefined instance variables, method redefined) - [@nbeyer](https://github.com/nbeyer).
nbeyer marked this conversation as resolved.
Show resolved Hide resolved
* [#1994](https://github.com/ruby-grape/grape/pull/1993): Fix typos in README - [@bellmyer](https://github.com/bellmyer).
* [#1993](https://github.com/ruby-grape/grape/pull/1993): Lazy join allow header - [@ericproulx](https://github.com/ericproulx).
* [#1987](https://github.com/ruby-grape/grape/pull/1987): Re-add exactly_one_of mutually exclusive error message - [@ZeroInputCtrl](https://github.com/ZeroInputCtrl).
Expand All @@ -13,6 +15,7 @@
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
* [#1988](https://github.com/ruby-grape/grape/pull/1988): Refactored the full_messages method and stop overriding full_message - [@hosseintoussi](https://github.com/hosseintoussi).

### 1.3.0 (2020/01/11)

#### Features
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ group :test do
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'mime-types'
gem 'multi_xml'
nbeyer marked this conversation as resolved.
Show resolved Hide resolved
gem 'rack-jsonp', require: 'rack/jsonp'
gem 'rack-test', '~> 1.1.0'
gem 'rspec', '~> 3.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Grape
# should subclass this class in order to build an API.
class API
# Class methods that we want to call on the API rather than on the API object
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile!]).freeze
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile! inherited]).freeze

class << self
attr_accessor :base_instance, :instances
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def api_changed(new_api)
protected

def process_named_params
return unless @named_params && @named_params.any?
return unless instance_variable_defined?(:@named_params) && @named_params && @named_params.any?
api.namespace_stackable(:named_params, @named_params)
end
end
Expand Down
17 changes: 9 additions & 8 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ def status(status = nil)
when Integer
@status = status
when nil
return @status if @status
return @status if instance_variable_defined?(:@status) && @status
case request.request_method.to_s.upcase
when Grape::Http::Headers::POST
201
when Grape::Http::Headers::DELETE
if @body.present?
if instance_variable_defined?(:@body) && @body.present?
200
else
204
Expand Down Expand Up @@ -238,7 +238,7 @@ def body(value = nil)
@body = ''
status 204
else
@body
instance_variable_defined?(:@body) ? @body : nil
end
end

Expand Down Expand Up @@ -272,7 +272,7 @@ def file(value = nil)
warn '[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.'
@file = Grape::ServeFile::FileResponse.new(value)
else
@file
instance_variable_defined?(:@file) ? @file : nil
end
end

Expand Down Expand Up @@ -331,11 +331,12 @@ def present(*args)
end

representation = { root => representation } if root

if key
representation = (@body || {}).merge(key => representation)
elsif entity_class.present? && @body
representation = (body || {}).merge(key => representation)
elsif entity_class.present? && body
raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
representation = @body.merge(representation)
representation = body.merge(representation)
end

body representation
Expand Down Expand Up @@ -387,7 +388,7 @@ def entity_class_for_obj(object, options)
def entity_representation_for(entity_class, object, options)
embeds = { env: env }
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
entity_class.represent(object, **embeds.merge(options))
entity_class.represent(object, embeds.merge(options))
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def requires(*attrs, &block)

opts = attrs.extract_options!.clone
opts[:presence] = { value: true, message: opts[:message] }
opts = @group.merge(opts) if @group
opts = @group.merge(opts) if instance_variable_defined?(:@group) && @group

if opts[:using]
require_required_and_optional_fields(attrs.first, opts)
Expand All @@ -146,7 +146,7 @@ def optional(*attrs, &block)

opts = attrs.extract_options!.clone
type = opts[:type]
opts = @group.merge(opts) if @group
opts = @group.merge(opts) if instance_variable_defined?(:@group) && @group

# check type for optional parameter group
if attrs && block_given?
Expand Down Expand Up @@ -243,8 +243,8 @@ def map_params(params, element)
# @return hash of parameters relevant for the current scope
# @api private
def params(params)
params = @parent.params(params) if @parent
params = map_params(params, @element) if @element
params = @parent.params(params) if instance_variable_defined?(:@parent) && @parent
params = map_params(params, @element) if instance_variable_defined?(:@element) && @element
params
end

Expand Down
4 changes: 3 additions & 1 deletion lib/grape/dsl/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def version(*args, &block)
end
end

@versions.last unless @versions.nil?
@versions.last if instance_variable_defined?(:@versions) && @versions
end

# Define a root URL prefix for your entire API.
Expand Down Expand Up @@ -163,6 +163,8 @@ def route(methods, paths = ['/'], route_options = {}, &block)
# end
# end
def namespace(space = nil, options = {}, &block)
@namespace_description = nil unless instance_variable_defined?(:@namespace_description) && @namespace_description

if space || block_given?
within_namespace do
previous_namespace_description = @namespace_description
Expand Down
13 changes: 7 additions & 6 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ class DummyFormatClass

it 'adds a before filter to current and child namespaces only' do
subject.get '/' do
"root - #{@foo}"
"root - #{instance_variable_defined?(:@foo) ? @foo : nil}"
end
subject.namespace :blah do
before { @foo = 'foo' }
Expand Down Expand Up @@ -3677,12 +3677,13 @@ def before
end
end
context ':serializable_hash' do
before(:each) do
class SerializableHashExample
def serializable_hash
{ abc: 'def' }
end
class SerializableHashExample
def serializable_hash
{ abc: 'def' }
end
end

before(:each) do
subject.format :serializable_hash
end
it 'instance' do
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/validations/instance_behaivour_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:validator_type) do
Class.new(Grape::Validations::Base) do
def validate_param!(_attr_name, _params)
if @instance_variable
if instance_variable_defined?(:@instance_variable) && @instance_variable
raise Grape::Exceptions::Validation.new(params: ['params'],
message: 'This should never happen')
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def read_chunks(body)
config.include Spec::Support::Helpers
config.raise_errors_for_deprecations!
config.filter_run_when_matching :focus
config.warnings = true

config.before(:each) { Grape::Util::InheritableSetting.reset_global! }

Expand Down