Class: Faraday::Multipart::CompositeReadIO

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/multipart/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

Constructor Details

#initialize(*parts) ⇒ CompositeReadIO

Returns a new instance of CompositeReadIO.


62
63
64
65
66
# File 'lib/faraday/multipart/file_part.rb', line 62

def initialize(*parts)
  @parts = parts.flatten
  @ios = @parts.map(&:to_io)
  @index = 0
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close each of the IOs.


105
106
107
# File 'lib/faraday/multipart/file_part.rb', line 105

def close
  @ios.each(&:close)
end

#ensure_open_and_readableObject


109
110
111
# File 'lib/faraday/multipart/file_part.rb', line 109

def ensure_open_and_readable
  # Rubinius compatibility
end

#lengthInteger

Returns sum of the lengths of all the parts.

Returns:

  • (Integer)

    sum of the lengths of all the parts


69
70
71
# File 'lib/faraday/multipart/file_part.rb', line 69

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.

Parameters:

  • length (Integer, nil) (defaults to: nil)
  • outbuf (String, nil) (defaults to: nil)

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/faraday/multipart/file_part.rb', line 85

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

#rewindvoid

This method returns an undefined value.

Rewind each of the IOs and reset the index to 0.


76
77
78
79
# File 'lib/faraday/multipart/file_part.rb', line 76

def rewind
  @ios.each(&:rewind)
  @index = 0
end