Skip to content

Commit 5f9c3d4

Browse files
authored
Use double splat to pass opts to File.open (#27)
1 parent 70dace5 commit 5f9c3d4

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/doc/
66
/pkg/
77
/spec/reports/
8+
/spec/protocol/http/body/reader_spec.txt
89
/tmp/
910
/external/
1011

lib/protocol/http/body/reader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def finish
5757
end
5858

5959
# Write the body of the response to the given file path.
60-
def save(path, mode = ::File::WRONLY|::File::CREAT, *args)
60+
def save(path, mode = ::File::WRONLY|::File::CREAT, **options)
6161
if @body
62-
::File.open(path, mode, *args) do |file|
62+
::File.open(path, mode, **options) do |file|
6363
self.each do |chunk|
6464
file.write(chunk)
6565
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'protocol/http/body/reader'
2+
3+
RSpec.describe Protocol::HTTP::Body::Reader do
4+
class TestReader
5+
include Protocol::HTTP::Body::Reader
6+
7+
def initialize(body)
8+
@body = body
9+
end
10+
end
11+
12+
subject do
13+
TestReader.new(%w(the quick brown fox))
14+
end
15+
16+
describe '#save' do
17+
let(:path) { File.expand_path('reader_spec.txt', __dir__) }
18+
19+
before do
20+
File.open(path, 'w') {}
21+
end
22+
23+
it 'saves to the provided filename' do
24+
expect { subject.save(path) }
25+
.to change { File.read(path) }
26+
.from('')
27+
.to('thequickbrownfox')
28+
end
29+
30+
it 'mirrors the interface of File.open' do
31+
expect { subject.save(path, nil, mode: 'w') }
32+
.to change { File.read(path) }
33+
.from('')
34+
.to('thequickbrownfox')
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)