Class: Rack::TrafficLogger::Echo

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/traffic_logger/echo.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/traffic_logger/echo.rb', line 7

def call(env)
  body = env['rack.input'].tap(&:rewind).read
  headers = {}
  begin
    body = JSON.parse(body).to_json
    headers['Content-Type'] = 'application/json'
  rescue JSON::ParserError
    # ignored
  end
  if env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
    zipped = StringIO.new('w')
    writer = Zlib::GzipWriter.new(zipped)
    writer.write body
    writer.close
    body = zipped.string
    headers['Content-Encoding'] = 'gzip'
  end
  [200, headers, [body]]
end