Class: TreasureData::Command::JsonParser

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

Instance Method Summary collapse

Constructor Details

#initialize(time_key) ⇒ JsonParser

Returns a new instance of JsonParser.



665
666
667
668
# File 'lib/td/command/table.rb', line 665

def initialize(time_key)
  require 'json'
  @time_key = time_key
end

Instance Method Details

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



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/td/command/table.rb', line 670

def call(file, path, &block)
  i = 0
  file.each_line {|line|
    i += 1
    begin
      record = JSON.parse(line)

      unless record.is_a?(Hash)
        raise "record must be a Hash"
      end

      time = record[@time_key]
      unless time
        raise "record doesn't have '#{@time_key}' column"
      end

      case time
      when Integer
        # do nothing
      else
        time = Time.parse(time.to_s).to_i
      end
      record['time'] = time

      block.call(record)

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