Class: ActionDispatch::Response::FileBody

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/http/response.rb

Overview

Avoid having to pass an open file handle as the response body. Rack::Sendfile will usually intercept the response and uses the path directly, so there is no reason to open the file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileBody

Returns a new instance of FileBody.



338
339
340
# File 'actionpack/lib/action_dispatch/http/response.rb', line 338

def initialize(path)
  @to_path = path
end

Instance Attribute Details

#to_pathObject (readonly)

Returns the value of attribute to_path



336
337
338
# File 'actionpack/lib/action_dispatch/http/response.rb', line 336

def to_path
  @to_path
end

Instance Method Details

#bodyObject



342
343
344
# File 'actionpack/lib/action_dispatch/http/response.rb', line 342

def body
  File.binread(to_path)
end

#eachObject

Stream the file’s contents if Rack::Sendfile isn’t present.



347
348
349
350
351
352
353
# File 'actionpack/lib/action_dispatch/http/response.rb', line 347

def each
  File.open(to_path, "rb") do |file|
    while chunk = file.read(16384)
      yield chunk
    end
  end
end