Class: Lugg::Request
- Inherits:
-
Object
- Object
- Lugg::Request
- Defined in:
- lib/lugg/request.rb
Overview
TODO:
optimise performance
Request is a value object representing a single log entry from start to finish. Its value is the original source text from the log file, but it defines various reader methods to extract useful information from it.
Note that a request is frozen once created. Two Request objects with the same source are considered equal.
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #action ⇒ Object
- #code ⇒ Object
- #controller ⇒ Object
- #duration ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #format ⇒ Object
-
#initialize(source) ⇒ Request
constructor
A new instance of Request.
- #ip ⇒ Object
- #method ⇒ Object
- #params ⇒ Object
- #path ⇒ Object
- #query ⇒ Object
- #status ⇒ Object
- #timestamp ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(source) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 |
# File 'lib/lugg/request.rb', line 15 def initialize(source) @source = source @hash = self.class.hash ^ source.hash freeze end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
13 14 15 |
# File 'lib/lugg/request.rb', line 13 def hash @hash end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
13 14 15 |
# File 'lib/lugg/request.rb', line 13 def source @source end |
Instance Method Details
#action ⇒ Object
34 35 36 37 |
# File 'lib/lugg/request.rb', line 34 def action source[/^Processing by (\w+)#(\w+) as (\w+)$/, 1] + '#' + source[/^Processing by (\w+)#(\w+) as (\w+)$/, 2] end |
#code ⇒ Object
47 48 49 |
# File 'lib/lugg/request.rb', line 47 def code source[/^Completed (\d+) (\w+)/, 1].to_i end |
#controller ⇒ Object
30 31 32 |
# File 'lib/lugg/request.rb', line 30 def controller source[/^Processing by (\w+)#(\w+) as (\w+)$/, 1] end |
#duration ⇒ Object
71 72 73 |
# File 'lib/lugg/request.rb', line 71 def duration source[/^Completed .* in (\d+)ms/, 1].to_i end |
#eql?(other) ⇒ Boolean Also known as: ==
21 22 23 |
# File 'lib/lugg/request.rb', line 21 def eql?(other) self.class == other.class && source == other.source end |
#format ⇒ Object
39 40 41 |
# File 'lib/lugg/request.rb', line 39 def format source[/^Processing by (\w+)#(\w+) as (\w+)$/, 3] end |
#ip ⇒ Object
51 52 53 |
# File 'lib/lugg/request.rb', line 51 def ip source[/^Started .* for ([0-9\.]+)/, 1] end |
#method ⇒ Object
26 27 28 |
# File 'lib/lugg/request.rb', line 26 def method source[/^Started ([A-Z]+)/, 1] end |
#params ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/lugg/request.rb', line 75 def params params_string = source[/^ Parameters: (.+)$/, 1] String(params_string) .scan(/(?<!\\)"(.+?)(?<!\\)"=>(?<!\\)"(.+?)(?<!\\)"/) .each_with_object({}) do |match, output| output[match[0]] = match[1] end end |
#path ⇒ Object
63 64 65 |
# File 'lib/lugg/request.rb', line 63 def path uri.split('?').first end |
#query ⇒ Object
67 68 69 |
# File 'lib/lugg/request.rb', line 67 def query uri.split('?', 2).last end |
#status ⇒ Object
43 44 45 |
# File 'lib/lugg/request.rb', line 43 def status source[/^Completed (\d+) (\w+)/, 2] end |
#timestamp ⇒ Object
55 56 57 |
# File 'lib/lugg/request.rb', line 55 def Time.parse(source[/^Started .* at (.+)$/, 1]) end |
#uri ⇒ Object
59 60 61 |
# File 'lib/lugg/request.rb', line 59 def uri source[/^Started \w+ "([^"]+)"/, 1] end |