Skip to content

Commit 75e8c5b

Browse files
authored
Remove file method (#2500)
* Remove file method and specs A * Add CHANGELOG.md and UPGRADING.md * Remove leaky dummy class in inside_route_spec.rb * Use match operator instead of multiple examples * Use match operator instead of multiple examples * Fix rubocop
1 parent 4a8b8c4 commit 75e8c5b

File tree

4 files changed

+33
-139
lines changed

4 files changed

+33
-139
lines changed

CHANGELOG.md

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

55
* [#2497](https://github.com/ruby-grape/grape/pull/2497): Update RuboCop to 1.66.1 - [@ericproulx](https://github.com/ericproulx).
6+
* [#2500](https://github.com/ruby-grape/grape/pull/2500): Remove deprecated `file` method - [@ericproulx](https://github.com/ericproulx).
67
* Your contribution here.
78

89
#### Fixes

UPGRADING.md

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

4+
### Upgrading to >= 2.3.0
5+
6+
#### Remove deprecated methods
7+
8+
Deprecated `file` method has been removed. Use `send_file` or `stream`.
9+
10+
See [#2500](https://github.com/ruby-grape/grape/pull/2500)
11+
412
### Upgrading to >= 2.2.0
513

614
### `Length` validator

lib/grape/dsl/inside_route.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,6 @@ def return_no_content
305305
body false
306306
end
307307

308-
# Deprecated method to send files to the client. Use `sendfile` or `stream`
309-
def file(value = nil)
310-
if value.is_a?(String)
311-
Grape.deprecator.warn('Use sendfile or stream to send files.')
312-
sendfile(value)
313-
elsif !value.is_a?(NilClass)
314-
Grape.deprecator.warn('Use stream to use a Stream object.')
315-
stream(value)
316-
else
317-
Grape.deprecator.warn('Use sendfile or stream to send files.')
318-
sendfile
319-
end
320-
end
321-
322308
# Allows you to send a file to the client via sendfile.
323309
#
324310
# @example

spec/grape/dsl/inside_route_spec.rb

Lines changed: 24 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
# frozen_string_literal: true
22

3-
module Grape
4-
module DSL
5-
module InsideRouteSpec
6-
class Dummy
7-
include Grape::DSL::InsideRoute
8-
9-
attr_reader :env, :request, :new_settings
10-
11-
def initialize
12-
@env = {}
13-
@header = {}
14-
@new_settings = { namespace_inheritable: {}, namespace_stackable: {} }
15-
end
3+
describe Grape::Endpoint do
4+
subject { dummy_class.new }
5+
6+
let(:dummy_class) do
7+
Class.new do
8+
include Grape::DSL::InsideRoute
9+
10+
attr_reader :env, :request, :new_settings
11+
12+
def initialize
13+
@env = {}
14+
@header = {}
15+
@new_settings = { namespace_inheritable: {}, namespace_stackable: {} }
1616
end
1717
end
1818
end
19-
end
20-
21-
describe Grape::Endpoint do
22-
subject { Grape::DSL::InsideRouteSpec::Dummy.new }
2319

2420
describe '#version' do
2521
it 'defaults to nil' do
@@ -202,38 +198,6 @@ def initialize
202198
end
203199
end
204200

205-
describe '#file' do
206-
describe 'set' do
207-
context 'as file path' do
208-
let(:file_path) { '/some/file/path' }
209-
210-
it 'emits a warning that this method is deprecated' do
211-
expect(Grape.deprecator).to receive(:warn).with(/Use sendfile or stream/)
212-
expect(subject).to receive(:sendfile).with(file_path)
213-
subject.file file_path
214-
end
215-
end
216-
217-
context 'as object (backward compatibility)' do
218-
let(:file_object) { double('StreamerObject', each: nil) }
219-
220-
it 'emits a warning that this method is deprecated' do
221-
expect(Grape.deprecator).to receive(:warn).with(/Use stream to use a Stream object/)
222-
expect(subject).to receive(:stream).with(file_object)
223-
subject.file file_object
224-
end
225-
end
226-
end
227-
228-
describe 'get' do
229-
it 'emits a warning that this method is deprecated' do
230-
expect(Grape.deprecator).to receive(:warn).with(/Use sendfile or stream/)
231-
expect(subject).to receive(:sendfile)
232-
subject.file
233-
end
234-
end
235-
end
236-
237201
describe '#sendfile' do
238202
describe 'set' do
239203
context 'as file path' do
@@ -248,36 +212,19 @@ def initialize
248212
subject.header Rack::CACHE_CONTROL, 'cache'
249213
subject.header Rack::CONTENT_LENGTH, 123
250214
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
251-
end
252-
253-
it 'sends no deprecation warnings' do
254-
expect(Grape.deprecator).not_to receive(:warn)
255-
256215
subject.sendfile file_path
257216
end
258217

259218
it 'returns value wrapped in StreamResponse' do
260-
subject.sendfile file_path
261-
262219
expect(subject.sendfile).to eq file_response
263220
end
264221

265-
it 'does not change the Cache-Control header' do
266-
subject.sendfile file_path
267-
268-
expect(subject.header[Rack::CACHE_CONTROL]).to eq 'cache'
269-
end
270-
271-
it 'does not change the Content-Length header' do
272-
subject.sendfile file_path
273-
274-
expect(subject.header[Rack::CONTENT_LENGTH]).to eq 123
275-
end
276-
277-
it 'does not change the Transfer-Encoding header' do
278-
subject.sendfile file_path
279-
280-
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to eq 'base64'
222+
it 'set the correct headers' do
223+
expect(subject.header).to match(
224+
Rack::CACHE_CONTROL => 'cache',
225+
Rack::CONTENT_LENGTH => 123,
226+
Grape::Http::Headers::TRANSFER_ENCODING => 'base64'
227+
)
281228
end
282229
end
283230

@@ -309,42 +256,15 @@ def initialize
309256
subject.header Rack::CACHE_CONTROL, 'cache'
310257
subject.header Rack::CONTENT_LENGTH, 123
311258
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
312-
end
313-
314-
it 'emits no deprecation warnings' do
315-
expect(Grape.deprecator).not_to receive(:warn)
316-
317259
subject.stream file_path
318260
end
319261

320262
it 'returns file body wrapped in StreamResponse' do
321-
subject.stream file_path
322-
323263
expect(subject.stream).to eq file_response
324264
end
325265

326-
it 'sets Cache-Control header to no-cache' do
327-
subject.stream file_path
328-
329-
expect(subject.header[Rack::CACHE_CONTROL]).to eq 'no-cache'
330-
end
331-
332-
it 'does not change Cache-Control header' do
333-
subject.stream
334-
335-
expect(subject.header[Rack::CACHE_CONTROL]).to eq 'cache'
336-
end
337-
338-
it 'sets Content-Length header to nil' do
339-
subject.stream file_path
340-
341-
expect(subject.header[Rack::CONTENT_LENGTH]).to be_nil
342-
end
343-
344-
it 'sets Transfer-Encoding header to nil' do
345-
subject.stream file_path
346-
347-
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to be_nil
266+
it 'sets only the cache-control header' do
267+
expect(subject.header).to match(Rack::CACHE_CONTROL => 'no-cache')
348268
end
349269
end
350270

@@ -359,36 +279,15 @@ def initialize
359279
subject.header Rack::CACHE_CONTROL, 'cache'
360280
subject.header Rack::CONTENT_LENGTH, 123
361281
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
362-
end
363-
364-
it 'emits no deprecation warnings' do
365-
expect(Grape.deprecator).not_to receive(:warn)
366-
367282
subject.stream stream_object
368283
end
369284

370285
it 'returns value wrapped in StreamResponse' do
371-
subject.stream stream_object
372-
373286
expect(subject.stream).to eq stream_response
374287
end
375288

376-
it 'sets Cache-Control header to no-cache' do
377-
subject.stream stream_object
378-
379-
expect(subject.header[Rack::CACHE_CONTROL]).to eq 'no-cache'
380-
end
381-
382-
it 'sets Content-Length header to nil' do
383-
subject.stream stream_object
384-
385-
expect(subject.header[Rack::CONTENT_LENGTH]).to be_nil
386-
end
387-
388-
it 'sets Transfer-Encoding header to nil' do
389-
subject.stream stream_object
390-
391-
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to be_nil
289+
it 'set only the cache-control header' do
290+
expect(subject.header).to match(Rack::CACHE_CONTROL => 'no-cache')
392291
end
393292
end
394293

@@ -403,7 +302,7 @@ def initialize
403302

404303
it 'returns default' do
405304
expect(subject.stream).to be_nil
406-
expect(subject.header[Rack::CACHE_CONTROL]).to be_nil
305+
expect(subject.header).to be_empty
407306
end
408307
end
409308

0 commit comments

Comments
 (0)