Class: ZipKit::RackTempfileBody
- Inherits:
-
Object
- Object
- ZipKit::RackTempfileBody
- Defined in:
- lib/zip_kit/rack_tempfile_body.rb
Overview
Contains a file handle which can be closed once the response finishes sending.
It supports to_path so that Rack::Sendfile can intercept it
Constant Summary collapse
- TEMPFILE_NAME_PREFIX =
"zip-tricks-tf-body-"
Instance Attribute Summary collapse
-
#tempfile ⇒ Object
readonly
Returns the value of attribute tempfile.
Instance Method Summary collapse
-
#each ⇒ void
Stream the file's contents if
Rack::Sendfileisn't present. -
#initialize(env, body) ⇒ RackTempfileBody
constructor
A new instance of RackTempfileBody.
-
#size ⇒ Integer
Returns the size of the contained
Tempfileso that a correct Content-Length header can be set. -
#to_path ⇒ String
Returns the path to the
Tempfile, so that Rack::Sendfile can send this response using the downstream webserver.
Constructor Details
#initialize(env, body) ⇒ RackTempfileBody
Returns a new instance of RackTempfileBody.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 11 def initialize(env, body) @tempfile = Tempfile.new(TEMPFILE_NAME_PREFIX) # Rack::TempfileReaper calls close! on tempfiles which get buffered # We wil assume that it works fine with Rack::Sendfile (i.e. the path # to the file getting served gets used before we unlink the tempfile) env["rack.tempfiles"] ||= [] env["rack.tempfiles"] << @tempfile @tempfile.binmode @body = body @did_flush = false end |
Instance Attribute Details
#tempfile ⇒ Object (readonly)
Returns the value of attribute tempfile.
7 8 9 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 7 def tempfile @tempfile end |
Instance Method Details
#each ⇒ void
This method returns an undefined value.
Stream the file's contents if Rack::Sendfile isn't present.
45 46 47 48 49 50 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 45 def each flush while (chunk = @tempfile.read(16384)) yield chunk end end |
#size ⇒ Integer
Returns the size of the contained Tempfile so that a correct
Content-Length header can be set
28 29 30 31 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 28 def size flush @tempfile.size end |
#to_path ⇒ String
Returns the path to the Tempfile, so that Rack::Sendfile can send this response
using the downstream webserver
37 38 39 40 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 37 def to_path flush @tempfile.to_path end |