Sha256: 0ac12c2a308a8793af0e3b8dac88bfb9c8ee17fac77a331091f310c967c15e25

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

module VatsimTools

  class StationParser

    %w{tmpdir csv}.each { |lib| require lib }
    require_relative "data_downloader"
    require_relative "station"

    attr_accessor :role
    attr_accessor :icao

    LOCAL_DATA = "#{Dir.tmpdir}/vatsim_data.txt"

    def initialize(icao, args = nil)
      VatsimTools::DataDownloader.new
      args.class == Hash ? @role = determine_role(args) : @role = "all"
      @icao = icao.upcase
    end

    def determine_role(args)
      args[:atc] == false ? role = "pilot" : role = "all"
      args[:pilots] == false ? role = "atc" : role = role
      role = "all" if args[:pilots] == false && args[:atc] == false
      role
    end

    def stations
      stations = []
      CSV.foreach(LOCAL_DATA, :col_sep =>':') do |row|
        callsign, origin, destination, client = row[0].to_s, row[11].to_s, row[13].to_s, row[3].to_s
        stations << row if (callsign[0...@icao.length] == @icao && client == "ATC") unless @role == "pilot"
        stations << row if (origin[0...@icao.length] == @icao || destination[0...@icao.length] == @icao) unless @role == "atc"
      end
      stations
    end

    def station_objects
      station_objects= []
      stations.each {|station| station_objects << VatsimTools::Station.new(station) }
      station_objects
    end

    def sorted_station_objects
      atc = []; pilots = []; arrivals = []; departures = []
      station_objects.each {|sobj| sobj.role == "ATC" ? atc << sobj : pilots << sobj}
      pilots.each {|p| p.origin[0...@icao.length] == @icao ? departures << p : arrivals << p }
      {:atc => atc, :pilots => pilots, :arrivals => arrivals, :departures => departures}
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vatsim_online-0.4 lib/vatsim_online/station_parser.rb
vatsim_online-0.3 lib/vatsim_online/station_parser.rb