-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-data.rb
executable file
·36 lines (25 loc) · 1 KB
/
process-data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env ruby
# Usage: [ruby] ./process-data.rb [source_dir [output_dir]]
#
# Default source_dir is './data'. Default output_dir is '(source_dir).processed'.
require 'json'
require 'time'
require 'fileutils'
SOURCE_DIR = File.join('.', ARGV.shift || 'data')
OUTPUT_DIR = ARGV.shift || SOURCE_DIR + '.processed'
FileUtils.mkdir_p(OUTPUT_DIR)
Dir.glob(File.join(SOURCE_DIR, '*.json')).each do |file_path|
puts "Processing #{file_path}."
json_str = File.read(file_path)
hash = JSON.parse(json_str)
# Add identity based on the data file's name so we can have something
# to use for uniquely identifying the entries.
hash['identity'] = File.basename(file_path).gsub(/_.*/, '')
# Convert 'datetime' values to floating-point.
hash['datetime'] = Time.parse(hash['datetime']).strftime('%s.%N').to_f
new_json_str = JSON.generate(hash)
new_file_path = File.join(OUTPUT_DIR, File.basename(file_path))
puts "Saving data to #{new_file_path}."
File.write(new_file_path, new_json_str)
end
puts "Done."