forked from NathanielWroblewski/data-for-democracy-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_json.rb
66 lines (55 loc) · 1.94 KB
/
to_json.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 'csv'
require 'json'
require 'active_support/inflector'
TIME_FRAME = 2012..2016
contributions = {}
TIME_FRAME.each do |year|
contributions[year] = {
nodes: [],
links: [],
recipients: {},
lobbyists: {},
employers: {},
contributions: {}
}
end
CSV.foreach('./flattened.csv', headers: true, header_converters: :symbol) do |row|
next if row[:recipient] == 'NA'
year = row[:date].split('/').last.to_i
recipient = "#{row[:recipient].titleize} (Recipient)"
lobbyist = "#{row[:lobbyist].titleize} (Lobbyist)"
employer = "#{row[:employer].titleize} (Funder)"
amount = row[:amount].delete('$').delete(',').to_i
if TIME_FRAME.include?(year) && amount > 0 && lobbyist && employer
contributions[year][:recipients][recipient] = true
contributions[year][:lobbyists][lobbyist] = true
contributions[year][:employers][employer] = true
contributions[year][:contributions][lobbyist] ||= Hash.new(0)
contributions[year][:contributions][lobbyist][recipient] += amount
contributions[year][:contributions][employer] ||= Hash.new(0)
contributions[year][:contributions][employer][lobbyist] += amount
end
end
contributions.each do |year, contributions_for_year|
contributions_for_year[:recipients].each do |recipient, value|
contributions_for_year[:nodes] << { id: recipient, group: 1 }
end
contributions_for_year[:lobbyists].each do |lobbyist, value|
contributions_for_year[:nodes] << { id: lobbyist, group: 2 }
end
contributions_for_year[:employers].each do |employer, value|
contributions_for_year[:nodes] << { id: employer, group: 3 }
end
contributions_for_year[:contributions].each do |source, contribution|
contribution.each do |target, amount|
contributions_for_year[:links] << {
source: source,
target: target,
value: amount
}
end
end
end
File.open('./public/javascripts/datasets/data.json', 'w') do |file|
file.write(contributions.to_json)
end