Bicing

node v0.12.18
version: 2.0.0
endpointsharetweet
Let's first declare some helper methods for easier usage below
var request = require('request-promise') var Get = function(endpoint, property) { return new Promise(function(resolve, reject) { request.get('http://barcelonaapi.marcpous.com/'+endpoint+'.json').then(function(res) { console.log(JSON.parse(res)) var points = JSON.parse(res).data[property].map(function(point) { return { latitude: parseFloat(point.lat), longitude: parseFloat(point.lon) } }) resolve(points) }) }) }
Then we'll write a quick map for even easier use!
var Data = { bicing: { property: 'bici', endpoint: 'bicing/stations' }, bus: { property: 'tmbs', endpoint: 'bus/stations' }, metro: { property: 'metro', endpoint: 'metro/stations' } } var GetData = function(type) { return Get(Data[type].endpoint, Data[type].property) }
So let's try to get all the bicing stations
var bicing_stations = await GetData('bicing');
Oooh, it's working! Awesome! Now you should see a map of all the bicing stations above. Let's see how many bustations there are in Barcelona
var stops = await GetData('bus') stops.length
So, there it is, the number of stations that exists in Barcelona! Simple enough!
var stations = await GetData('metro') stations.length
What's cool with Tonicdev though, is that now when we made this assignment in the last window, we can continue doing things with it. So if we just return stations this time, we'll get a cool map of the current metro stations in Barcelona.
stations
Loading…

no comments

    sign in to comment