diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1d7f66c28..67503bab1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-02-19 15:40:46 -0500 using RuboCop version 0.47.1. +# on 2017-02-27 18:05:03 +0000 using RuboCop version 0.47.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -25,11 +25,11 @@ Metrics/BlockNesting: Metrics/ClassLength: Max: 281 -# Offense count: 26 +# Offense count: 28 Metrics/CyclomaticComplexity: Max: 14 -# Offense count: 993 +# Offense count: 994 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: @@ -45,9 +45,9 @@ Metrics/MethodLength: Metrics/ModuleLength: Max: 212 -# Offense count: 16 +# Offense count: 19 Metrics/PerceivedComplexity: - Max: 14 + Max: 16 # Offense count: 2 Style/IdenticalConditionalBranches: diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1576850..b9ada4619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#1561](https://github.com/ruby-grape/grape/pull/1561): Fix performance issue introduced by duplicated calls in StackableValue#[] - [@brucehsu](https://github.com/brucehsu). * [#1564](https://github.com/ruby-grape/grape/pull/1564): Fix declared params bug with nested namespaces - [@bmarini](https://github.com/bmarini). * [#1567](https://github.com/ruby-grape/grape/pull/1567): Fix values validator when value is empty array and apply except to input array - [@jlfaber](https://github.com/jlfaber). +* [#1583](https://github.com/ruby-grape/grape/pull/1583): Fix spec warnings - [@axfcampos](https://github.com/axfcampos). * Your contribution here. ### 0.19.1 (1/9/2017) diff --git a/lib/grape/dsl/helpers.rb b/lib/grape/dsl/helpers.rb index 9d2200a7c..c28b89925 100644 --- a/lib/grape/dsl/helpers.rb +++ b/lib/grape/dsl/helpers.rb @@ -78,7 +78,7 @@ def api_changed(new_api) protected def process_named_params - return unless @named_params && @named_params.any? + return unless defined?(@named_params) && @named_params && @named_params.any? api.namespace_stackable(:named_params, @named_params) end end diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 0e771504a..681f6d127 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -144,12 +144,12 @@ def status(status = nil) when Integer @status = status when nil - return @status if @status + return @status if 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 defined?(@body) && @body.present? 200 else 204 @@ -199,7 +199,7 @@ def body(value = nil) elsif value == false @body = '' status 204 - else + elsif defined?(@body) @body end end @@ -233,7 +233,7 @@ def file(value = nil) elsif !value.is_a?(NilClass) warn '[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.' @file = Grape::ServeFile::FileResponse.new(value) - else + elsif defined?(@file) @file end end @@ -294,8 +294,8 @@ def present(*args) representation = { root => representation } if root if key - representation = (@body || {}).merge(key => representation) - elsif entity_class.present? && @body + representation = ((defined?(@body) && @body) || {}).merge(key => representation) + elsif entity_class.present? && defined?(@body) && @body raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge) representation = @body.merge(representation) end diff --git a/lib/grape/dsl/parameters.rb b/lib/grape/dsl/parameters.rb index 7d3986ac0..4fd2e7afc 100644 --- a/lib/grape/dsl/parameters.rb +++ b/lib/grape/dsl/parameters.rb @@ -99,7 +99,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 defined?(@group) && @group if opts[:using] require_required_and_optional_fields(attrs.first, opts) @@ -119,7 +119,7 @@ def optional(*attrs, &block) opts = attrs.extract_options!.clone type = opts[:type] - opts = @group.merge(opts) if @group + opts = @group.merge(opts) if defined?(@group) && @group # check type for optional parameter group if attrs && block_given? @@ -209,8 +209,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 defined?(@parent) && @parent + params = map_params(params, @element) if defined?(@element) && @element params end end diff --git a/lib/grape/dsl/routing.rb b/lib/grape/dsl/routing.rb index 24b9d5f3a..369597628 100644 --- a/lib/grape/dsl/routing.rb +++ b/lib/grape/dsl/routing.rb @@ -49,7 +49,7 @@ def version(*args, &block) end end - @versions.last unless @versions.nil? + @versions.last if defined?(@versions) && @versions end # Define a root URL prefix for your entire API. @@ -159,8 +159,8 @@ def route(methods, paths = ['/'], route_options = {}, &block) def namespace(space = nil, options = {}, &block) if space || block_given? within_namespace do - previous_namespace_description = @namespace_description - @namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {}) + previous_namespace_description = defined?(@namespace_description) && @namespace_description + @namespace_description = ((defined?(@namespace_description) && @namespace_description) || {}).deep_merge(namespace_setting(:description) || {}) nest(block) do if space namespace_stackable(:namespace, Namespace.new(space, options)) diff --git a/lib/grape/validations/validators/values.rb b/lib/grape/validations/validators/values.rb index 1926bc902..e4269c3fd 100644 --- a/lib/grape/validations/validators/values.rb +++ b/lib/grape/validations/validators/values.rb @@ -9,6 +9,7 @@ def initialize(attrs, options, required, scope, opts = {}) raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc) else @values = options + @excepts = @proc = nil end super end diff --git a/spec/grape/dsl/parameters_spec.rb b/spec/grape/dsl/parameters_spec.rb index a0fae5f66..ee45c1868 100644 --- a/spec/grape/dsl/parameters_spec.rb +++ b/spec/grape/dsl/parameters_spec.rb @@ -50,19 +50,21 @@ def extract_message_option(attrs) describe '#use' do before do allow_message_expectations_on_nil - allow(subject.api).to receive(:namespace_stackable).with(:named_params) + subject.api = api + allow(api).to receive(:namespace_stackable).with(:named_params) end + let(:api) { double('api') } let(:options) { { option: 'value' } } let(:named_params) { { params_group: proc {} } } it 'calls processes associated with named params' do - allow(subject.api).to receive(:namespace_stackable_with_hash).and_return(named_params) + allow(api).to receive(:namespace_stackable_with_hash).and_return(named_params) expect(subject).to receive(:instance_exec).with(options).and_yield subject.use :params_group, options end it 'raises error when non-existent named param is called' do - allow(subject.api).to receive(:namespace_stackable_with_hash).and_return({}) + allow(api).to receive(:namespace_stackable_with_hash).and_return({}) expect { subject.use :params_group }.to raise_error('Params :params_group not found!') end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e00fba228..cdd0b742c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,6 +20,8 @@ config.raise_errors_for_deprecations! config.before(:each) { Grape::Util::InheritableSetting.reset_global! } + + config.warnings = true end require 'coveralls'