Class: Parts::FilePart
Overview
Represents a part to be filled from file IO.
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #build_head(boundary, name, filename, type, content_len) ⇒ Object
-
#initialize(boundary, name, io) ⇒ FilePart
constructor
A new instance of FilePart.
Methods included from Part
Constructor Details
#initialize(boundary, name, io) ⇒ FilePart
Returns a new instance of FilePart.
40 41 42 43 44 45 46 |
# File 'lib/parts.rb', line 40 def initialize(boundary, name, io) file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path) @head = StringIO.new(build_head(boundary, name, io.original_filename, io.content_type, file_length)) @foot = StringIO.new("\r\n") @length = @head.length + file_length + @foot.length @io = CompositeReadIO.new(@head, io, @foot) end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
39 40 41 |
# File 'lib/parts.rb', line 39 def length @length end |
Instance Method Details
#build_head(boundary, name, filename, type, content_len) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/parts.rb', line 48 def build_head(boundary, name, filename, type, content_len) part = '' part << "--#{boundary}\r\n" part << "Content-Disposition: form-data; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n" part << "Content-Length: #{content_len}\r\n" part << "Content-Type: #{type}\r\n" part << "Content-Transfer-Encoding: binary\r\n" part << "\r\n" end |