Created
June 20, 2017 21:04
-
-
Save json2d/344703da1022674da0b426caa941f4aa to your computer and use it in GitHub Desktop.
mta-bustime-promise.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_PATH = "http://bustime.mta.info/api" | |
const request = require('request') | |
function BusTime(key) { | |
this.key = key; | |
this.where = service('where') | |
this.siri = service('siri') | |
} | |
var factory = function(name) { | |
return function(command, arg, qs) { | |
let url | |
if(typeof arg == 'object') { | |
qs = arg | |
arg = undefined | |
url = [API_PATH,name,command].join('/') | |
}else if(typeof arg == 'string'){ | |
url = [API_PATH,name,command,arg].join('/') | |
} | |
let opts = buildOptions(url,this.key,qs) | |
return new Promise(function (fulfill, reject){ | |
request(opts, (err,res,body) => { | |
if (err) reject(err) | |
else fullfill({res,body,command,arg,qs}) | |
}) | |
}) | |
} | |
} | |
var buildOptions(url,key,qs) { | |
return { | |
url, | |
json:true, | |
qs: Object.assign({},qs,{key}) | |
} | |
} | |
// Usage: | |
// var bustime = new BusTime('this-is-my-key') | |
// bustime.where('agencies-with-coverage').then(storeData) | |
// bustime.where('routes-for-agency',someAgency,{}).then(storeData) | |
// bustime.where('stops-for-route',someRoute,{}).then(storeData) | |
// bustime.siri('stop-monitoring',{LineRef:'Q8'}).then(broadcastData) | |
// | |
module.exports = BusTime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment