Skip to content

Commit 0214a9a

Browse files
committed
Fix RuboCop offenses
1 parent c06b69c commit 0214a9a

12 files changed

Lines changed: 119 additions & 54 deletions

File tree

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
inherit_from:
22
- .rubocop/layout.yml
33
- .rubocop/metrics.yml
4+
- .rubocop/rspec.yml
45
- .rubocop/style.yml
56

67
plugins:

.rubocop/rspec.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RSpec/ExampleLength:
2+
Max: 25
3+
4+
RSpec/MultipleExpectations:
5+
Max: 5
6+
7+
RSpec/MultipleMemoizedHelpers:
8+
Max: 6
9+
10+
RSpec/NestedGroups:
11+
Max: 4

http-form_data.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
1919
DESC
2020

2121
spec.metadata["changelog_uri"] = "https://github.com/httprb/form_data/blob/master/CHANGES.md"
22+
spec.metadata["rubygems_mfa_required"] = "true"
2223

2324
spec.files = `git ls-files -z`.split("\x0")
2425
spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) }
25-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
2626
spec.require_paths = ["lib"]
2727

2828
spec.required_ruby_version = ">= 2.5"

lib/http/form_data.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def multipart?(data)
8686
data.any? do |_, v|
8787
next true if v.is_a? FormData::Part
8888

89-
v.respond_to?(:to_ary) && v.to_ary.any? { |e| e.is_a? FormData::Part }
89+
v.respond_to?(:to_ary) && v.to_ary.any?(FormData::Part)
9090
end
9191
end
9292
end

lib/http/form_data/urlencoded.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,26 @@ module DefaultEncoder
6767
class << self
6868
def encode(value, prefix = nil)
6969
case value
70-
when Hash
71-
encode_hash(value, prefix)
72-
when Array
73-
if prefix
74-
value.map { |v| encode(v, "#{prefix}[]") }.join("&")
75-
else
76-
encode_pairs(value)
77-
end
78-
when nil then prefix.to_s
70+
when Hash then encode_hash(value, prefix)
71+
when Array then encode_array(value, prefix)
72+
when nil then prefix.to_s
7973
else
8074
raise ArgumentError, "value must be a Hash" if prefix.nil?
75+
8176
"#{prefix}=#{escape(value)}"
8277
end
8378
end
8479

8580
private
8681

82+
def encode_array(value, prefix)
83+
if prefix
84+
value.map { |v| encode(v, "#{prefix}[]") }.join("&")
85+
else
86+
encode_pairs(value)
87+
end
88+
end
89+
8790
def encode_pairs(pairs)
8891
pairs.map { |k, v| encode(v, escape(k)) }.reject(&:empty?).join("&")
8992
end

spec/lib/http/form_data/composite_io_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# frozen_string_literal: true
22

33
RSpec.describe HTTP::FormData::CompositeIO do
4-
subject(:composite_io) { HTTP::FormData::CompositeIO.new(ios) }
4+
subject(:composite_io) { described_class.new(ios) }
55

66
let(:ios) { ["Hello", " ", "", "world", "!"].map { |s| StringIO.new(s) } }
77

88
describe "#initialize" do
99
it "accepts IOs and strings" do
10-
io = HTTP::FormData::CompositeIO.new(["Hello ", StringIO.new("world!")])
10+
io = described_class.new(["Hello ", StringIO.new("world!")])
1111
expect(io.read).to eq "Hello world!"
1212
end
1313

1414
it "fails if an IO is neither a String nor an IO" do
15-
expect { HTTP::FormData::CompositeIO.new %i[hello world] }
15+
expect { described_class.new %i[hello world] }
1616
.to raise_error(ArgumentError)
1717
end
1818
end
@@ -36,7 +36,7 @@
3636

3737
it "returns nil when no partial data was retrieved" do
3838
composite_io.read
39-
expect(composite_io.read(3)).to eq nil
39+
expect(composite_io.read(3)).to be_nil
4040
end
4141

4242
it "reads partial data with a buffer" do
@@ -62,12 +62,12 @@
6262
it "returns nil when no partial data was retrieved with a buffer" do
6363
outbuf = String.new("content")
6464
composite_io.read
65-
expect(composite_io.read(3, outbuf)).to eq nil
65+
expect(composite_io.read(3, outbuf)).to be_nil
6666
expect(outbuf).to eq ""
6767
end
6868

