class UniversalAccessLogParser
Public Class Methods
make_metods(names)
click to toggle source
# File lib/universal-access-log-parser.rb, line 320 def self.make_metods(names) names.each do |name| class_eval """ def #{name} return @cache[:#{name}] if @cache.member? :#{name} begin value = @parsers[:#{name}].call(@strings[:#{name}]) rescue => e raise ElementParsingError.new(e) end @cache[:#{name}] = value value end """ end end
name()
click to toggle source
# File lib/universal-access-log-parser.rb, line 316 def self.name superclass.name end
new(&block)
click to toggle source
# File lib/universal-access-log-parser.rb, line 299 def initialize(&block) @@parser_id ||= 0 @@parser_id += 1 @elements = ElementGroup::Root.new(' ', &block) @skip_lines = @elements.skip_lines.map{|s| Regexp.new(s)} @regexp = Regexp.new('^' + @elements.regexp + '$') @names = @elements.names @parsers = {} @names.zip(@elements.parsers).each do |name, parser| @parsers[name] = parser end @parsed_log_entry_class = Class.new(ParsedLogLine) do def self.name superclass.name end def self.make_metods(names) names.each do |name| class_eval """ def #{name} return @cache[:#{name}] if @cache.member? :#{name} begin value = @parsers[:#{name}].call(@strings[:#{name}]) rescue => e raise ElementParsingError.new(e) end @cache[:#{name}] = value value end """ end end def initialize(names, parsers, strings) @parsers = parsers @strings = {} names.zip(strings).each do |name, string| @strings[name] = string end @cache = {} end def parse! @strings.keys.each do |name| send(name) end self end def to_hash parse! @cache end def inspect hash = @cache.dup @strings.keys.each do |name| hash[name] = '<unparsed>' unless hash.member? name end "#<#{self.class.name}: #{hash.keys.map{|s| s.to_s}.sort.map{|name| "#{name}: #{hash[name.to_sym].inspect}"}.join(', ')}>" end def to_s "#<#{self.class.name}:#{object_id}>" end end @parsed_log_entry_class.make_metods(@names) end
parser(name, &block)
click to toggle source
custom parser definition
# File lib/universal-access-log-parser.rb, line 377 def self.parser(name, &block) ElementGroup.parser(name, &block) eval """ def self.#{name} self.new{ #{name} } end """ end
Public Instance Methods
inspect()
click to toggle source
# File lib/universal-access-log-parser.rb, line 360 def inspect hash = @cache.dup @strings.keys.each do |name| hash[name] = '<unparsed>' unless hash.member? name end "#<#{self.class.name}: #{hash.keys.map{|s| s.to_s}.sort.map{|name| "#{name}: #{hash[name.to_sym].inspect}"}.join(', ')}>" end
parse(line)
click to toggle source
# File lib/universal-access-log-parser.rb, line 394 def parse(line) begin matched, *strings = @regexp.match(line).to_a rescue ArgumentError => e raise ParsingError.new("parser regexp error: #{e}", self, line) end raise ParsingError.new('parser regexp did not match log line', self, line) if strings.empty? @parsed_log_entry_class.new(@names, @parsers, strings) end
parse!()
click to toggle source
# File lib/universal-access-log-parser.rb, line 348 def parse! @strings.keys.each do |name| send(name) end self end
parse_file(file_path)
click to toggle source
# File lib/universal-access-log-parser.rb, line 410 def parse_file(file_path) io = File.open(file_path) # io will be closed after each parse_io(io, true) end
parse_io(io, close_io = false)
click to toggle source
# File lib/universal-access-log-parser.rb, line 406 def parse_io(io, close_io = false) EntryIterator.new(self, io, close_io) end
skip?(line)
click to toggle source
# File lib/universal-access-log-parser.rb, line 387 def skip?(line) @skip_lines.each do |regexp| return true if line =~ regexp end return false end
to_hash()
click to toggle source
# File lib/universal-access-log-parser.rb, line 355 def to_hash parse! @cache end
to_s()
click to toggle source
# File lib/universal-access-log-parser.rb, line 368 def to_s "#<#{self.class.name}:#{object_id}>" end