forked from openaustralia/openaustralia-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postcodes.rb
executable file
·39 lines (29 loc) · 1.1 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
#!/usr/bin/env ruby
# Load the postcode data directly into the database
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'rubygems'
require 'csv'
require 'mysql'
require 'configuration'
require 'people'
conf = Configuration.new
def quote_string(s)
s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end
data = CSV.readlines("data/postcodes.csv")
# Remove the first two elements
data.shift
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
constituencies.each do |constituency|
throw "Constituency #{constituency} not found" unless all_members.any? {|m| m.division == constituency}
end
db = Mysql.real_connect(conf.database_host, conf.database_user, conf.database_password, 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}")