Sha256: d23b7b6ad4f25770dc55cc04f8743925da18eb17eb433de3ddbc9ea07c1fea86

Contents?: true

Size: 841 Bytes

Versions: 3

Compression:

Stored size: 841 Bytes

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
    return unless airport_data = parsed_data.fetch(iata_code, nil)

    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", __FILE__)
  end
  private_class_method :data_path
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
airports-1.0.0 lib/airports.rb
airports-0.0.2 lib/airports.rb
airports-0.0.1 lib/airports.rb