Skip to content

Commit 71fc2d4

Browse files
ubergesundheitmpfeil
authored andcommitted
Add parameter to return all stations from luqs function (#3)
1 parent 8ea0395 commit 71fc2d4

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Returns an array containing all LUQS stations.
2828

2929
Source: https://www.lanuv.nrw.de/luqs/messorte/messorte.php
3030

31+
Optionally accepts an `options` object as first parameter:
32+
33+
- `allStations: true` returns all active and inactive stations.
34+
3135
### luqs.station(kuerzel)
3236
Returns detailed information about a specific LUQS station
3337

index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ const MAX_KUERZEL_LENGTH = 6
1717
*
1818
* @param {String} url
1919
*/
20-
const query = (url) => {
20+
const query = (url, options = {}) => {
2121
return new Promise((resolve, reject) => {
22-
request(url, function (error, response, body) {
22+
request({
23+
url,
24+
method: options.method ? options.method : 'GET',
25+
form: options.form ? options.form : undefined
26+
}, function (error, response, body) {
2327
if (error) {
2428
reject(error)
2529
}
@@ -37,12 +41,23 @@ const query = (url) => {
3741
/**
3842
* Requests LUQS stations website and parse stations table.
3943
*
40-
* @param {*} options
44+
* @param options.allStations return all stations if true
4145
* @returns Promise resolves with an array of all luqs stations
4246
*/
4347
const luqs = (options = {}) => {
4448
return new Promise((resolve, reject) => {
45-
query(messorteUrl)
49+
let requestOptions
50+
if (options.allStations === true) {
51+
requestOptions = {
52+
method: 'POST',
53+
form: {
54+
auswahl_plz: 'alle',
55+
auswahl_status: 'alle',
56+
auswahl_klassifizierung: 'alle'
57+
}
58+
}
59+
}
60+
query(messorteUrl, requestOptions)
4661
.then($ => {
4762
const stations = []
4863
const tableRows = $('#wrapper > table > tbody > tr')

test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import luqs from '.'
44
test('luqs()', async t => {
55
const stations = await luqs()
66
t.truthy(Array.isArray(stations))
7-
t.is(stations.length, 165)
7+
t.truthy(stations.length > 160)
8+
})
9+
10+
test('luqs({ allStations: true })', async t => {
11+
const stations = await luqs({ allStations: true })
12+
t.truthy(Array.isArray(stations))
13+
t.truthy(stations.length > 578)
814
})
915

1016
test('luqs.station() should reject with TypeError', async t => {

0 commit comments

Comments
 (0)