Class: ApiHammer::TrailingNewline

Inherits:
Object
  • Object
show all
Defined in:
lib/api_hammer/trailing_newline.rb

Overview

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.

Defined Under Namespace

Classes: TNLBodyProxy

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TrailingNewline

Returns a new instance of TrailingNewline.



11
12
13
# File 'lib/api_hammer/trailing_newline.rb', line 11

def initialize(app)
  @app=app
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# 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