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: CaptureDefiner, 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.



52
53
54
55
56
57
58
# File 'lib/request_log_analyzer/line_definition.rb', line 52

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

Instance Attribute Details

#capturesObject

Returns the value of attribute captures.



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

def captures
  @captures
end

#compoundObject

Returns the value of attribute compound.



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

def compound
  @compound
end

Returns the value of attribute footer.



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

def footer
  @footer
end

#headerObject Also known as: header?

Returns the value of attribute header.



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

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/request_log_analyzer/line_definition.rb', line 43

def name
  @name
end

#regexpObject

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

#teaserObject

Returns the value of attribute teaser.



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

def teaser
  @teaser
end

Class Method Details

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

Yields:

  • (definition)


60
61
62
63
64
# File 'lib/request_log_analyzer/line_definition.rb', line 60

def self.define(name, &_block)
  definition = new(name)
  yield(definition) if block_given?
  definition
end

Instance Method Details

#all_captured_variablesObject



74
75
76
# File 'lib/request_log_analyzer/line_definition.rb', line 74

def all_captured_variables
  captures.map { |c| c[:name] } + captures.map { |c| c[:provides] }.compact.map { |pr| pr.keys }.flatten
end

#capture(name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/request_log_analyzer/line_definition.rb', line 66

def capture(name)
  new_capture_hash = {}
  new_capture_hash[:name] = name
  new_capture_hash[:type] = :string
  captures << new_capture_hash
  CaptureDefiner.new(new_capture_hash)
end

#captures?(name) ⇒ Boolean

Returns true if this line captures values of the given name

Returns:

  • (Boolean)


134
135
136
# File 'lib/request_log_analyzer/line_definition.rb', line 134

def captures?(name)
  all_captured_variables.include?(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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/request_log_analyzer/line_definition.rb', line 114

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.is_a?(Hash) && capture[:provides].is_a?(Hash)
      capture[:provides].each do |name, type|
        value_hash[name] ||= request.convert_value(converted[name],  type: type)
      end
    end
  end
  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.



104
105
106
107
108
109
110
# File 'lib/request_log_analyzer/line_definition.rb', line 104

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.



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

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
rescue
  return false
end