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.



606
607
608
609
610
# File 'lib/td/command/table.rb', line 606

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

Instance Method Details

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



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
639
640
641
# File 'lib/td/command/table.rb', line 612

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



644
645
646
# File 'lib/td/command/table.rb', line 644

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