-
Notifications
You must be signed in to change notification settings - Fork 21
/
postcodes.rb
executable file
·54 lines (41 loc) · 1.51 KB
/
postcodes.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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Load the postcode data directly into the database
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
require "rubygems"
require "csv"
require "mysql2"
require "configuration"
require "people"
require "optparse"
# Defaults
options = { load_database: true }
OptionParser.new do |opts|
opts.banner = "Usage: postcodes.rb [--no-load]"
opts.on("--no-load", "Just generate XML and don't load up database") do |l|
options[:load_database] = l
end
end.parse!
def quote_string(text)
text.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end
data = CSV.readlines("data/postcodes.csv")
# Remove headers
data.shift
puts "Reading members data..."
people = PeopleCSVReader.read_members
all_members = people.all_periods_in_house(House.representatives)
# First check that all the constituencies are valid
constituencies = data.map { |row| row[1] }.uniq.reject(&:empty?)
constituencies.each do |constituency|
raise "Constituency #{constituency} not found" unless all_members.any? { |m| m.division == constituency }
end
if options[:load_database]
conf = Configuration.new
db = Mysql2::Client.new(host: conf.database_host, username: conf.database_user, password: conf.database_password,
database: conf.database_name)
# Clear out the old data
db.query("DELETE FROM postcode_lookup")
values = data.map { |row| "('#{row[0]}', '#{quote_string(row[1])}')" }.join(",")
db.query("INSERT INTO postcode_lookup (postcode, name) VALUES #{values}")
end