Class: LogWeaver::ParsedLog
- Inherits:
-
Object
- Object
- LogWeaver::ParsedLog
- Defined in:
- lib/log_weaver/parsed_log.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
TODO: rename; attr_reader should suffice.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(prefix, log) ⇒ ParsedLog
constructor
A new instance of ParsedLog.
Constructor Details
Instance Attribute Details
#lines ⇒ Object
TODO: rename; attr_reader should suffice
5 6 7 |
# File 'lib/log_weaver/parsed_log.rb', line 5 def lines @lines end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/log_weaver/parsed_log.rb', line 6 def prefix @prefix end |
Class Method Details
.extract_time_stamp(line) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/log_weaver/parsed_log.rb', line 33 def self.extract_time_stamp(line) = line[/^[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9](:[0-5][0-9]){2}\.[0-9]{3}/,0] = line.sub(/^#{}/,'') = Time.parse() unless .nil? [,] end |
.parse_log(log) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/log_weaver/parsed_log.rb', line 15 def self.parse_log(log) res = {} previous_key = nil log.string.split("\n").each do |line| (, ) = extract_time_stamp(line) if key = res[key] = [] unless key == previous_key previous_key = key else raise ArgumentError, "Log does not begin with a timestamp." if previous_key.nil? end res[previous_key] << line #message end res end |