Skip to content

Commit

Permalink
Add fetchAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
fdesjardins committed Sep 11, 2016
1 parent fe384e1 commit 2aa472e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const notams = require('./')

notams.fetchAllTFR({ format: 'ICAO' }).then(results => {
notams.fetchAll().then(results => {
console.log(JSON.stringify(results, null, 2))
})
36 changes: 32 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const notams = module.exports = (icaos, options = {}) => {
}

// Main fetching method; accepts one or more ICAO codes
notams.fetch = (icaos, options) => {
notams.fetch = (icaos, options = {}) => {
const formatType = options.format || 'ICAO'

if (Array.isArray(icaos)) {
Expand Down Expand Up @@ -40,6 +40,34 @@ notams.fetchAllSpecialNotices = (options = {}) => {
return fetchAll('ALLSPECIALNOTICES', formatType)
}

// Fetch all NOTAMs
notams.fetchAll = (options = {}) => {
return Promise.join(
notams.fetchAllTFR(options),
notams.fetchAllGPS(options),
notams.fetchAllCARF(options),
notams.fetchAllSpecialNotices(options)
).then(([tfrs, gps, carfs, specialNotices]) => {
// Reconstruct/flatten the entire listing
const index = {}
;[tfrs, gps, carfs, specialNotices].map(notams => {
notams.map(notam => {
if (index[notam.icao] === undefined) {
index[notam.icao] = []
}
index[notam.icao].push(...notam.notams)
})
})
// Reformat the output
return Object.keys(index).map(icao => {
return {
icao: icao,
notams: index[icao]
}
})
})
}

// Helper method for the above fetchAll methods
const fetchAll = (queryType, formatType) => {
return request(`https://pilotweb.nas.faa.gov/PilotWeb/noticesAction.do?queryType=${queryType}&reportType=RAW&formatType=${formatType}`)
Expand Down Expand Up @@ -74,17 +102,17 @@ const parse = (html) => {

let $next = $(el).parent().next()
while (true) {
// stop if we hit the next ICAO section
// Stop if we hit the next ICAO section
const titleText = $next.find('#resultsTitleLeft').html()
if (titleText !== null) {
break
}
// stop at the end of the reports
// Stop at the end of the reports
const summaryText = $next.find('#alertFont').html()
if (summaryText !== null) {
break
}
// extract the current NOTAM text
// Extract the current NOTAM text
const notamText = $next.find('pre').text()
if (notamText !== '') {
notams.push(notamText)
Expand Down
14 changes: 11 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
const notams = require('./index')

describe('notams', () => {
it('should accept a single ICAO', (done) => {
it('should fetch NOTAMs for a single ICAO', (done) => {
notams('KFDC').then(() => done())
})

it('should accept a comma separated list of ICAOs', (done) => {
it('should fetch NOTAMs for a comma separated list of ICAOs', (done) => {
notams(['KFDC,KZBW']).then(() => done())
})

it('should accept an array of ICAOs', (done) => {
it('should fetch NOTAMs for an array of ICAOs', (done) => {
notams(['KFDC', 'KZBW']).then(() => done())
})

it('should expose the fetch method', (done) => {
notams.fetch('KFDC').then(() => done())
})

it('should fetch TFRs', (done) => {
notams.fetchAllTFR().then(() => done())
})
Expand All @@ -30,4 +34,8 @@ describe('notams', () => {
it('should fetch Special Notices', (done) => {
notams.fetchAllSpecialNotices().then(() => done())
})

it('should fetch all NOTAMs', (done) => {
notams.fetchAll().then(() => done())
})
})

0 comments on commit 2aa472e

Please sign in to comment.