Class: RestClient::Payload::Multipart
- Inherits:
-
Base
- Object
- Base
- RestClient::Payload::Multipart
- Defined in:
- lib/restclient/payload.rb
Constant Summary
- EOL =
"\r\n"
Instance Method Summary (collapse)
- - (Object) boundary
- - (Object) build_stream(params)
- - (Object) close
- - (Object) create_file_field(s, k, v)
- - (Object) create_regular_field(s, k, v)
-
- (Object) handle_key(key)
for Multipart do not escape the keys.
- - (Object) headers
- - (Object) mime_for(path)
Methods inherited from Base
#flatten_params, #flatten_params_array, #initialize, #inspect, #read, #short_inspect, #size
Constructor Details
This class inherits a constructor from RestClient::Payload::Base
Instance Method Details
- (Object) boundary
217 218 219 |
# File 'lib/restclient/payload.rb', line 217 def boundary @boundary ||= rand(1_000_000).to_s end |
- (Object) build_stream(params)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/restclient/payload.rb', line 161 def build_stream(params) b = "--#{boundary}" @stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}") @stream.binmode @stream.write(b + EOL) if params.is_a? Hash x = flatten_params(params) else x = params end last_index = x.length - 1 x.each_with_index do |a, index| k, v = * a if v.respond_to?(:read) && v.respond_to?(:path) create_file_field(@stream, k, v) else create_regular_field(@stream, k, v) end @stream.write(EOL + b) @stream.write(EOL) unless last_index == index end @stream.write('--') @stream.write(EOL) @stream.seek(0) end |
- (Object) close
230 231 232 |
# File 'lib/restclient/payload.rb', line 230 def close @stream.close! end |
- (Object) create_file_field(s, k, v)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/restclient/payload.rb', line 197 def create_file_field(s, k, v) begin s.write("Content-Disposition: form-data;") s.write(" name=\"#{k}\";") unless (k.nil? || k=='') s.write(" filename=\"#{v.respond_to?(:original_filename) ? v.original_filename : File.basename(v.path)}\"#{EOL}") s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{EOL}") s.write(EOL) while data = v.read(8124) s.write(data) end ensure v.close if v.respond_to?(:close) end end |
- (Object) create_regular_field(s, k, v)
190 191 192 193 194 195 |
# File 'lib/restclient/payload.rb', line 190 def create_regular_field(s, k, v) s.write("Content-Disposition: form-data; name=\"#{k}\"") s.write(EOL) s.write(EOL) s.write(v) end |
- (Object) handle_key(key)
for Multipart do not escape the keys
222 223 224 |
# File 'lib/restclient/payload.rb', line 222 def handle_key key key end |
- (Object) headers
226 227 228 |
# File 'lib/restclient/payload.rb', line 226 def headers super.merge({'Content-Type' => %Q{multipart/form-data; boundary=#{boundary}}}) end |
- (Object) mime_for(path)
212 213 214 215 |
# File 'lib/restclient/payload.rb', line 212 def mime_for(path) mime = MIME::Types.type_for path mime.empty? ? 'text/plain' : mime[0].content_type end |