Class: TreasureData::Command::TextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/td/command/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(names, regexp, time_format) ⇒ TextParser



603
604
605
606
607
# File 'lib/td/command/table.rb', line 603

def initialize(names, regexp, time_format)
  @names = names
  @regexp = regexp
  @time_format = time_format
end

Instance Method Details

#call(file, path, &block) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/td/command/table.rb', line 609

def call(file, path, &block)
  i = 0
  file.each_line {|line|
    i += 1
    begin
      line.rstrip!
      m = @regexp.match(line)
      unless m
        raise "invalid log format at #{path}:#{i}"
      end

      record = {}

      cap = m.captures
      @names.each_with_index {|name,cap_i|
        if value = cap[cap_i]
          if name == "time"
            value = parse_time(value).to_i
          end
          record[name] = value
        end
      }

      block.call(record)

    rescue
      $stderr.puts "  skipped: #{$!}: #{line.dump}"
    end
  }
end

#parse_time(value) ⇒ Object



641
642
643
# File 'lib/td/command/table.rb', line 641

def parse_time(value)
  Time.strptime(value, @time_format)
end