Class: Utopia::ContentLength
- Inherits:
-
Object
- Object
- Utopia::ContentLength
- Defined in:
- lib/utopia/content_length.rb
Overview
A faster implementation of Rack::ContentLength which doesn’t rewrite body, but does expect it to either be an Array or an object that responds to #bytesize.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #content_length_of(body) ⇒ Object
-
#initialize(app) ⇒ ContentLength
constructor
A new instance of ContentLength.
Constructor Details
#initialize(app) ⇒ ContentLength
Returns a new instance of ContentLength.
11 12 13 |
# File 'lib/utopia/content_length.rb', line 11 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/utopia/content_length.rb', line 21 def call(env) response = @app.call(env) unless response[2]&.empty? or response[1].include?(Rack::CONTENT_LENGTH) if content_length = self.content_length_of(response[2]) response[1][Rack::CONTENT_LENGTH] = content_length end end return response end |
#content_length_of(body) ⇒ Object
15 16 17 18 19 |
# File 'lib/utopia/content_length.rb', line 15 def content_length_of(body) if body.respond_to?(:map) return body.map(&:bytesize).reduce(0, :+) end end |