Class: Evidence::RailsActionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/evidence/rails_action_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(pid, message, unmatched) ⇒ RailsActionParser

Returns a new instance of RailsActionParser.



4
5
6
7
# File 'lib/evidence/rails_action_parser.rb', line 4

def initialize(pid, message, unmatched)
  @pid, @message = pid, message
  @unmatched = unmatched
end

Instance Method Details

#[](output) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/evidence/rails_action_parser.rb', line 9

def [](output)
  processes = Hash.new
  lambda do |log|
    pid = @pid[log]
    if processes.has_key?(pid)
      processes[pid] << log
      if end_action?(@message[log])
        output.call(parse_action_logs(processes.delete(pid)))
      end
    else
      if start_action?(@message[log])
        processes[pid] = [log]
      else
        @unmatched.call(log)
      end
    end
  end
end

#end_action?(msg) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/evidence/rails_action_parser.rb', line 36

def end_action?(msg)
  msg =~ end_action_pattern
end

#end_action_patternObject

Completed in 755ms (View: 330, DB: 215) | 200 OK [url]



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/evidence/rails_action_parser.rb', line 66

def end_action_pattern
  /^
    Completed\sin\s
    (?<completed_time>\d+)ms\s+
    \(View\:\s(?<view_time>\d+)
    (,\s*DB\:\s(?<db_time>\d+))?
    \)\s+\|\s+
    (?<code>\d+)\s+
    (?<status>\w+)\s+
    \[(?<url>.+)\]
  $/x
end

#parse_action_logs(logs) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/evidence/rails_action_parser.rb', line 28

def parse_action_logs(logs)
  {
    request: request(@message[logs[0]]),
    response: response(@message[logs[-1]]),
    logs: logs
  }
end

#request(msg) ⇒ Object



44
45
46
# File 'lib/evidence/rails_action_parser.rb', line 44

def request(msg)
  to_hash(start_action_pattern.match(msg))
end

#response(msg) ⇒ Object



48
49
50
# File 'lib/evidence/rails_action_parser.rb', line 48

def response(msg)
  to_hash(end_action_pattern.match(msg))
end

#start_action?(msg) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/evidence/rails_action_parser.rb', line 40

def start_action?(msg)
  msg =~ start_action_pattern
end

#start_action_patternObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/evidence/rails_action_parser.rb', line 52

def start_action_pattern
  /^
    (\#012\#012)?             # ignore encoded newlines
    Processing\s+
    (?<controller>\w+)\#(?<action>\w+)\s+
    \(for\s+
    (?<remote_addr>[^\s]+)\s+
    at\s+
    (?<timestamp>[^\)]+)\)\s+
    \[(?<method>[\w]+)\]
  $/x
end

#to_hash(m) ⇒ Object



79
80
81
# File 'lib/evidence/rails_action_parser.rb', line 79

def to_hash(m)
  Hash[m.names.map(&:to_sym).zip(m.captures)]
end