-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathus.rb
66 lines (58 loc) · 1.92 KB
/
us.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_relative 'base'
require_relative '../country_mappings'
class Parser::US < Parser::Base
def execute
# Parse the CSV
responses = transform
headers = responses[0].collect(&:downcase)
offset = 0
# Find the column where the data starts by going through all headers and
# trying to convert them into a date. If it works we have found our first
# date column, otherwise we increase the offset towards the first data column
headers.each do |header|
begin
Date.strptime(header, '%m/%d/%y')
rescue Date::Error, TypeError
offset += 1
end
end
# Skip the first row, which is headers not data
responses[1..-1].each do |row|
identifier = row[0]
iso_country = iso_countries_by_id[row[1]]
# Build a country entry in our result set, if that has not
# happend before (this is a precaution in case some datasets only
# contain death or recovery data)
unless results.has_key?(identifier)
results[identifier] = {
iso2: row[1],
iso3: row[2],
code3: row[3],
fips: row[4],
admin2: row[5],
province_state: row[6],
country: row[7],
iso_3166: {
country: 'United States',
country_code: row[1],
division_code: iso_country&.dig('divisions', row[6]),
},
coordinates: [row[8] == '0.0' ? nil : row[8].to_f, row[9] == '0.0' ? nil : row[9].to_f].compact,
combined_key: row[10],
dates: responses[0][offset..-1].map { |d| Date.strptime(d, '%m/%d/%y') },
data: {}
}
end
if headers.include?('population')
results[identifier][:population] = row[11]
end
# Add the actual numbers to the combined dataset
results[identifier][:data][@set] = row[offset..-1].map(&:to_i)
end
return results
end
private
def transform
CSV.parse(data)
end
end