-
Notifications
You must be signed in to change notification settings - Fork 20
/
Rakefile
52 lines (42 loc) · 1.46 KB
/
Rakefile
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
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "csv"
require "json"
require "open-uri"
RSpec::Core::RakeTask.new(:spec)
def check_for_empty_data(field)
# OpenFlights uses "\\N" (i.e. a literal backslash followed by a capital N) to
# indicate a null field.
if field == "\\N"
nil
else
field
end
end
desc "Updates the airports data file based on the OpenFlights source and our patches"
task :update do
raw_data = URI.open("https://raw.githubusercontent.com/jpatokal/openflights/master/" \
"data/airports.dat").read + File.read("./data/patches.dat")
cleaned_data = raw_data.gsub(/\\"/, '""')
cleaned_data = CSV.parse(cleaned_data).each_with_object({}) do |row, accumulator|
iata_code = row[4]
# We'll skip other airports which don't have IATA codes
next unless iata_code != "\\N"
accumulator[iata_code] = {
name: check_for_empty_data(row[1]),
city: check_for_empty_data(row[2]),
country: check_for_empty_data(row[3]),
iata: iata_code,
icao: check_for_empty_data(row[5]),
latitude: check_for_empty_data(row[6]),
longitude: check_for_empty_data(row[7]),
altitude: check_for_empty_data(row[8]),
timezone: check_for_empty_data(row[9]),
dst: check_for_empty_data(row[10]),
tz_name: check_for_empty_data(row[11]),
}
end
File.open("data/airports.json", "w").puts JSON.generate(cleaned_data)
end
task default: :spec