forked from thecristen/phone-your-rep-api
/
db_pyr_update.rb
248 lines (220 loc) · 7.31 KB
/
db_pyr_update.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# frozen_string_literal: true
require_relative '../config/environment'
require 'governor_scraper'
module DbPyrUpdate
class Base
def initialize(file_name)
File.open file_name do |file|
@reps = YAML.safe_load(file)
end
end
end
class Governors
def call
GovernorScraper.scrape
GovernorScraper.governors.each do |gov|
state = State.find_by(name: gov.state_name)
db_gov = Governor.find_or_create_by(official_full: gov.official_full, state: state)
update_basic_info(db_gov, gov)
db_gov.add_photo
db_gov.active = true
db_gov.save
gov.office_locations.each { |off| update_office(db_gov, off) }
puts "Updated #{db_gov.official_full}"
end
end
def update_office(db_gov, off)
o = OfficeLocation.find_or_create_by(rep: db_gov, address: off[:address])
o.city = off[:city]
o.state = off[:state]
o.zip = off[:zip]
o.phone = off[:phone]
o.fax = off[:fax]
o.office_type = off[:office_type]
o.active = true
o.save
o.add_v_card
end
def update_basic_info(db_gov, gov)
db_gov.photo_url = gov.photo_url
db_gov.url = gov.url
db_gov.party = gov.party
db_gov.first = gov.first
db_gov.last = gov.last
db_gov.middle = gov.middle
db_gov.nickname = gov.nickname
db_gov.suffix = gov.suffix
end
end
class Reps < Base
def call
@reps.each do |yaml_rep|
db_rep = CongressionalRep.find_or_create_by(official_id: yaml_rep['id']['bioguide'])
update_rep(db_rep, yaml_rep)
puts "Updated #{db_rep.official_full}"
end
end
private
def update_rep(db_rep, yaml_rep)
name = yaml_rep['name']
term = yaml_rep['terms'].last
db_rep.tap do |rep|
update_rep_name(rep, name)
update_rep_term_info(rep, term)
rep.add_photo
update_rep_capitol_office(rep, term)
rep.active = true
end
db_rep.save
end
def update_rep_name(rep, name)
rep.official_full = name['official_full']
rep.first = name['first']
rep.middle = name['middle']
rep.last = name['last']
rep.suffix = name['suffix']
rep.nickname = name['nickname']
end
def update_rep_term_info(rep, term)
dis_code = format('%d', term['district']) if term['district']
dis_code = dis_code.size == 1 ? "0#{dis_code}" : dis_code if dis_code
rep.chamber = determine_current_rep_chamber(term)
rep.state = State.find_by(abbr: term['state'])
rep.district = CongressionalDistrict.where(code: dis_code, state: rep.state).take
rep.party = term['party']
rep.url = term['url']
rep.contact_form = term['contact_form']
rep.senate_class = format('0%o', term['class']) if term['class']
end
def determine_current_rep_chamber(term)
case term['type']
when 'sen' then 'upper'
when 'rep' then 'lower'
else term['type']
end
end
def update_rep_capitol_office(rep, term)
address_ary = term['address'].split(' ')
cap_office = OfficeLocation.find_or_create_by(
office_type: 'capitol',
official_id: rep.official_id
)
cap_office.tap do |off|
update_basic_office_info(off, rep)
update_phone_fax_and_hours(off, term)
update_cap_office_address(address_ary, off)
end
cap_office.add_v_card
end
def update_basic_office_info(off, rep)
off.office_id = "#{rep.official_id}-capitol"
off.rep = rep
off.active = true
end
def update_phone_fax_and_hours(off, term)
off.phone = term['phone']
off.fax = term['fax']
off.hours = term['hours']
end
def update_cap_office_address(address_ary, off)
off.zip = address_ary.pop
off.state = address_ary.pop
off.city = address_ary.pop
off.address = address_ary.
join(' ').
delete(';').
sub('HOB', 'House Office Building')
end
# End of private methods
end
class HistoricalReps < Base
def call
bioguide_ids = @reps.map { |h_rep| h_rep['id']['bioguide'] }
CongressionalRep.where(official_id: bioguide_ids).each do |rep|
rep.update(active: false)
rep.office_locations.each { |office| office.update(active: false) }
puts "Retired #{rep.official_full}"
end
end
end
class Socials < Base
def call
@reps.each do |social|
rep = CongressionalRep.find_or_create_by(official_id: social['id']['bioguide'])
update_rep_socials(rep, social)
rep.save
puts "Updated socials for #{rep.official_full}"
end
end
private
def update_rep_socials(rep, social)
update_facebook(rep, social)
update_twitter(rep, social)
update_youtube(rep, social)
update_instagram(rep, social)
rep.googleplus = social['social']['googleplus']
end
def update_instagram(rep, social)
rep.instagram = social['social']['instagram']
rep.instagram_id = social['social']['instagram_id']
end
def update_youtube(rep, social)
rep.youtube = social['social']['youtube']
rep.youtube_id = social['social']['youtube_id']
end
def update_twitter(rep, social)
rep.twitter = social['social']['twitter']
rep.twitter_id = social['social']['twitter_id']
end
def update_facebook(rep, social)
rep.facebook = social['social']['facebook']
rep.facebook_id = social['social']['facebook_id']
end
# End of private methods
end
class OfficeLocations < Base
def call
@active_offices = []
@reps.each do |yaml_office|
next if yaml_office['offices'].blank?
find_or_create_offices(yaml_office)
end
district_offices = OfficeLocation.where(office_type: 'district').map(&:id)
inactive_offices = district_offices - @active_offices
OfficeLocation.find(inactive_offices).each { |o| o.update(active: false) }
end
private
def find_or_create_offices(yaml_office)
yaml_office['offices'].each do |yaml_off|
office = OfficeLocation.find_or_create_by(
official_id: yaml_office['id']['bioguide'],
office_id: yaml_off['id'],
office_type: 'district'
)
update_location_info(office, yaml_off)
update_other_office_info(office, yaml_off)
@active_offices << office.id
puts "Updated #{office.rep.official_full}'s #{office.city} office"
end
end
def update_location_info(office, yaml_off)
office.office_id = yaml_off['id']
office.suite = yaml_off['suite']
office.phone = yaml_off['phone']
office.address = yaml_off['address']
office.building = yaml_off['building']
office.city = yaml_off['city']
office.state = yaml_off['state']
office.zip = yaml_off['zip']
office.latitude = yaml_off['latitude']
office.longitude = yaml_off['longitude']
end
def update_other_office_info(office, yaml_off)
office.fax = yaml_off['fax']
office.hours = yaml_off['hours']
office.active = true
office.add_v_card
end
# End of private methods
end
end