Class: HTTP::Client::Multipart

Inherits:
Object
  • Object
show all
Defined in:
lib/http/client.rb

Overview

Response

Constant Summary collapse

EOL =
"\r\n"
DEFAULT_MIME_TYPE =
'application/octet-stream'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, query = {}) ⇒ Multipart

Returns a new instance of Multipart.



313
314
315
316
317
# File 'lib/http/client.rb', line 313

def initialize files, query = {}
  @files    = files
  @query    = query
  @boundary = generate_boundary
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



308
309
310
# File 'lib/http/client.rb', line 308

def boundary
  @boundary
end

Instance Method Details

#bodyObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/http/client.rb', line 323

def body
  body      = ''.encode('ASCII-8BIT')
  separator = "--#{boundary}"

  @query.each do |key, value|
    body << separator << EOL
    body << %Q{Content-Disposition: form-data; name="#{key}"} << EOL
    body << EOL
    body << value
    body << EOL
  end

  @files.each do |name, handle|
    if handle.respond_to?(:read)
      path = handle.path
      data = io.read
    else
      path = handle
      data = IO.read(path)
    end

    filename = File.basename(path)
    mime     = mime_type(filename)

    body << separator << EOL
    body << %Q{Content-Disposition: form-data; name="#{name}"; filename="#{filename}"} << EOL
    body << %Q{Content-Type: #{mime}}              << EOL
    body << %Q{Content-Transfer-Encoding: binary}  << EOL
    body << %Q{Content-Length: #{data.bytesize}}   << EOL
    body << EOL
    body << data
    body << EOL
  end

  body << separator << "--" << EOL
  body
end

#content_typeObject



319
320
321
# File 'lib/http/client.rb', line 319

def content_type
  "multipart/form-data; boundary=#{boundary}"
end