Skip to content

Commit 54c8ba2

Browse files
authored
Merge pull request #28 from janko/ruby-2-7-compatibility
Ruby 2.7 compatibility
2 parents 7850828 + 15f097e commit 54c8ba2

6 files changed

Lines changed: 40 additions & 28 deletions

File tree

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cache: bundler
66
before_install:
77
- gem update --system
88
- gem --version
9-
- gem install bundler --no-rdoc --no-ri
9+
- gem install bundler --no-document
1010
- bundle --version
1111

1212
install: bundle install --without development doc
@@ -18,14 +18,15 @@ env: JRUBY_OPTS="$JRUBY_OPTS --debug"
1818
rvm:
1919
# Include JRuby first because it takes the longest
2020
- jruby-9.1.13.0
21-
- 2.2
22-
- 2.3.4
23-
- 2.4.1
21+
- 2.4
22+
- 2.5
23+
- 2.6
24+
- 2.7
2425

2526
matrix:
2627
fast_finish: true
2728
include:
28-
- rvm: 2.4.1
29+
- rvm: 2.7
2930
env: SUITE="rubocop"
3031

3132
branches:

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ form = HTTP::FormData.create({
5858
This library aims to support and is [tested against][ci] the following Ruby
5959
versions:
6060

61-
* Ruby 2.1.x
62-
* Ruby 2.2.x
63-
* Ruby 2.3.x
6461
* Ruby 2.4.x
62+
* Ruby 2.5.x
63+
* Ruby 2.6.x
64+
* Ruby 2.7.x
6565
* JRuby 9.1.x.x
6666

6767
If something doesn't work on one of these versions, it's a bug.

http-form_data.gemspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@ Gem::Specification.new do |spec|
2222
spec.executables = spec.files.grep(%r{^bin\/}).map { |f| File.basename(f) }
2323
spec.test_files = spec.files.grep(%r{^(test|spec|features)\/})
2424
spec.require_paths = ["lib"]
25-
26-
spec.add_development_dependency "bundler", "~> 1.7"
2725
end

lib/http/form_data/composite_io.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,12 @@ def initialize(ios)
2929
#
3030
# @return [String, nil]
3131
def read(length = nil, outbuf = nil)
32-
outbuf = outbuf.to_s.clear
33-
# buffer in JRuby is sometimes US-ASCII, force to ASCII-8BIT
34-
outbuf.force_encoding(Encoding::BINARY)
32+
data = outbuf.clear.force_encoding(Encoding::BINARY) if outbuf
33+
data ||= "".b
3534

36-
while current_io
37-
current_io.read(length, @buffer)
38-
outbuf << @buffer.force_encoding(Encoding::BINARY)
39-
40-
if length
41-
length -= @buffer.bytesize
42-
break if length.zero?
43-
end
44-
45-
advance_io
46-
end
35+
read_chunks(length) { |chunk| data << chunk }
4736

48-
outbuf unless length && outbuf.empty?
37+
data unless length && data.empty?
4938
end
5039

5140
# Returns sum of all IO sizes.
@@ -61,6 +50,30 @@ def rewind
6150

6251
private
6352

53+
# Yields chunks with total length up to `length`.
54+
def read_chunks(length = nil)
55+
while (chunk = readpartial(length))
56+
yield chunk.force_encoding(Encoding::BINARY)
57+
58+
next if length.nil?
59+
60+
length -= chunk.bytesize
61+
62+
break if length.zero?
63+
end
64+
end
65+
66+
# Reads chunk from current IO with length up to `max_length`.
67+
def readpartial(max_length = nil)
68+
while current_io
69+
chunk = current_io.read(max_length, @buffer)
70+
71+
return chunk if chunk && !chunk.empty?
72+
73+
advance_io
74+
end
75+
end
76+
6477
# Returns IO object under the cursor.
6578
def current_io
6679
@ios[@index]

spec/lib/http/form_data/multipart_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def disposition(params)
5757

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

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

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

7979
it "doesn't include a filename" do
8080
boundary_value = form_data.content_type[/(#{boundary})$/, 1]

spec/lib/http/form_data/part_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe HTTP::FormData::Part do
44
let(:body) { "" }
55
let(:opts) { {} }
6-
subject(:part) { HTTP::FormData::Part.new(body, opts) }
6+
subject(:part) { HTTP::FormData::Part.new(body, **opts) }
77

88
describe "#size" do
99
subject { part.size }

0 commit comments

Comments
 (0)