Class: Web::Upload
Overview
Purpose
This class delegates to the uploaded Tempfile, and adds content type and original filename attributes.
If you are testing a multipart/form, use this class to pretend you uploaded a file:
"uploaded_file" => Upload.new(anIO,
"image/png",
"my_favorite_image.png" )
See Web::Testing for more information.
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#original_filename ⇒ Object
readonly
Returns the value of attribute original_filename.
Instance Method Summary collapse
-
#initialize(tempfile, content_type, original_filename) ⇒ Upload
constructor
A new instance of Upload.
- #local_path ⇒ Object
-
#open {|@tempfile| ... } ⇒ Object
how do I get in IO object from an upload?.
-
#read ⇒ Object
how do I get the contents of an upload?.
-
#save(filename) ⇒ Object
use this method to move an upload somewhere you care about.
Constructor Details
#initialize(tempfile, content_type, original_filename) ⇒ Upload
Returns a new instance of Upload.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/web/upload.rb', line 20 def initialize( tempfile, content_type, original_filename ) if (tempfile.is_a? String) contents = File.open( tempfile, "r" ) { |f| f.read } tempfile = Tempfile.new("Web") tempfile.binmode tempfile.write contents tempfile.rewind end @content_type = content_type @original_filename = original_filename @tempfile = tempfile end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
18 19 20 |
# File 'lib/web/upload.rb', line 18 def content_type @content_type end |
#original_filename ⇒ Object (readonly)
Returns the value of attribute original_filename.
18 19 20 |
# File 'lib/web/upload.rb', line 18 def original_filename @original_filename end |
Instance Method Details
#local_path ⇒ Object
34 35 36 |
# File 'lib/web/upload.rb', line 34 def local_path @tempfile.path end |
#open {|@tempfile| ... } ⇒ Object
how do I get in IO object from an upload?
53 54 55 56 |
# File 'lib/web/upload.rb', line 53 def open @tempfile.rewind yield @tempfile end |
#read ⇒ Object
how do I get the contents of an upload?
47 48 49 50 |
# File 'lib/web/upload.rb', line 47 def read @tempfile.rewind @tempfile.read end |
#save(filename) ⇒ Object
use this method to move an upload somewhere you care about
39 40 41 42 43 44 |
# File 'lib/web/upload.rb', line 39 def save( filename ) File.open( filename, "w" ) { |f| f.binmode f.write( self.read ) } end |