Sha256: 762f4673828a1837835aede4129105cf6940041bef1d9f5c19722106ee6f54ba

Contents?: true

Size: 867 Bytes

Versions: 5

Compression:

Stored size: 867 Bytes

Contents

# convert date columns to numerics
require "csv"

class Masticate::Datify < Masticate::Base
  def datify(opts)
    standard_options(opts)

    field = opts[:field] or raise "missing field to datify"
    format = opts[:format] or raise "strptime format required for parsing timestamps"

    @output_count = 0
    headers = nil
    with_input do |input|
      while line = get
        row = CSV.parse_line(line, csv_options)
        if !headers
          headers = row
          index = headers.index(field) or raise "Unable to find column '#{field}'"
          emit(headers.to_csv)
        else
          row[index] = DateTime.strptime(row[index], format).to_time.to_i rescue nil
          emit(row.to_csv)
        end
      end
    end
    @output.close if opts[:output]

    {
      :input_count => @input_count,
      :output_count => @output_count
    }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
masticate-0.2 lib/masticate/datify.rb
masticate-0.1.5 lib/masticate/datify.rb
masticate-0.1.4 lib/masticate/datify.rb
masticate-0.1.3 lib/masticate/datify.rb
masticate-0.1.1 lib/masticate/datify.rb