Class: RequestVisualizer

Inherits:
Object
  • Object
show all
Defined in:
lib/request_visualizer.rb,
lib/request_visualizer/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(app, &lookup) ⇒ RequestVisualizer

Returns a new instance of RequestVisualizer.



3
4
5
6
# File 'lib/request_visualizer.rb', line 3

def initialize(app, &lookup)
  @app = app
  @lookup = lookup
end

Instance Method Details

#call(env) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/request_visualizer.rb', line 65

def call(env)
  @@indent ||= 0
  @@indent += 5
  from, to = log_request(Rack::Request.new(env))
  status, headers, body = @app.call(env)
  log_response(from, to, headers, body, status)
  @@indent -= 5
  [status, headers, body]
end

#colorize(string) ⇒ Object



12
13
14
15
16
17
# File 'lib/request_visualizer.rb', line 12

def colorize(string)
  @@colors ||= [:white_on_black, :white_on_red, :white_on_blue, :black_on_cyan, :black_on_magenta, :black_on_yellow, :black_on_green]
  @@colorized_before ||= {}
  @@colorized_before[string] ||= string.send(@@colors.pop || :black_on_white)
  @@colorized_before[string]
end

#indentObject



19
20
21
# File 'lib/request_visualizer.rb', line 19

def indent
  " "*@@indent
end

#log_request(request) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/request_visualizer.rb', line 23

def log_request(request)
  from = parse(request.user_agent.to_s)
  to = parse(request.url.to_s)
  puts "#{self.indent}#{from} -> #{request.request_method.upcase.bold} (#{request.url.underline}) -> #{to}"
  request.body.rewind
  req_body = request.body.read
  indent_prefix = self.indent
  begin
    if request.env["CONTENT_TYPE"].to_s.include?("form-urlencoded")
      puts indent_prefix + CGI.parse(req_body).pretty_inspect.gsub("\n","\n#{indent_prefix}")
    else
      json = JSON.parse(req_body)
      puts indent_prefix + json.pretty_inspect.gsub("\n","\n#{indent_prefix}")
    end
  rescue => e
    # puts "WARN: JSON request with non-json body! (#{req_body})"
  end
  request.body.rewind
  [from, to]
end

#log_response(from, to, headers, body, status) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/request_visualizer.rb', line 44

def log_response(from, to, headers, body, status)
  location = headers["Location"]
  if location
    puts "#{self.indent}#{from} <--#{status}-- #{location.underline} <- #{to}"
  else
    puts "#{self.indent}#{from} <--#{status}-- #{to}"
  end
  do_inspect = true
  if headers["Content-Type"].to_s.match(/json/) && do_inspect
    body.each do |bod|
      begin
        json = JSON.parse(bod)
        indent_prefix = self.indent
        puts indent_prefix + json.pretty_inspect.gsub("\n","\n#{indent_prefix}")
      rescue => e
        puts "WARN: JSON response with non-json body! (#{bod})"
      end
    end
  end
end

#parse(string) ⇒ Object



8
9
10
# File 'lib/request_visualizer.rb', line 8

def parse(string)
  colorize((@lookup && @lookup.call(string.to_s)) || smart_lookup(string.to_s))
end