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.



570
571
572
573
574
# File 'lib/td/command/table.rb', line 570

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

Instance Method Details

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



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/td/command/table.rb', line 576

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



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

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