Class: CSVKit::Middleware
- Inherits:
-
Object
- Object
- CSVKit::Middleware
- Defined in:
- lib/csvkit/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, conditions = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, options = {}, conditions = {}) ⇒ Middleware
3 4 5 6 7 |
# File 'lib/csvkit/middleware.rb', line 3 def initialize(app, = {}, conditions = {}) @app = app @options = @conditions = conditions end |
Instance Method Details
#call(env) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/csvkit/middleware.rb', line 9 def call(env) @request = Rack::Request.new(env) @render_csv = false set_request_to_render_as_csv(env) if render_as_csv? status, headers, response = @app.call(env) if rendering_csv? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/ body = response.respond_to?(:body) ? response.body : response.join body = body.join if body.is_a?(Array) body = CSVKit.new(body, @options).to_csv response = [body] # Do not cache CSVs headers.delete('ETag') headers.delete('Cache-Control') headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s headers["Content-Type"] = "text/csv" end [status, headers, response] end |