Sha256: 8d33ed343ca857a05266c472026d6a7b475832e257986ba944d82c89540b2e91

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require "json"
require "airports/version"
require "airports/airport"

module Airports
  def self.find_by_iata_code(iata_code)
    return unless iata_code.length == 3
    airport_data = parsed_data.fetch(iata_code, nil)

    return unless airport_data

    Airport.
      new(airport_data.each_with_object({}) { |(k, v), h| h[k.to_sym] = v })
  end

  def self.find_by_icao_code(icao_code)
    return unless icao_code.length == 4

    airport_data = parsed_data.values.find do |data|
      data["icao"] == icao_code
    end

    return unless airport_data

    Airport.
      new(airport_data.each_with_object({}) { |(k, v), h| h[k.to_sym] = v })
  end

  def self.iata_codes
    parsed_data.keys
  end

  def self.all
    @all ||= parsed_data.map do |_iata_code, airport_data|
      Airport.
        new(airport_data.each_with_object({}) { |(k, v), h| h[k.to_sym] = v })
    end
  end

  def self.parsed_data
    @parsed_data ||= JSON.parse(data)
  end

  def self.data
    @data ||= File.read(data_path)
  end
  private_class_method :data

  def self.data_path
    File.expand_path("../data/airports.json", __dir__)
  end
  private_class_method :data_path
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
airports-1.3.0 lib/airports.rb
airports-1.2.0 lib/airports.rb