Method: Mechanize::HTTP::Agent#auto_io

Defined in:
lib/mechanize/http/agent.rb

#auto_io(name, read_size, input_io) ⇒ Object

Creates a new output IO by reading input_io in read_size chunks. If the output is over the max_file_buffer size a Tempfile with name is created.

If a block is provided, each chunk of input_io is yielded for further processing.

[View source]

1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/mechanize/http/agent.rb', line 1261

def auto_io name, read_size, input_io
  out_io = StringIO.new.set_encoding(Encoding::BINARY)

  until input_io.eof? do
    if StringIO === out_io and use_tempfile? out_io.size then
      new_io = make_tempfile name
      new_io.write out_io.string
      out_io = new_io
    end

    chunk = input_io.read read_size
    chunk = yield chunk if block_given?

    out_io.write chunk
  end

  out_io.rewind

  out_io
end