Class: LogTool::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/logtool/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(blocks, options) ⇒ Query

Returns a new instance of Query.



3
4
5
6
7
# File 'lib/logtool/query.rb', line 3

def initialize(blocks, options)
  @blocks = blocks
  @filter = parse_filter(options[:mode_args])
  @target_data = parse_target_data(options[:mode_args])
end

Instance Method Details

#ip_regexpObject



25
26
27
# File 'lib/logtool/query.rb', line 25

def ip_regexp
  /\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
end

#keywordsObject



21
22
23
# File 'lib/logtool/query.rb', line 21

def keywords
  target_data_type + %W(ip and or not < > <= >= == != asset)
end

#parse_filter(options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/logtool/query.rb', line 29

def parse_filter(options)
  filter = []
  options.first.split.each do |word|
    if keywords.include?(word) 
      filter << word
    elsif word =~ ip_regexp 
      filter << "\"#{word}\""
    elsif word =~ /\d+/ 
      filter << word
    else
      LogTool::fatal_error("Invalid filter keyword: #{word}")
    end
  end
  filter.join(' ')
end

#parse_target_data(args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/logtool/query.rb', line 13

def parse_target_data(args)
  if target_data_type.include?(args[1])
    args[1]
  else
    LogTool::fatal_error("Undefined target data type: #{args[1]}")
  end
end

#runObject



45
46
47
# File 'lib/logtool/query.rb', line 45

def run
  @blocks.find_all{|b| b.test_for(@filter)}.each{|b| puts b.send(@target_data)}
end

#target_data_typeObject



9
10
11
# File 'lib/logtool/query.rb', line 9

def target_data_type
  %W(ip head tail method time)
end