Class: RequestLogAnalyzer::Filter::Field

Inherits:
Base
  • Object
show all
Defined in:
lib/request_log_analyzer/filter/field.rb

Overview

Filter to select or reject a specific field Options

  • :mode :reject or :accept.

  • :field Specific field to accept or reject.

  • :value Value that the field should match to be accepted or rejected.

Instance Attribute Summary collapse

Attributes inherited from Base

#file_format, #options

Instance Method Summary collapse

Constructor Details

#initialize(file_format, options = {}) ⇒ Field

Returns a new instance of Field.



12
13
14
15
# File 'lib/request_log_analyzer/filter/field.rb', line 12

def initialize(file_format, options = {})
  super(file_format, options)
  setup_filter
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



10
11
12
# File 'lib/request_log_analyzer/filter/field.rb', line 10

def field
  @field
end

#modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/request_log_analyzer/filter/field.rb', line 10

def mode
  @mode
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/request_log_analyzer/filter/field.rb', line 10

def value
  @value
end

Instance Method Details

#filter(request) ⇒ Object

Keep request if @mode == :select and request has the field and value. Drop request if @mode == :reject and request has the field and value. Returns nil otherwise. request Request Object



34
35
36
37
38
39
# File 'lib/request_log_analyzer/filter/field.rb', line 34

def filter(request)
  found_field = request.every(@field).any? { |value| @value === value.to_s }      
  return nil if !found_field && @mode == :select
  return nil if found_field && @mode == :reject
  return request
end

#setup_filterObject

Setup mode, field and value.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/request_log_analyzer/filter/field.rb', line 18

def setup_filter
  @mode = (@options[:mode] || :accept).to_sym
  @field = @options[:field].to_sym
  
  # Convert the timestamp to the correct formats for quick timestamp comparisons
  if @options[:value].kind_of?(String) && @options[:value][0, 1] == '/' && @options[:value][-1, 1] == '/'
    @value = Regexp.new(@options[:value][1..-2])
  else
    @value = @options[:value] # TODO: convert value?
  end
end