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.



284
285
286
287
288
# File 'lib/http/client.rb', line 284

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

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



279
280
281
# File 'lib/http/client.rb', line 279

def boundary
  @boundary
end

Instance Method Details

#bodyObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/http/client.rb', line 294

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



290
291
292
# File 'lib/http/client.rb', line 290

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