Class: TreasureData::Command::JsonParser

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

Instance Method Summary collapse

Constructor Details

#initialize(time_key) ⇒ JsonParser

Returns a new instance of JsonParser.



238
239
240
241
# File 'lib/td/command/import.rb', line 238

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

Instance Method Details

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/td/command/import.rb', line 243

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