6969
it "returns data in binary encoding" do
70-
io = HTTP::FormData::CompositeIO.new(%w[Janko Marohnić])
70+
io = described_class.new(%w[Janko Marohnić])
7171

7272
expect(io.read(5).encoding).to eq Encoding::BINARY
7373
expect(io.read(9).encoding).to eq Encoding::BINARY
@@ -79,7 +79,7 @@
7979

8080
it "reads data in bytes" do
8181
emoji = "😃"
82-
io = HTTP::FormData::CompositeIO.new([emoji])
82+
io = described_class.new([emoji])
8383

8484
expect(io.read(1)).to eq emoji.b[0]
8585
expect(io.read(1)).to eq emoji.b[1]
@@ -102,7 +102,7 @@
102102
end
103103

104104
it "returns 0 when there are no IOs" do
105-
empty_composite_io = HTTP::FormData::CompositeIO.new []
105+
empty_composite_io = described_class.new []
106106
expect(empty_composite_io.size).to eq 0
107107
end
108108
end

spec/lib/http/form_data/file_spec.rb

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99

1010
context "when file given as a String" do
1111
let(:file) { fixture("the-http-gem.info").to_s }
12+
1213
it { is_expected.to eq fixture("the-http-gem.info").size }
1314
end
1415

1516
context "when file given as a Pathname" do
1617
let(:file) { fixture("the-http-gem.info") }
18+
1719
it { is_expected.to eq fixture("the-http-gem.info").size }
1820
end
1921

2022
context "when file given as File" do
2123
let(:file) { fixture("the-http-gem.info").open }
24+
2225
after { file.close }
26+
2327
it { is_expected.to eq fixture("the-http-gem.info").size }
2428
end
2529

2630
context "when file given as IO" do
2731
let(:file) { StringIO.new "привет мир!" }
32+
2833
it { is_expected.to eq 20 }
2934
end
3035
end
@@ -34,6 +39,7 @@
3439

3540
context "when file given as a String" do
3641
let(:file) { fixture("the-http-gem.info").to_s }
42+
3743
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
3844

3945
it "rewinds content" do
@@ -45,6 +51,7 @@
4551

4652
context "when file given as a Pathname" do
4753
let(:file) { fixture("the-http-gem.info") }
54+
4855
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
4956

5057
it "rewinds content" do
@@ -56,7 +63,9 @@
5663

5764
context "when file given as File" do
5865
let(:file) { fixture("the-http-gem.info").open("rb") }
66+
5967
after { file.close }
68+
6069
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
6170

6271
it "rewinds content" do
@@ -68,6 +77,7 @@
6877

6978
context "when file given as IO" do
7079
let(:file) { StringIO.new "привет мир!" }
80+
7181
it { is_expected.to eq "привет мир!" }
7282

7383
it "rewinds content" do
@@ -83,22 +93,27 @@
8393

8494
context "when file given as a String" do
8595
let(:file) { fixture("the-http-gem.info").to_s }
96+
8697
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
8798
end
8899

89100
context "when file given as a Pathname" do
90101
let(:file) { fixture("the-http-gem.info") }
102+
91103
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
92104
end
93105

94106
context "when file given as File" do
95107
let(:file) { fixture("the-http-gem.info").open("rb") }
108+
96109
after { file.close }
110+
97111
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
98112
end
99113

100114
context "when file given as IO" do
101115
let(:file) { StringIO.new "привет мир!" }
116+
102117
it { is_expected.to eq "привет мир!" }
103118
end
104119
end
@@ -126,6 +141,7 @@
126141

127142
context "when file given as File" do
128143
let(:file) { fixture("the-http-gem.info").open("rb") }
144+
129145
after { file.close }
130146

131147
it "rewinds the underlying IO object" do
@@ -152,33 +168,37 @@
152168
context "when file given as a String" do
153169
let(:file) { fixture("the-http-gem.info").to_s }
154170

155-
it { is_expected.to eq ::File.basename file }
171+
it { is_expected.to eq File.basename file }
156172

