-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathparse-postcodes.rb
More file actions
57 lines (42 loc) · 1.87 KB
/
Copy pathparse-postcodes.rb
File metadata and controls
57 lines (42 loc) · 1.87 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
require "optparse"
require "fileutils"
require "mechanize"
require_relative "lib/configuration"
class ParsePostcodes
def initialize(args)
@args = args
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: parse-postcodes.rb [--output-dir=PATH] [--morph-api-key=KEY]"
opts.on("--output-dir=PATH", "Write postcodes.csv to this dir instead of data/") do |path|
@options[:output_dir] = path
end
opts.on("--morph-api-key=KEY", "Override Morph.io API key") do |key|
@options[:morph_api_key] = key
end
end.parse!(@args)
end
def run
conf = Configuration.new
morph_api_key = @options[:morph_api_key] || conf.morph_api_key
output_dir = @options[:output_dir] || "data"
data_filename = "#{output_dir}/postcodes.csv"
puts "Fetching postcodes from morph.io ..."
puts "WARNING: morph_api_key is not set in configuration.yml! The api call will fail!" if morph_api_key.nil? || morph_api_key =~ /\AX*\z/
FileUtils.mkdir_p output_dir
sql_query = "SELECT DISTINCT postcode,COALESCE(NULLIF(electorate,''),redistributedElectorate) AS electorate FROM 'data' order by postcode,electorate"
url = "https://api.morph.io/drzax/morph-division-postcode-correspondence/data.csv"
agent = Mechanize.new
response = agent.get(url, { key: morph_api_key, query: sql_query })
raise "HTTP ERROR: #{response.code} fetching postcodes — check your morph.io API key" if response.code != "200"
raise "ERROR: Empty response — check your morph.io API key" if response.body.strip.empty?
puts "Saving to #{data_filename} ..."
File.write(data_filename, response.body)
puts "Done."
end
end
Configuration.report_errors { ParsePostcodes.new(ARGV).run } if $PROGRAM_NAME == __FILE__