Class: Multiparty
- Inherits:
-
Object
- Object
- Multiparty
- Defined in:
- lib/multiparty.rb
Instance Attribute Summary collapse
-
#boundary ⇒ Object
Returns the value of attribute boundary.
-
#parts ⇒ Object
Returns the value of attribute parts.
Instance Method Summary collapse
- #add_part(index, value) ⇒ Object (also: #[]=)
- #add_parts(parts) ⇒ Object (also: #<<)
- #body ⇒ Object (also: #to_s)
- #header ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ Multiparty
constructor
Multiparty.new(“my-boundary”) Multiparty.new(:boundary => “my-boundary”).
- #parse_part(name, value) ⇒ Object
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Multiparty
Multiparty.new(“my-boundary”) Multiparty.new(:boundary => “my-boundary”)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/multiparty.rb', line 10 def initialize( = {}) case when Hash @boundary = [:boundary] @content_type = [:content_type] when String @boundary = end @parts = {} @content_type ||= "multipart/form-data" @boundary ||= "multiparty-boundary-#{rand(1000000000)}" yield self if block_given? end |
Instance Attribute Details
#boundary ⇒ Object
Returns the value of attribute boundary.
5 6 7 |
# File 'lib/multiparty.rb', line 5 def boundary @boundary end |
#parts ⇒ Object
Returns the value of attribute parts.
6 7 8 |
# File 'lib/multiparty.rb', line 6 def parts @parts end |
Instance Method Details
#add_part(index, value) ⇒ Object Also known as: []=
71 72 73 |
# File 'lib/multiparty.rb', line 71 def add_part(index, value) parts[index] = value end |
#add_parts(parts) ⇒ Object Also known as: <<
76 77 78 79 80 |
# File 'lib/multiparty.rb', line 76 def add_parts(parts) parts.each do |index, value| add_part(index, value) end end |
#body ⇒ Object Also known as: to_s
30 31 32 33 34 35 36 37 |
# File 'lib/multiparty.rb', line 30 def body result = "--#{@boundary}\r\n" result << parts.map do |name, value| parse_part(name, value) end.join("\r\n") result << "--" end |
#header ⇒ Object
26 27 28 |
# File 'lib/multiparty.rb', line 26 def header "Content-Type: #{@content_type}; boundary=#{@boundary}\r\n" end |
#parse_part(name, value) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/multiparty.rb', line 40 def parse_part(name, value) content_disposition = "form-data" case value when Hash content_disposition = value[:content_disposition] if value[:content_disposition] content_type = value[:content_type] filename = value[:filename] encoding = value[:encoding] body_part = value[:content] when File, Tempfile content_type = "application/octet-stream" filename = File.split(value.path).last body_part = value.read else body_part = value end if filename content_type ||= MIME::Types.of(filename).first.to_s || "application/octet-stream" encoding ||= "binary" end head_part = "Content-Disposition: #{content_disposition}; name=\"#{name}\"" head_part << "; filename=\"#{filename}\"" if filename head_part << "\r\n" head_part << "Content-Type: #{content_type}\r\n" if content_type head_part << "Content-Transfer-Encoding: #{encoding}\r\n" if encoding "#{head_part}\r\n#{body_part}\r\n--#{@boundary}" end |