The price is right
Having completed Chapter 3 of Raymond Camden’s “Apache Cordova in action” and having a really basic app that makes a GET request to the Github search API; I thought let’s try an extension activity. This is Rethinking Transport, so it had better be about transport. How about we find the rail fares between two stations ?
I am not affiliated with BRFares, but I thought that I’d use their web API because it is currently free and doesn’t require a OAuth subscription process. That simplifies the app a whole load. The JSON API is described at: http://www.brfares.com/api/
Changes to the HTML file
The changes to the HTML file are trivial. For the GitHub search API, we only needed one search parameter. For a BRFareas search, we need two: an origin and a destination.
Here’s the code with the new text highlighted:
Basically, I have duplicated the input SearchField and re-named each one to be SearchField1 and SearchField2.
CHANGES TO THE JAVASCRIPT
There’s a bit more to change to the Javascript file app.js. Firstly, we need to declare an extra variable to match the new search box: $search1 & $search2.
We create a second variable to hold not only the Origin of the search, but also the Destination.
We update the text when searching to say that we are doing a search for the Origin to the Destination.
Now, we get to the meaty bit. We need to change our API call to:
$.get(“http://api.brfares.com/querysimple”,
{“orig”:search1, “dest”: search2}, function(res,code) {…});
Querysimple picks up all of the fares between an origin and a destination. The format of the response is:
We are looking for four data tags:
- Ticket code at fares.ticket.code e.g. 7DF
- Ticket name at fares.ticket.name e.g. “Seven Day 1st”
- Route name at fares.route.name e.g. “Not via London”
- Adult Fare at fares..adult.fare e.g. £640.00
We now need to modify the function (res, code) {} to parse the data, not from GitHub API, but from the BRFaresAPI instead.
Tip for the day
It’s hard these days to find an API that doesn’t require OAuth and a developer key, even if it is not charged. It’s time to learn some new skills and get to grips with OAuth … However, I’m afraid that that is not for this blog series.