Class: Mushikago::Http::MultipartRequest
- Inherits:
-
Request
- Object
- Request
- Mushikago::Http::MultipartRequest
- Defined in:
- lib/mushikago/http/multipart.rb
Direct Known Subclasses
Constant Summary
- CR =
"\015"
- LF =
"\012"
- EOL =
CR+LF
Instance Attribute Summary
Attributes inherited from Request
headers, host, params, path, port
Instance Method Summary (collapse)
Methods inherited from Request
#[], #[]=, add_param, #initialize, #url_encoded_params
Constructor Details
This class inherits a constructor from Mushikago::Http::Request
Instance Method Details
- (Object) multipart_body(boundary = 'boundary')
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mushikago/http/multipart.rb', line 22 def multipart_body boundary='boundary' content = '' files, parameters = params.partition{|k, v| v.kind_of?(File)} parameters.each do |k, v| content << [ %Q|--#{boundary}|, %Q|Content-Disposition: form-data; name="#{k}"|, %Q||, %Q|#{v}| ].join(EOL) content << EOL end files.each do |k, v| v.rewind content_type = '' begin content_type = MIME::Types.of(v.path).first.to_s rescue NameError => e ensure content_type = "application/octet-stream" if content_type.to_s.empty? end content << [ %Q|--#{boundary}|, %Q|Content-Disposition: form-data; name="#{k}"; filename="#{File::basename(v.path)}"|, %Q|Content-Type: "#{content_type}"|, %Q||, %Q|#{v.read}| ].join(EOL) end content << %Q|--#{boundary}--| content << EOL return content end |
- (Object) to_http_request
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mushikago/http/multipart.rb', line 10 def to_http_request http_request = new_http_request headers.each do |key, value| http_request[key] = value end boundary = '----------------38117' http_request.set_content_type "multipart/form-data; boundary=#{boundary}" http_request.body = multipart_body(boundary) http_request.content_length = http_request.body.size return http_request end |