-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.rb
52 lines (45 loc) · 1.56 KB
/
global.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
require_relative 'base'
require_relative '../country_mappings'
class Parser::Global < Parser::Base
def execute
# Parse the CSV
responses = transform
# Skip the first row, which is headers not data
responses[1..-1].each do |row|
# Build an identifier digest to ensure we talk about the same rows across
# all three datasets, this contains:
# - area name
# - country name
# - latitude
# - longitude
identifier = Digest::SHA512.hexdigest("#{row[0]}|#{row[1]}|#{row[2]}|#{row[3]}")
# 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)
iso_country = iso_countries_by_name[row[1]] || iso_countries_by_name[MANUAL_COUNTRY_MAPPINGS[row[1]]]
results[identifier] = {
province_state: row[0],
country: row[1],
lat: row[2],
long: row[3],
iso_3166: {
country: iso_country&.dig('name'),
country_code: iso_country&.dig('code'),
division_code: iso_country&.dig(row[1], 'divisions', row[0]),
},
coordinates: [row[2].to_f, row[3].to_f],
dates: responses[0][4..-1].map { |d| Date.strptime(d, '%m/%d/%y') },
data: {}
}
end
# Add the actual numbers to the combined dataset
results[identifier][:data][@set] = row[4..-1].map(&:to_i)
end
return results
end
private
def transform
CSV.parse(data)
end
end