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
Creates a new CompositeIO from an array of IOs.
-
#read(length = nil, outbuf = nil) ⇒ String?
Reads and returns content across multiple IO objects.
-
#rewind ⇒ void
Rewinds all IO objects and resets cursor.
-
#size ⇒ Integer
Returns sum of all IO sizes.
Constructor Details
#initialize(ios) ⇒ CompositeIO
Creates a new CompositeIO from an array of IOs
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/http/form_data/composite_io.rb', line 16 def initialize(ios) @index = 0 @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 content across multiple IO objects
40 41 42 43 44 45 46 47 |
# File 'lib/http/form_data/composite_io.rb', line 40 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 ⇒ void
This method returns an undefined value.
Rewinds all IO objects and resets cursor
67 68 69 70 |
# File 'lib/http/form_data/composite_io.rb', line 67 def rewind @ios.each(&:rewind) @index = 0 end |
#size ⇒ Integer
Returns sum of all IO sizes
56 57 58 |
# File 'lib/http/form_data/composite_io.rb', line 56 def size @size ||= @ios.sum(&:size) end |