Class: St2csv::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/st2csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_string) ⇒ Converter

Returns a new instance of Converter.



8
9
10
# File 'lib/st2csv.rb', line 8

def initialize(json_string)
  @json_string = json_string
end

Instance Method Details

#convertObject



12
13
14
15
16
17
18
19
20
21
22
23
# 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"].sub('-','00'),
              hash["TRAFFIC_COUNT"]
            ]
  	end
	end
  csv_string
end