Class: HTTP::FormData::CompositeIO
- Inherits:
-
Object
- Object
- HTTP::FormData::CompositeIO
- Defined in:
- lib/http/form_data/composite_io.rb
Overview
Provides IO interface across multiple IO objects.
Instance Method Summary collapse
-
#initialize(ios) ⇒ CompositeIO
constructor
A new instance of CompositeIO.
-
#read(length = nil, outbuf = nil) ⇒ String?
Reads and returns partial content acrosss multiple IO objects.
-
#rewind ⇒ Object
Rewinds all IO objects and set cursor to the first IO object.
-
#size ⇒ Object
Returns sum of all IO sizes.
Constructor Details
#initialize(ios) ⇒ CompositeIO
Returns a new instance of CompositeIO.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/http/form_data/composite_io.rb', line 10 def initialize(ios) @index = 0 @buffer = "".b @ios = ios.map do |io| if io.is_a?(String) StringIO.new(io) elsif io.respond_to?(:read) io else raise ArgumentError, "#{io.inspect} is neither a String nor an IO object" end end end |
Instance Method Details
#read(length = nil, outbuf = nil) ⇒ String?
Reads and returns partial content acrosss multiple IO objects.
31 32 33 34 35 36 37 38 |
# File 'lib/http/form_data/composite_io.rb', line 31 def read(length = nil, outbuf = nil) data = outbuf.clear.force_encoding(Encoding::BINARY) if outbuf data ||= "".b read_chunks(length) { |chunk| data << chunk } data unless length && data.empty? end |
#rewind ⇒ Object
Rewinds all IO objects and set cursor to the first IO object.
46 47 48 49 |
# File 'lib/http/form_data/composite_io.rb', line 46 def rewind @ios.each(&:rewind) @index = 0 end |
#size ⇒ Object
Returns sum of all IO sizes.
41 42 43 |
# File 'lib/http/form_data/composite_io.rb', line 41 def size @size ||= @ios.map(&:size).inject(0, :+) end |