Skip to content

Commit dc5e4b0

Browse files
authored
Merge pull request #1526 from cpetschnig/avoid_ruby_warnings
Avoid noise when code runs with Ruby warnings
2 parents b511add + dba6064 commit dc5e4b0

File tree

8 files changed

+64
-44
lines changed

8 files changed

+64
-44
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Next Release
1313
* [#1517](https://github.com/ruby-grape/grape/pull/1517), [#1089](https://github.com/ruby-grape/grape/pull/1089): Fix: priority of ANY routes - [@namusyaka](https://github.com/namusyaka), [@wagenet](https://github.com/wagenet).
1414
* [#1512](https://github.com/ruby-grape/grape/pull/1512): Fix: deeply nested parameters are included within `#declared(params)` - [@krbs](https://github.com/krbs).
1515
* [#1510](https://github.com/ruby-grape/grape/pull/1510): Fix: inconsistent validation for multiple parameters - [@dgasper](https://github.com/dgasper).
16+
* [#1526](https://github.com/ruby-grape/grape/pull/1526): Reduce warnings caused by instance variables not initialized - [@cpetschnig](https://github.com/cpetschnig).
1617

1718
0.18.0 (10/7/2016)
1819
==================

lib/grape/endpoint.rb

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def initialize(new_settings, options = {}, &block)
9595
@options[:route_options] ||= {}
9696

9797
@lazy_initialize_lock = Mutex.new
98+
@lazy_initialized = nil
99+
@block = nil
98100

99101
return unless block_given?
100102

lib/grape/middleware/base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Base
1313
def initialize(app, **options)
1414
@app = app
1515
@options = default_options.merge(**options)
16+
@app_response = nil
1617
end
1718

1819
def default_options

lib/grape/validations/params_scope.rb

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def initialize(opts, &block)
2929
@group = opts[:group] || {}
3030
@dependent_on = opts[:dependent_on]
3131
@declared_params = []
32+
@index = nil
3233

3334
instance_eval(&block) if block_given?
3435

spec/grape/api_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ class PhonyMiddleware
12021202
def initialize(app, *args)
12031203
@args = args
12041204
@app = app
1205-
@block = true if block_given?
1205+
@block = block_given? ? true : nil
12061206
end
12071207

12081208
def call(env)

spec/grape/middleware/formatter_spec.rb

+39-30
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,53 @@
44
subject { Grape::Middleware::Formatter.new(app) }
55
before { allow(subject).to receive(:dup).and_return(subject) }
66

7-
let(:app) { ->(_env) { [200, {}, [@body || { 'foo' => 'bar' }]] } }
7+
let(:body) { { 'foo' => 'bar' } }
8+
let(:app) { ->(_env) { [200, {}, [body]] } }
89

910
context 'serialization' do
11+
let(:body) { { 'abc' => 'def' } }
1012
it 'looks at the bodies for possibly serializable data' do
11-
@body = { 'abc' => 'def' }
1213
_, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
13-
bodies.each { |b| expect(b).to eq(MultiJson.dump(@body)) }
14+
bodies.each { |b| expect(b).to eq(MultiJson.dump(body)) }
1415
end
1516

16-
it 'calls #to_json since default format is json' do
17-
@body = ['foo']
18-
@body.instance_eval do
19-
def to_json
20-
'"bar"'
17+
context 'default format' do
18+
let(:body) { ['foo'] }
19+
it 'calls #to_json since default format is json' do
20+
body.instance_eval do
21+
def to_json
22+
'"bar"'
23+
end
2124
end
22-
end
2325

24-
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') }
26+
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') }
27+
end
2528
end
2629

27-
it 'calls #to_json if the content type is jsonapi' do
28-
@body = { 'foos' => [{ 'bar' => 'baz' }] }
29-
@body.instance_eval do
30-
def to_json
31-
'{"foos":[{"bar":"baz"}] }'
30+
context 'jsonapi' do
31+
let(:body) { { 'foos' => [{ 'bar' => 'baz' }] } }
32+
it 'calls #to_json if the content type is jsonapi' do
33+
body.instance_eval do
34+
def to_json
35+
'{"foos":[{"bar":"baz"}] }'
36+
end
3237
end
33-
end
3438

35-
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') }
39+
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') }
40+
end
3641
end
3742

38-
it 'calls #to_xml if the content type is xml' do
39-
@body = 'string'
40-
@body.instance_eval do
41-
def to_xml
42-
'<bar/>'
43+
context 'xml' do
44+
let(:body) { 'string' }
45+
it 'calls #to_xml if the content type is xml' do
46+
body.instance_eval do
47+
def to_xml
48+
'<bar/>'
49+
end
4350
end
44-
end
4551

46-
subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') }
52+
subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') }
53+
end
4754
end
4855
end
4956

@@ -189,10 +196,12 @@ def to_xml
189196
_, _, body = subject.call('PATH_INFO' => '/info.custom')
190197
expect(body.body).to eq(['CUSTOM FORMAT'])
191198
end
192-
it 'uses default json formatter' do
193-
@body = ['blah']
194-
_, _, body = subject.call('PATH_INFO' => '/info.json')
195-
expect(body.body).to eq(['["blah"]'])
199+
context 'default' do
200+
let(:body) { ['blah'] }
201+
it 'uses default json formatter' do
202+
_, _, body = subject.call('PATH_INFO' => '/info.json')
203+
expect(body.body).to eq(['["blah"]'])
204+
end
196205
end
197206
it 'uses custom json formatter' do
198207
subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
@@ -284,10 +293,10 @@ def to_xml
284293
end
285294

286295
context 'send file' do
287-
let(:app) { ->(_env) { [200, {}, @body] } }
296+
let(:body) { Grape::ServeFile::FileResponse.new('file') }
297+
let(:app) { ->(_env) { [200, {}, body] } }
288298

289299
it 'returns Grape::Uril::SendFileReponse' do
290-
@body = Grape::ServeFile::FileResponse.new('file')
291300
env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
292301
expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
293302
end

spec/grape/middleware/versioner/param_spec.rb

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
describe Grape::Middleware::Versioner::Param do
44
let(:app) { ->(env) { [200, env, env['api.version']] } }
5-
subject { Grape::Middleware::Versioner::Param.new(app, @options || {}) }
5+
let(:options) { {} }
6+
subject { Grape::Middleware::Versioner::Param.new(app, options) }
67

78
it 'sets the API version based on the default param (apiver)' do
89
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
@@ -22,7 +23,7 @@
2223
end
2324

2425
context 'with specified parameter name' do
25-
before { @options = { version_options: { parameter: 'v' } } }
26+
let(:options) { { version_options: { parameter: 'v' } } }
2627
it 'sets the API version based on the custom parameter name' do
2728
env = Rack::MockRequest.env_for('/awesome', params: { 'v' => 'v1' })
2829
expect(subject.call(env)[1]['api.version']).to eq('v1')
@@ -34,7 +35,7 @@
3435
end
3536

3637
context 'with specified versions' do
37-
before { @options = { versions: %w(v1 v2) } }
38+
let(:options) { { versions: %w(v1 v2) } }
3839
it 'throws an error if a non-allowed version is specified' do
3940
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v3' })
4041
expect(catch(:error) { subject.call(env) }[:status]).to eq(404)
@@ -45,13 +46,17 @@
4546
end
4647
end
4748

48-
it 'returns a 200 when no version is set (matches the first version found)' do
49-
@options = {
50-
versions: ['v1'],
51-
version_options: { using: :header }
52-
}
53-
env = Rack::MockRequest.env_for('/awesome', params: {})
54-
expect(subject.call(env).first).to eq(200)
49+
context 'when no version is set' do
50+
let(:options) do
51+
{
52+
versions: ['v1'],
53+
version_options: { using: :header }
54+
}
55+
end
56+
it 'returns a 200 (matches the first version found)' do
57+
env = Rack::MockRequest.env_for('/awesome', params: {})
58+
expect(subject.call(env).first).to eq(200)
59+
end
5560
end
5661

5762
context 'when there are multiple versions without a custom param' do

spec/grape/middleware/versioner/path_spec.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
describe Grape::Middleware::Versioner::Path do
44
let(:app) { ->(env) { [200, env, env['api.version']] } }
5-
subject { Grape::Middleware::Versioner::Path.new(app, @options || {}) }
5+
let(:options) { {} }
6+
subject { Grape::Middleware::Versioner::Path.new(app, options) }
67

78
it 'sets the API version based on the first path' do
89
expect(subject.call('PATH_INFO' => '/v1/awesome').last).to eq('v1')
@@ -17,7 +18,7 @@
1718
end
1819

1920
context 'with a pattern' do
20-
before { @options = { pattern: /v./i } }
21+
let(:options) { { pattern: /v./i } }
2122
it 'sets the version if it matches' do
2223
expect(subject.call('PATH_INFO' => '/v1/awesome').last).to eq('v1')
2324
end
@@ -29,7 +30,7 @@
2930

3031
[%w(v1 v2), [:v1, :v2], [:v1, 'v2'], ['v1', :v2]].each do |versions|
3132
context "with specified versions as #{versions}" do
32-
before { @options = { versions: versions } }
33+
let(:options) { { versions: versions } }
3334

3435
it 'throws an error if a non-allowed version is specified' do
3536
expect(catch(:error) { subject.call('PATH_INFO' => '/v3/awesome') }[:status]).to eq(404)

0 commit comments

Comments
 (0)