Class: XamarinTestCloud::HTTP::Payload::Multipart

Inherits:
Base
  • Object
show all
Defined in:
lib/xamarin-test-cloud/http/payload.rb

Constant Summary collapse

EOL =
"\r\n"

Instance Method Summary collapse

Methods inherited from Base

#flatten_params, #flatten_params_array, #initialize, #inspect, #read, #short_inspect, #size

Constructor Details

This class inherits a constructor from XamarinTestCloud::HTTP::Payload::Base

Instance Method Details

#boundaryObject



248
249
250
# File 'lib/xamarin-test-cloud/http/payload.rb', line 248

def boundary
  @boundary ||= rand(1_000_000).to_s
end

#build_stream(params) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xamarin-test-cloud/http/payload.rb', line 192

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

#closeObject



261
262
263
# File 'lib/xamarin-test-cloud/http/payload.rb', line 261

def close
  @stream.close!
end

#create_file_field(s, k, v) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/xamarin-test-cloud/http/payload.rb', line 228

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

#create_regular_field(s, k, v) ⇒ Object



221
222
223
224
225
226
# File 'lib/xamarin-test-cloud/http/payload.rb', line 221

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

#handle_key(key) ⇒ Object

for Multipart do not escape the keys



253
254
255
# File 'lib/xamarin-test-cloud/http/payload.rb', line 253

def handle_key key
  key
end

#headersObject



257
258
259
# File 'lib/xamarin-test-cloud/http/payload.rb', line 257

def headers
  super.merge({'Content-Type' => %Q{multipart/form-data; boundary=#{boundary}}})
end

#mime_for(path) ⇒ Object



243
244
245
246
# File 'lib/xamarin-test-cloud/http/payload.rb', line 243

def mime_for(path)
  mime = MIME::Types.type_for path
  mime.empty? ? 'text/plain' : mime[0].content_type
end