Skip to content

Commit

Permalink
Factor out London Terminals so they can be returned by the CRS API
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsingleton committed May 14, 2015
1 parent 923b7f5 commit c3d8594
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/Huxley/Controllers/CrsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public IEnumerable<CrsRecord> Get() {

// GET /crs/{query}
public IEnumerable<CrsRecord> Get(string query) {
if (query.Equals("London Terminals", StringComparison.InvariantCultureIgnoreCase)) {
return HuxleyApi.LondonTerminals;
}
// Could use a RegEx here but putting user input into a RegEx can be dangerous
var results = HuxleyApi.CrsCodes.Where(c => c.StationName.IndexOf(query, StringComparison.InvariantCultureIgnoreCase) >= 0);
return results;
Expand Down
27 changes: 2 additions & 25 deletions src/Huxley/Controllers/DelaysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,6 @@ public async Task<DelaysResponse> Get([FromUri] StationBoardRequest request) {
}
}

// https://en.wikipedia.org/wiki/London_station_group
// Farringdon [ZFD] is not a London Terminal but it probably should be (maybe when Crossrail opens it will be)
var londonTerminals = new List<string> {
"BFR", // Blackfriars
"CST", // Cannon Street
"CHX", // Charing Cross
"CTX", // City Thameslink
"EUS", // Euston
"FST", // Fenchurch Street
"KGX", // King's Cross
"LST", // Liverpool Street
"LBG", // London Bridge
"MYB", // Marylebone
"MOG", // Moorgate
"OLD", // Old Street
"PAD", // Paddington
"STP", // St. Pancras
"VXH", // Vauxhall
"VIC", // Victoria
"WAT", // Waterloo
"WAE", // Waterloo East
};

var totalDelayMinutes = 0;
var delayedTrains = new List<ServiceItem>();

Expand All @@ -113,10 +90,10 @@ public async Task<DelaysResponse> Get([FromUri] StationBoardRequest request) {
// Could query for every terminal or get service for every train and check calling points. Very chatty either way.
switch (request.FilterType) {
case FilterType.to:
trainServices = trainServices.Where(ts => ts.destination.Any(d => londonTerminals.Contains(d.crs.ToUpperInvariant()))).ToArray();
trainServices = trainServices.Where(ts => ts.destination.Any(d => HuxleyApi.LondonTerminals.Any(lt => lt.CrsCode == d.crs.ToUpperInvariant()))).ToArray();
break;
case FilterType.from:
trainServices = trainServices.Where(ts => ts.origin.Any(d => londonTerminals.Contains(d.crs.ToUpperInvariant()))).ToArray();
trainServices = trainServices.Where(ts => ts.origin.Any(o => HuxleyApi.LondonTerminals.Any(lt => lt.CrsCode == o.crs.ToUpperInvariant()))).ToArray();
break;
default:
throw new ArgumentOutOfRangeException();
Expand Down
26 changes: 26 additions & 0 deletions src/Huxley/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class HuxleyApi : HttpApplication {
// Singleton to store the station name to CRS lookup
public static IList<CrsRecord> CrsCodes { get; private set; }

// Singleton to store the London Terminals CRS lookup
public static IList<CrsRecord> LondonTerminals { get; private set; }

// Singleton to store the Huxley settings
public static HuxleySettings Settings { get; private set; }

Expand All @@ -59,6 +62,29 @@ protected void Application_Start() {

// Set the CRS dictionary passing in embedded CRS path
CrsCodes = GetCrsCodes(Server.MapPath("~/RailReferences.csv")).Result;

// https://en.wikipedia.org/wiki/London_station_group
// Farringdon [ZFD] is not a London Terminal but it probably should be (maybe when Crossrail opens it will be)
LondonTerminals = new List<CrsRecord> {
new CrsRecord {CrsCode = "BFR", StationName = "London Blackfriars",},
new CrsRecord {CrsCode = "CST", StationName = "London Cannon Street",},
new CrsRecord {CrsCode = "CHX", StationName = "London Charing Cross",},
new CrsRecord {CrsCode = "CTX", StationName = "City Thameslink",},
new CrsRecord {CrsCode = "EUS", StationName = "London Euston",},
new CrsRecord {CrsCode = "FST", StationName = "London Fenchurch Street",},
new CrsRecord {CrsCode = "KGX", StationName = "London Kings Cross",},
new CrsRecord {CrsCode = "LST", StationName = "London Liverpool Street",},
new CrsRecord {CrsCode = "LBG", StationName = "London Bridge",},
new CrsRecord {CrsCode = "MYB", StationName = "London Marylebone",},
new CrsRecord {CrsCode = "MOG", StationName = "Moorgate",},
new CrsRecord {CrsCode = "OLD", StationName = "Old Street",},
new CrsRecord {CrsCode = "PAD", StationName = "London Paddington",},
new CrsRecord {CrsCode = "STP", StationName = "London St Pancras International",},
new CrsRecord {CrsCode = "VXH", StationName = "Vauxhall",},
new CrsRecord {CrsCode = "VIC", StationName = "London Victoria",},
new CrsRecord {CrsCode = "WAT", StationName = "London Waterloo",},
new CrsRecord {CrsCode = "WAE", StationName = "London Waterloo East",},
};
}

protected void Application_BeginRequest(object sender, EventArgs e) {
Expand Down
79 changes: 78 additions & 1 deletion src/Huxley/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ <h4>Delays</h4>
}
</pre>
<p>
Additionally, this action will accept <code>LON</code> or <code>London</code> as a filter CRS to find trains going to or coming from any London terminal.
Additionally, this action will accept <code>LON</code> or <code>London</code> as a filter CRS to find trains going to or coming from any of the <a href="http://www.nationalrail.co.uk/times_fares/ticket_types/46587.aspx#terminals">London Terminals</a>.
</p>
<p>
You can also pass in a comma separated list of 24 hour train times to filter on (e.g. <code>/delays/btn/to/lon/50/0729,0744,0748</code>).
Expand All @@ -161,6 +161,83 @@ <h4>CRS Station Codes</h4>
"crsCode": "LZB"
}
]
</pre>
<p><code><a href="/crs/london terminals">/crs/london terminals</a></code> returns all codes in the <a href="https://en.wikipedia.org/wiki/London_station_group">London station group</a>.</p>
<pre>
[
{
"stationName": "London Blackfriars",
"crsCode": "BFR"
},
{
"stationName": "London Cannon Street",
"crsCode": "CST"
},
{
"stationName": "London Charing Cross",
"crsCode": "CHX"
},
{
"stationName": "City Thameslink",
"crsCode": "CTX"
},
{
"stationName": "London Euston",
"crsCode": "EUS"
},
{
"stationName": "London Fenchurch Street",
"crsCode": "FST"
},
{
"stationName": "London Kings Cross",
"crsCode": "KGX"
},
{
"stationName": "London Liverpool Street",
"crsCode": "LST"
},
{
"stationName": "London Bridge",
"crsCode": "LBG"
},
{
"stationName": "London Marylebone",
"crsCode": "MYB"
},
{
"stationName": "Moorgate",
"crsCode": "MOG"
},
{
"stationName": "Old Street",
"crsCode": "OLD"
},
{
"stationName": "London Paddington",
"crsCode": "PAD"
},
{
"stationName": "London St Pancras International",
"crsCode": "STP"
},
{
"stationName": "Vauxhall",
"crsCode": "VXH"
},
{
"stationName": "London Victoria",
"crsCode": "VIC"
},
{
"stationName": "London Waterloo",
"crsCode": "WAT"
},
{
"stationName": "London Waterloo East",
"crsCode": "WAE"
}
]
</pre>
<h3>Service</h3>
<p><code><a href="/service/Z/zlpIG8jJacKayAnOXODw==?accessToken=">/service/[Service ID]?accessToken=[Your GUID token]</a></code></p>
Expand Down

0 comments on commit c3d8594

Please sign in to comment.