-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.rb
executable file
·60 lines (54 loc) · 1.66 KB
/
parse.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
#!/usr/bin/env ruby
require 'rubygems'
require 'fastercsv'
require 'model'
FasterCSV.foreach("data/catcodes.csv", :headers => true) do |row|
BusinessCategory.create(
:id => row['code'],
:source => row['source'],
:name => row['name'],
:industry => row['industry'],
:order => row['order']
)
end
FasterCSV.foreach("data/contributions.csv", :headers => true) do |row|
business = Business.first_or_create(
:name => row["organization_name"] || row["contributor_employer"],
:business_category_id => row["contributor_category"] || ''
)
contributor = Contributor.first_or_create(
:name => row["contributor_name"],
:type => row["contributor_type"],
:address => row["contributor_address"],
:city => row["contributor_city"],
:state => row["contributor_state"],
:zipcode => row["contributor_zipcode"],
:occupation => row["contributor_occupation"] || '',
:business => business
)
recipient = Recipient.first_or_create(
:name => row["recipient_name"] || row["committee_name"],
:party => row["recipient_party"] || row["committee_party"],
:state => row["recipient_state"],
:election_type => row["election_type"],
:seat => row["seat"],
:district => row["district"] || ''
)
contribution = Contribution.create(
:amount => row["amount"],
:date => row["date"],
:contributor => contributor,
:recipient => recipient
)
end
# NONEXISTANT PARTIES WILL BE DELETED
Recipient.all(:party => nil).destroy
Contribution.all.each do |c|
c.destroy if c.recipient.nil?
end
Contributor.all.each do |c|
c.destroy if c.contributions.empty?
end
Business.all.each do |b|
b.destroy if b.contributors.empty?
end