Class: RequestLogAnalyzer::LineDefinition
- Inherits:
-
Object
- Object
- RequestLogAnalyzer::LineDefinition
- Defined in:
- lib/request_log_analyzer/line_definition.rb
Overview
The line definition class is used to specify what lines should be parsed from the log file. It contains functionality to match a line against the definition and parse the information from this line. This is used by the LogParser class when parsing a log file..
Defined Under Namespace
Classes: Definer
Instance Attribute Summary collapse
-
#captures ⇒ Object
Returns the value of attribute captures.
-
#footer ⇒ Object
(also: #footer?)
Returns the value of attribute footer.
-
#header ⇒ Object
(also: #header?)
Returns the value of attribute header.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#regexp ⇒ Object
Returns the value of attribute regexp.
-
#teaser ⇒ Object
Returns the value of attribute teaser.
Class Method Summary collapse
Instance Method Summary collapse
-
#captures?(name) ⇒ Boolean
Returns true if this line captures values of the given name.
-
#convert_captured_values(values, request) ⇒ Object
Updates a captures hash using the converters specified in the request and handle the :provides option in the line definition.
-
#initialize(name, definition = {}) ⇒ LineDefinition
constructor
Initializes the LineDefinition instance with a hash containing the different elements of the definition.
-
#match_for(line, request, &warning_handler) ⇒ Object
matches the line and converts the captured values using the request’s convert_value function.
-
#matches(line, &warning_handler) ⇒ Object
(also: #=~)
Checks whether a given line matches this definition.
Constructor Details
#initialize(name, definition = {}) ⇒ LineDefinition
Initializes the LineDefinition instance with a hash containing the different elements of the definition.
38 39 40 41 42 43 |
# File 'lib/request_log_analyzer/line_definition.rb', line 38 def initialize(name, definition = {}) @name = name @captures = [] = nil definition.each { |key, value| self.send("#{key.to_s}=".to_sym, value) } end |
Instance Attribute Details
#captures ⇒ Object
Returns the value of attribute captures.
30 31 32 |
# File 'lib/request_log_analyzer/line_definition.rb', line 30 def captures @captures end |
#footer ⇒ Object Also known as:
Returns the value of attribute footer.
31 32 33 |
# File 'lib/request_log_analyzer/line_definition.rb', line 31 def end |
#header ⇒ Object Also known as: header?
Returns the value of attribute header.
31 32 33 |
# File 'lib/request_log_analyzer/line_definition.rb', line 31 def header @header end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/request_log_analyzer/line_definition.rb', line 29 def name @name end |
#regexp ⇒ Object
Returns the value of attribute regexp.
30 31 32 |
# File 'lib/request_log_analyzer/line_definition.rb', line 30 def regexp @regexp end |
#teaser ⇒ Object
Returns the value of attribute teaser.
30 31 32 |
# File 'lib/request_log_analyzer/line_definition.rb', line 30 def end |
Class Method Details
.define(name) {|definition| ... } ⇒ Object
45 46 47 48 49 |
# File 'lib/request_log_analyzer/line_definition.rb', line 45 def self.define(name, &block) definition = self.new(name) yield(definition) if block_given? return definition end |
Instance Method Details
#captures?(name) ⇒ Boolean
Returns true if this line captures values of the given name
105 106 107 |
# File 'lib/request_log_analyzer/line_definition.rb', line 105 def captures?(name) captures.any? { |c| c[:name] == name } end |
#convert_captured_values(values, request) ⇒ Object
Updates a captures hash using the converters specified in the request and handle the :provides option in the line definition.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/request_log_analyzer/line_definition.rb', line 85 def convert_captured_values(values, request) value_hash = {} captures.each_with_index do |capture, index| # convert the value using the request convert_value function converted = request.convert_value(values[index], capture) value_hash[capture[:name]] ||= converted # Add items directly to the resulting hash from the converted value # if it is a hash and they are set in the :provides hash for this line definition if converted.kind_of?(Hash) && capture[:provides].kind_of?(Hash) capture[:provides].each do |name, type| value_hash[name] ||= request.convert_value(converted[name], { :type => type }) end end end return value_hash end |
#match_for(line, request, &warning_handler) ⇒ Object
matches the line and converts the captured values using the request’s convert_value function.
75 76 77 78 79 80 81 |
# File 'lib/request_log_analyzer/line_definition.rb', line 75 def match_for(line, request, &warning_handler) if match_info = matches(line, &warning_handler) convert_captured_values(match_info[:captures], request) else false end end |
#matches(line, &warning_handler) ⇒ Object Also known as: =~
Checks whether a given line matches this definition. It will return false if a line does not match. If the line matches, a hash is returned with all the fields parsed from that line as content. If the line definition has a teaser-check, a :teaser_check_failed warning will be emitted if this teaser-check is passed, but the full regular exprssion does not ,atch.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/request_log_analyzer/line_definition.rb', line 56 def matches(line, &warning_handler) if .nil? || =~ line if match_data = line.match(@regexp) return { :line_definition => self, :captures => match_data.captures} else if && warning_handler warning_handler.call(:teaser_check_failed, "Teaser matched for #{name.inspect}, but full line did not:\n#{line.inspect}") end return false end else return false end end |