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.



213
214
215
216
# File 'lib/td/command/import.rb', line 213

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

Instance Method Details

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/td/command/import.rb', line 218

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