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

Returns a new instance of TextParser.



616
617
618
619
620
# File 'lib/td/command/table.rb', line 616

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

Instance Method Details

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



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/td/command/table.rb', line 622

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



654
655
656
# File 'lib/td/command/table.rb', line 654

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