class ApiHammer::TrailingNewline

Rack middleware which adds a trailing newline to any responses which do not include one.

one effect of this is to make curl more readable, as without this, the prompt that follows the request will be on the same line.

does not add a newline to blank responses.

Public Class Methods

new(app) click to toggle source
# File lib/api_hammer/trailing_newline.rb, line 11
def initialize(app)
  @app=app
end

Public Instance Methods

call(env) click to toggle source
# File lib/api_hammer/trailing_newline.rb, line 27
def call(env)
  status, headers, body = *@app.call(env)
  _, content_type = headers.detect { |(k,_)| k =~ /\Acontent.type\z/i }
  if env['REQUEST_METHOD'].downcase != 'head' && ApiHammer::ContentTypeAttrs.new(content_type).text?
    body = TNLBodyProxy.new(body){}
    if headers["Content-Length"]
      headers["Content-Length"] = body.map(&:bytesize).inject(0, &:+).to_s
    end
  end
  [status, headers, body]
end