157-
context "and filename given with options" do
173+
context "with filename given in options" do
158174
let(:opts) { { :filename => "foobar.txt" } }
175+
159176
it { is_expected.to eq "foobar.txt" }
160177
end
161178
end
162179

163180
context "when file given as a Pathname" do
164181
let(:file) { fixture("the-http-gem.info") }
165182

166-
it { is_expected.to eq ::File.basename file }
183+
it { is_expected.to eq File.basename file }
167184

168-
context "and filename given with options" do
185+
context "with filename given in options" do
169186
let(:opts) { { :filename => "foobar.txt" } }
187+
170188
it { is_expected.to eq "foobar.txt" }
171189
end
172190
end
173191

174192
context "when file given as File" do
175193
let(:file) { fixture("the-http-gem.info").open }
194+
176195
after { file.close }
177196

178197
it { is_expected.to eq "the-http-gem.info" }
179198

180-
context "and filename given with options" do
199+
context "with filename given in options" do
181200
let(:opts) { { :filename => "foobar.txt" } }
201+
182202
it { is_expected.to eq "foobar.txt" }
183203
end
184204
end
@@ -188,27 +208,30 @@
188208

189209
it { is_expected.to eq "stream-#{file.object_id}" }
190210

191-
context "and filename given with options" do
211+
context "with filename given in options" do
192212
let(:opts) { { :filename => "foobar.txt" } }
213+
193214
it { is_expected.to eq "foobar.txt" }
194215
end
195216
end
196217
end
197218

198219
describe "#content_type" do
199-
let(:file) { StringIO.new }
200220
subject { form_file.content_type }
201221

222+
let(:file) { StringIO.new }
223+
202224
it { is_expected.to eq "application/octet-stream" }
203225

204226
context "when it was given with options" do
205227
let(:opts) { { :content_type => "application/json" } }
228+
206229
it { is_expected.to eq "application/json" }
207230
end
208231
end
209232

210233
describe "#mime_type" do
211-
it "should be an alias of #content_type" do
234+
it "is an alias of #content_type" do
212235
expect(described_class.instance_method(:mime_type))
213236
.to eq(described_class.instance_method(:content_type))
214237
end

spec/lib/http/form_data/multipart_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def disposition(params)
3737

3838
context "with user-defined boundary" do
3939
subject(:form_data) do
40-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
40+
described_class.new params, :boundary => "my-boundary"
4141
end
4242

4343
it "uses the given boundary" do
@@ -56,7 +56,7 @@ def disposition(params)
5656

5757
context "with filename set to nil" do
5858
let(:part) { HTTP::FormData::Part.new("s", :content_type => "mime/type") }
59-
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
59+
let(:form_data) { described_class.new({ :foo => part }) }
6060

6161
it "doesn't include a filename" do
6262
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
@@ -73,7 +73,7 @@ def disposition(params)
7373

7474
context "with content type set to nil" do
7575
let(:part) { HTTP::FormData::Part.new("s") }
76-
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
76+
let(:form_data) { described_class.new({ :foo => part }) }
7777

7878
it "doesn't include a filename" do
7979
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
@@ -88,7 +88,7 @@ def disposition(params)
8888
end
8989

9090
it "supports any Enumerable of pairs" do
91-
enum = Enumerator.new { |y| y << [:foo, :bar] << [:foo, :baz] }
91+
enum = Enumerator.new { |y| y << %i[foo bar] << %i[foo baz] }
9292
form_data = described_class.new(enum)
9393

9494
boundary_value = form_data.boundary
@@ -172,7 +172,7 @@ def disposition(params)
172172

173173
context "with user-defined boundary" do
174174
let(:form_data) do
175-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
175+
described_class.new params, :boundary => "my-boundary"
176176
end
177177

178178
it "includes the given boundary" do
@@ -184,6 +184,7 @@ def disposition(params)
184184

185185
describe "#content_length" do
186186
subject { form_data.content_length }
187+
187188
it { is_expected.to eq form_data.to_s.bytesize }
188189
end
189190

@@ -194,7 +195,7 @@ def disposition(params)
194195

195196
context "with user-defined boundary" do
196197
let(:form_data) do
197-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
198+
described_class.new params, :boundary => "my-boundary"
198199
end
199200

200201
it "returns the given boundary" do

0 commit comments

Comments
 (0)