Class: HTTParty::Request::StreamingMultipartBody

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/request/streaming_multipart_body.rb

Constant Summary collapse

NEWLINE =
"\r\n"
CHUNK_SIZE =

64 KB chunks

64 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(parts, boundary) ⇒ StreamingMultipartBody

Returns a new instance of StreamingMultipartBody.



9
10
11
12
13
14
15
16
17
18
# File 'lib/httparty/request/streaming_multipart_body.rb', line 9

def initialize(parts, boundary)
  @parts = parts
  @boundary = boundary
  @part_index = 0
  @state = :header
  @current_file = nil
  @header_buffer = nil
  @header_offset = 0
  @footer_sent = false
end

Instance Method Details

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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/httparty/request/streaming_multipart_body.rb', line 24

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

  return read_all(outbuf) if length.nil?

  while outbuf.bytesize < length
    chunk = read_chunk(length - outbuf.bytesize)
    break if chunk.nil?
    outbuf << chunk
  end

  outbuf.empty? ? nil : outbuf
end

#rewindObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/httparty/request/streaming_multipart_body.rb', line 38

def rewind
  @part_index = 0
  @state = :header
  @current_file = nil
  @header_buffer = nil
  @header_offset = 0
  @footer_sent = false
  @parts.each do |_key, value, _is_file|
    value.rewind if value.respond_to?(:rewind)
  end
end

#sizeObject



20
21
22
# File 'lib/httparty/request/streaming_multipart_body.rb', line 20

def size
  @size ||= calculate_size
end