Sha256: 1a16666c78e77b00586ccce0850a2d18038e3a6c7ae5c84c399841ff7562ba01

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# 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

task :update do
  raw_data = 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
airports-1.6.1 Rakefile
airports-1.6.0 Rakefile