Module: MrMurano::Pretties

Defined in:
lib/MrMurano/makePretty.rb

Constant Summary collapse

PRETTIES_COLORSCHEME =
HighLine::ColorScheme.new do |cs|
  cs[:subject] = [:red, :on_aliceblue]
  cs[:timestamp] = [:blue]
  cs[:json] = [:magenta]
end

Class Method Summary collapse

Class Method Details

.makeJsonPretty(data, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/MrMurano/makePretty.rb', line 16

def self.makeJsonPretty(data, options)
  if options.pretty then
    ret = JSON.pretty_generate(data).to_s
    ret[0] = HighLine.color(ret[0], :json)
    ret[-1] = HighLine.color(ret[-1], :json)
    ret
  else
    data.to_json
  end
end

.makePretty(line, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/MrMurano/makePretty.rb', line 27

def self.makePretty(line, options)
  out=''
  out << HighLine.color("#{line[:type] || '--'} ".upcase, :subject)
  out << HighLine.color("[#{line[:subject] || ''}]", :subject)
  out << " "
  if line.has_key?(:timestamp) then
    if line[:timestamp].kind_of? Numeric then
      if options.localtime then
        curtime = Time.at(line[:timestamp]).localtime.to_datetime.iso8601(3)
      else
        curtime = Time.at(line[:timestamp]).gmtime.to_datetime.iso8601(3)
      end
    else
      curtime = line[:timestamp]
    end
  else
    curtime = "<no timestamp>"
  end
  out << HighLine.color(curtime, :timestamp)
  out << ":\n"
  if line.has_key?(:data) then
    data = line[:data]

    if data.kind_of?(Hash) then
      if data.has_key?(:request) and data.has_key?(:response) then
        out << "---------\nrequest:"
        out << makeJsonPretty(data[:request], options)

        out << "\n---------\nresponse:"
        out << makeJsonPretty(data[:response], options)
      else
        out << makeJsonPretty(data, options)
      end
    else
      out << data.to_s
    end

  else
    line.delete :type
    line.delete :timestamp
    line.delete :subject
    out << makeJsonPretty(line, options)
  end
  out
end