Class: Net::HTTP::Post::FilePart

Inherits:
Object
  • Object
show all
Includes:
Part
Defined in:
lib/net/http/post/multipart.rb

Overview

Represents a part to be filled from file IO.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Part

new, #to_io

Constructor Details

#initialize(boundary, name, io) ⇒ FilePart

Returns a new instance of FilePart.



54
55
56
57
58
59
60
61
62
63
# File 'lib/net/http/post/multipart.rb', line 54

def initialize(boundary, name, io)
  @head = build_head(boundary, name, io.original_filename, io.content_type)
  file_length = if io.respond_to? :length
    io.length
  else
    File.size(io.local_path)
  end
  @length = @head.length + file_length
  @io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new("\r\n"))
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



53
54
55
# File 'lib/net/http/post/multipart.rb', line 53

def length
  @length
end

Instance Method Details

#build_head(boundary, name, filename, type) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/net/http/post/multipart.rb', line 65

def build_head(boundary, name, filename, type)
  part = ''
  part << "--#{boundary}\r\n"
  part << "Content-Disposition: form-data; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n"
  part << "Content-Type: #{type}\r\n"
  part << "Content-Transfer-Encoding: binary\r\n"
  part << "\r\n"
end