Class: Mosquito::MockUpload

Inherits:
StringIO
  • Object
show all
Defined in:
lib/mosquito.rb

Overview

Works like a wrapper for a simulated file upload. To use:

uploaded = Mosquito::MockUpload.new("beach.jpg")

This will create a file with the JPEG content-type and 122 bytes of purely random data, which can then be submitted as a part of the test request

Constant Summary collapse

IMAGE_TYPES =
{:jpg => 'image/jpeg', :png => 'image/png', :gif => 'image/gif',
:pdf => 'application/pdf' }.stringify_keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MockUpload

Returns a new instance of MockUpload.



337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/mosquito.rb', line 337

def initialize(filename)
  tempname = "tempfile_#{Time.now.to_i}"
  
  @temp = ::Tempfile.new(tempname)
  @local_path = @temp.path    
  @original_filename = File.basename(filename)
  @extension = File.extname(@original_filename).gsub(/^\./, '').downcase
  @content_type = IMAGE_TYPES[@extension] || "application/#{@extension}"
  
  size = 100.bytes
  super("Stub file %s \n%s\n" % [@original_filename, Mosquito::garbage(size)])
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



333
334
335
# File 'lib/mosquito.rb', line 333

def content_type
  @content_type
end

#extensionObject (readonly)

Returns the value of attribute extension.



333
334
335
# File 'lib/mosquito.rb', line 333

def extension
  @extension
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



333
334
335
# File 'lib/mosquito.rb', line 333

def local_path
  @local_path
end

#original_filenameObject (readonly)

Returns the value of attribute original_filename.



333
334
335
# File 'lib/mosquito.rb', line 333

def original_filename
  @original_filename
end

Instance Method Details

#inspectObject



350
351
352
353
# File 'lib/mosquito.rb', line 350

def inspect
  info = " @size='#{length}' @filename='#{original_filename}' @content_type='#{content_type}'>"
  super[0..-2] + info
end