Class: LogTool::Query
- Inherits:
-
Object
- Object
- LogTool::Query
- Defined in:
- lib/logtool/query.rb
Instance Method Summary collapse
-
#initialize(blocks, options) ⇒ Query
constructor
A new instance of Query.
- #ip_regexp ⇒ Object
- #keywords ⇒ Object
- #parse_filter(options) ⇒ Object
- #parse_target_data(args) ⇒ Object
- #run ⇒ Object
- #target_data_type ⇒ Object
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, ) @blocks = blocks @filter = parse_filter([:mode_args]) @target_data = parse_target_data([:mode_args]) end |
Instance Method Details
#ip_regexp ⇒ Object
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 |
#keywords ⇒ Object
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() filter = [] .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 |
#run ⇒ Object
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_type ⇒ Object
9 10 11 |
# File 'lib/logtool/query.rb', line 9 def target_data_type %W(ip head tail method time) end |