Class: RequestLogAnalyzer::LineDefinition

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
# File 'lib/request_log_analyzer/line_definition.rb', line 38

def initialize(name, definition = {})
  @name     = name
  @captures = []
  definition.each { |key, value| self.send("#{key.to_s}=".to_sym, value) }
end

Instance Attribute Details

#capturesObject

Returns the value of attribute captures.



30
31
32
# File 'lib/request_log_analyzer/line_definition.rb', line 30

def captures
  @captures
end

Returns the value of attribute footer.



31
32
33
# File 'lib/request_log_analyzer/line_definition.rb', line 31

def footer
  @footer
end

#headerObject 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

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/request_log_analyzer/line_definition.rb', line 29

def name
  @name
end

#regexpObject

Returns the value of attribute regexp.



30
31
32
# File 'lib/request_log_analyzer/line_definition.rb', line 30

def regexp
  @regexp
end

#teaserObject

Returns the value of attribute teaser.



30
31
32
# File 'lib/request_log_analyzer/line_definition.rb', line 30

def teaser
  @teaser
end

Class Method Details

.define(name) {|definition| ... } ⇒ Object

Yields:

  • (definition)


44
45
46
47
48
# File 'lib/request_log_analyzer/line_definition.rb', line 44

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/request_log_analyzer/line_definition.rb', line 104

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/request_log_analyzer/line_definition.rb', line 84

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.



74
75
76
77
78
79
80
# File 'lib/request_log_analyzer/line_definition.rb', line 74

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/request_log_analyzer/line_definition.rb', line 55

def matches(line, &warning_handler)
  if @teaser.nil? || @teaser =~ line
    if match_data = line.match(@regexp)
      return { :line_definition => self, :captures => match_data.captures}
    else
      if @teaser && 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