Class: Faraday::CompositeReadIO
- Inherits:
-
Object
- Object
- Faraday::CompositeReadIO
- Defined in:
- lib/faraday/file_part.rb
Overview
Similar to, but not compatible with CompositeReadIO provided by the multipart-post gem. github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
Instance Method Summary collapse
-
#close ⇒ void
Close each of the IOs.
- #ensure_open_and_readable ⇒ Object
-
#initialize(*parts) ⇒ CompositeReadIO
constructor
A new instance of CompositeReadIO.
-
#length ⇒ Integer
Sum of the lengths of all the parts.
-
#read(length = nil, outbuf = nil) ⇒ Object
Read from IOs in order until ‘length` bytes have been received.
-
#rewind ⇒ void
Rewind each of the IOs and reset the index to 0.
Constructor Details
#initialize(*parts) ⇒ CompositeReadIO
Returns a new instance of CompositeReadIO.
67 68 69 70 71 |
# File 'lib/faraday/file_part.rb', line 67 def initialize(*parts) @parts = parts.flatten @ios = @parts.map(&:to_io) @index = 0 end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close each of the IOs.
110 111 112 |
# File 'lib/faraday/file_part.rb', line 110 def close @ios.each(&:close) end |
#ensure_open_and_readable ⇒ Object
114 115 116 |
# File 'lib/faraday/file_part.rb', line 114 def ensure_open_and_readable # Rubinius compatibility end |
#length ⇒ Integer
Returns sum of the lengths of all the parts.
74 75 76 |
# File 'lib/faraday/file_part.rb', line 74 def length @parts.inject(0) { |sum, part| sum + part.length } end |
#read(length = nil, outbuf = nil) ⇒ Object
Read from IOs in order until ‘length` bytes have been received.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/faraday/file_part.rb', line 90 def read(length = nil, outbuf = nil) got_result = false outbuf = outbuf ? (+outbuf).replace('') : +'' while (io = current_io) if (result = io.read(length)) got_result ||= !result.nil? result.force_encoding('BINARY') if result.respond_to?(:force_encoding) outbuf << result length -= result.length if length break if length&.zero? end advance_io end !got_result && length ? nil : outbuf end |
#rewind ⇒ void
This method returns an undefined value.
Rewind each of the IOs and reset the index to 0.
81 82 83 84 |
# File 'lib/faraday/file_part.rb', line 81 def rewind @ios.each(&:rewind) @index = 0 end |