Module: Ja::DebugLogger

Defined in:
lib/ja/debug_logger.rb

Instance Method Summary collapse

Instance Method Details

#perform(req, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ja/debug_logger.rb', line 6

def perform(req, options)
  Ja.logger.debug {
    lines = ["Sending #{req.verb.to_s.upcase} to #{req.uri.to_s.inspect}"]
    lines << "\e[36m"
    lines << req.headline
    lines += req.headers.map { |key, value| "#{key}: #{value}" }
    body = Ja.format_body(req.headers) {
      buffer = ""
      req.body.each { |chunk| buffer << chunk }
      buffer
    }
    lines.join("\n") + "\n\n" + body + "\n\e[0m\n"
  }

  res = super

  Ja.logger.debug {
    lines = ["Response from #{req.verb.to_s.upcase} to #{req.uri.to_s.inspect}"]
    lines << "\e[35m"
    lines << "HTTP/#{res.instance_variable_get(:@version)} #{res.status}"
    lines += res.headers.map { |key, value| "#{key}: #{value}" }
    body = Ja.format_body(res.headers) { res.body.to_s }
    lines.join("\n") + "\n\n" + body + "\n\e[0m\n"
  }

  res
end