Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created May 14, 2010 09:28
Show Gist options
  • Save follesoe/400981 to your computer and use it in GitHub Desktop.
Save follesoe/400981 to your computer and use it in GitHub Desktop.
public class AirlineNamesService
{
private readonly Uri _serviceUrl;
public AirlineNamesService()
{
_serviceUrl = new Uri("http://flydata.avinor.no/airlineNames.asp");
}
public IObservable<Airline> GetAirlines()
{
return WebRequestFactory.GetData(_serviceUrl, ParseAirlineXml);
}
private static IEnumerable<Airline> ParseAirlineXml(XmlReader reader)
{
var xml = XDocument.Load(reader);
return from airlineNames in xml.Elements("airlineNames")
from airline in airlineNames.Elements("airlineName")
select new Airline
{
Code = airline.Attribute("code").Value,
Name = airline.Attribute("name").Value
};
}
}
public class AirportNamesService
{
private readonly Uri _serviceUrl;
public AirportNamesService()
{
_serviceUrl = new Uri("http://flydata.avinor.no/airportNames.asp");
}
public IObservable<Airport> GetAirports()
{
return WebRequestFactory.GetData(_serviceUrl, ParseAirportXml);
}
private static IEnumerable<Airport> ParseAirportXml(XmlReader reader)
{
var xml = XDocument.Load(reader);
return from airportNames in xml.Elements("airportNames")
from airport in airportNames.Elements("airportName")
select new Airport
{
Code = airport.Attribute("code").Value,
Name = airport.Attribute("name").Value
};
}
}
public class FlightsService
{
private readonly string _serviceUrl;
private readonly string _direction;
private readonly string _lastUpdate;
private Dictionary<string, Airport> _airports;
private Dictionary<string, Airline> _airlines;
private Dictionary<string, FlightStatus> _statuses;
public FlightsService()
{
_serviceUrl = "http://flydata.avinor.no/XmlFeed.asp?TimeFrom={0}&TimeTo={1}&airport={2}";
_lastUpdate = "&lastUpdate=2009-03-10T15:03:00";
_direction = "&direction={0}";
_airports = new Dictionary<string, Airport>();
_airlines = new Dictionary<string, Airline>();
}
public IObservable<Flight> GetFlightsFrom(Airport a)
{
var airports = new AirportNamesService().GetAirports();
var airlines = new AirlineNamesService().GetAirlines();
var statuses = new FlightStatusesService().GetStautses();
string url = string.Format(_serviceUrl, 1, 7, a.Code);
return GetFlightsFrom(url);
}
private static IObservable<Flight> GetFlightsFrom(string url)
{
return WebRequestFactory.GetData(new Uri(url), ParseFlightsXml);
}
private static IEnumerable<Flight> ParseFlightsXml(XmlReader reader)
{
var xml = XDocument.Load(reader);
return from airport in xml.Elements("airport")
from flights in airport.Elements("flights")
from flight in flights.Elements("flight")
select new Flight
{
UniqueId = flight.AttributeValueOrEmpty("uniqueID"),
Gate = flight.ElementValueOrEmpty("gate"),
Direction = ConvertToDirection(flight.Element("arr_dep")),
LastLeg = ConvertToFlightArea(flight.Element("dom_int")),
ScheduledTime = flight.ElementAsDateTime("schedule_time")
};
}
private static Direction ConvertToDirection(XElement element)
{
if(element == null) return Direction.Depature;
return element.Value == "D" ? Direction.Depature : Direction.Arrival;
}
private static FlightArea ConvertToFlightArea(XElement element)
{
if(element == null) return FlightArea.Domestic;
if (element.Value == "S") return FlightArea.Schengen;
if (element.Value == "I") return FlightArea.International;
return FlightArea.Domestic;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment