class St2csv::Converter

Public Class Methods

new(json_string) click to toggle source
# File lib/st2csv.rb, line 8
def initialize(json_string)
  @json_string = json_string
end

Public Instance Methods

convert() click to toggle source
# File lib/st2csv.rb, line 12
def convert
    csv_string = CSV.generate(options = {force_quotes: true}) do |csv|
    # csv << ["datetime","traffic_loop_id","traffic_count"]
    JSON.parse(@json_string).each do |hash|
      csv << [hash["DATETIME"].scan(/(\d{4}-\d{1,2}-\d{1,2}).([\d:]*)/).join(' '),
              hash["LOOP_ID"],
              hash["TRAFFIC_COUNT"]
            ]
    end
    end
  csv_string
end