Module: Depthcharge::Formatters
- Included in:
- RequestLogger
- Defined in:
- lib/depthcharge/formatters.rb
Constant Summary collapse
- MAIN_INDENT =
" | "- SUB_INDENT =
" "- NEWLINE =
"\n"
Instance Method Summary collapse
- #blank_line(level = 1) ⇒ Object
- #format_body(headers, body, level = 1) ⇒ Object
- #format_hash(name, hash, level = 1) ⇒ Object
- #format_line(line, level = 1) ⇒ Object
- #format_lines(lines, level = 1) ⇒ Object
- #indent(level) ⇒ Object
Instance Method Details
#blank_line(level = 1) ⇒ Object
38 39 40 |
# File 'lib/depthcharge/formatters.rb', line 38 def blank_line(level = 1) format_line("", level) end |
#format_body(headers, body, level = 1) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/depthcharge/formatters.rb', line 8 def format_body(headers, body, level = 1) content_type = headers["Content-Type"] body = body.join if body.respond_to?(:join) body = body.body if body.respond_to?(:body) if !content_type.nil? && content_type.include?("json") format_lines(JSON.pretty_generate(JSON.parse(body)), level) else format_lines(body, level) end end |
#format_hash(name, hash, level = 1) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/depthcharge/formatters.rb', line 20 def format_hash(name, hash, level = 1) output = format_line(name.to_s.upcase + ":", level) hash.each do |key, value| output << format_line("#{key.to_s}: #{value.inspect}", level + 1) end output end |
#format_line(line, level = 1) ⇒ Object
42 43 44 |
# File 'lib/depthcharge/formatters.rb', line 42 def format_line(line, level = 1) indent(level) + line + NEWLINE end |
#format_lines(lines, level = 1) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/depthcharge/formatters.rb', line 30 def format_lines(lines, level = 1) lines = lines.split($/).map do |line| format_line(line, level) end lines.join end |
#indent(level) ⇒ Object
46 47 48 49 50 |
# File 'lib/depthcharge/formatters.rb', line 46 def indent(level) return "" if level == 0 MAIN_INDENT + (SUB_INDENT * (level - 1)) end |