-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (31 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const rp = require('request-promise')
const parser = require('xml2json')
const BASE_URL = 'https://aviationweather.gov/adds/dataserver_current/httpparam'
const DEFAULT_HOURS_BEFORE = 4
let options = {
url: BASE_URL,
qs: {
requestType: 'retrieve',
format: 'xml'
}
}
function fetchWeather (dataSource, stationString, hoursBeforeNow = DEFAULT_HOURS_BEFORE) {
stationString = (Array.isArray(stationString)) ? stationString.join(',') : stationString
options.qs = { ...options.qs, dataSource, stationString, hoursBeforeNow }
return rp(options)
.then(weatherResponse => parser.toJson(weatherResponse, { object: true, coerce: true }))
}
module.exports = {
getMETAR (ICAO, hoursBeforeNow) {
return fetchWeather('metars', ICAO, hoursBeforeNow)
.then(paseredMETARS => paseredMETARS.response.data.METAR)
},
getTAF (ICAO, hoursBeforeNow) {
return fetchWeather('tafs', ICAO, hoursBeforeNow)
.then(parsedTAFS => parsedTAFS.response.data.TAF)
},
getAirportWeather (ICAO, hoursBeforeNow) {
return Promise.all([module.exports.getTAF(ICAO, hoursBeforeNow), module.exports.getMETAR(ICAO, hoursBeforeNow)])
.then(([TAFS, METARS]) => ({ TAFS, METARS }))
}
}