lib/protocol/http/body/readable.rb in protocol-http-0.24.3 vs lib/protocol/http/body/readable.rb in protocol-http-0.24.4
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.
+# Copyright, 2023, by Bruno Sutic.
module Protocol
module HTTP
module Body
# A generic base class for wrapping body instances. Typically you'd override `#read`.
@@ -64,14 +65,18 @@
Buffered.for(self)
end
# Enumerate all chunks until finished, then invoke `#close`.
def each
- while chunk = self.read
- yield chunk
+ return to_enum(:each) unless block_given?
+
+ begin
+ while chunk = self.read
+ yield chunk
+ end
+ ensure
+ self.close($!)
end
- ensure
- self.close($!)
end
# Read all remaining chunks into a single binary string using `#each`.
def join
buffer = String.new.force_encoding(Encoding::BINARY)