Class: FatZebra::Request::Multipart::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_zebra/request/multipart/stream.rb

Constant Summary collapse

BINARY_ENCODING =
'BINARY'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(*ios) ⇒ Stream

Returns a new instance of Stream.



13
14
15
16
# File 'lib/fat_zebra/request/multipart/stream.rb', line 13

def initialize(*ios)
  @ios   = ios.flatten
  @index = 0
end

Instance Method Details

#read(length = nil, outbuf = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fat_zebra/request/multipart/stream.rb', line 20

def read(length = nil, outbuf = nil)
  got_result = false
  outbuf = outbuf ? outbuf.replace('') : ''

  while current_io
    result = current_io.read(length)

    if result
      got_result ||= !result.nil?
      result.force_encoding(BINARY_ENCODING) if result.respond_to?(:force_encoding)
      outbuf << result

      length -= result.length if length
      break if length == 0
    end

    next_io
  end

  !got_result && length ? nil : outbuf
end