-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
spritpreisrechner.js
215 lines (191 loc) · 5.81 KB
/
spritpreisrechner.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @file apis/spritpreisrechner.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-Fuel
*/
/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');
/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
*/
const fetch = require('node-fetch');
const { filterStations } = require('./utils');
const BASE_URL = 'https://api.e-control.at/sprit/1.0';
const TYPES = {
diesel: 'DIE',
e5: 'SUP',
gas: 'GAS'
};
let config;
/**
* @function generateUrl
* @description Helper function to generate API request url.
*
* @param {string} type - Fuel type
*
* @returns {string} url
*/
function generateUrl(type) {
return `${BASE_URL}/search/gas-stations/by-address?latitude=${config.lat}&longitude=${
config.lng}&fuelType=${TYPES[type]}&includeClosed=${!config.showOpenOnly}`;
}
/**
* @function requestFuelType
* @description API request for specified type.
* @async
*
* @param {string} type - Fuel type.
*
* @returns {Promise} Object with fuel type and data.
*/
async function requestFuelType(type) {
const response = await fetch(generateUrl(type));
return {
type,
data: await response.json()
};
}
/**
* @function compareStations
* @description Helper function to compare gas stations.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {boolean} Flag if the gas stations are equal.
*/
function compareStations(a, b) {
return a.location.city === b.location.city
&& a.location.postalCode === b.location.postalCode
&& a.name === b.name
&& a.location.latitude === b.location.latitude
&& a.location.longitude === b.location.longitude;
}
/**
* @function reducePrice
* @description Reduces array of prices to single price.
*
* @param {Object[]} prices - All prices.
*
* @returns {number} Highest price or -1 if there is no price.
*/
function reducePrice(prices) {
return prices.reduce((current, price) => {
if (!Object.prototype.hasOwnProperty.call(price, 'amount') || price.amount === '') {
return current;
}
return current < price.amount ? price.amount : current;
}, -1);
}
/**
* @function normalizeStations
* @description Helper function to normalize the structure of gas stations for the UI.
*
* @param {Object[]} stations - Gas Station.
* @param {string[]} keys - Fuel types except config option sortBy.
*
* @returns {void}
*
* @see apis/README.md
*/
function normalizeStations(stations, keys) {
stations.forEach((value, index) => {
/* eslint-disable no-param-reassign */
stations[index].name = value.name;
stations[index].prices = { [config.sortBy]: reducePrice(value.prices) };
keys.forEach(type => {
stations[index].prices[type] = -1;
});
stations[index].isOpen = value.open;
stations[index].street = value.location.address;
stations[index].address = `${value.location.postalCode} ${value.location.city} - ${stations[index].street}`;
stations[index].lat = parseFloat(value.location.latitude);
stations[index].lng = parseFloat(value.location.longitude);
stations[index].distance = value.distance.toFixed(2);
/* eslint-enable no-param-reassign */
});
}
/**
* @function getData
* @description Performs the data query and processing.
* @async
*
* @returns {Object} Returns object described in the provider documentation.
*
* @see apis/README.md
*/
async function getData() {
const responses = await Promise.all(config.types.map(requestFuelType));
const collection = {};
responses.forEach(element => {
collection[element.type] = element.data;
});
let stations = collection[config.sortBy];
const maxPrices = {};
for (const type in collection) {
for (const station of collection[type]) {
for (const price of station.prices) {
if (!maxPrices[price.fuelType] || price.amount > maxPrices[price.fuelType]) {
maxPrices[price.fuelType] = price.amount;
}
}
}
}
stations = stations.filter(station => station.distance <= config.radius);
delete collection[config.sortBy];
const keys = Object.keys(collection);
normalizeStations(stations, keys);
keys.forEach(type => {
collection[type].forEach(station => {
for (let i = 0; i < stations.length; i += 1) {
if (compareStations(station, stations[i])) {
stations[i].prices[type] = reducePrice(station.prices);
break;
}
}
});
});
for (const station of stations) {
for (const type in station.prices) {
if (station.prices[type] === -1) {
station.prices[type] = `>${maxPrices[TYPES[type]]}`;
}
}
}
stations = stations.filter(filterStations);
return {
types: ['diesel', 'e5', 'gas'],
unit: 'kilometer',
currency: 'EUR',
byPrice: stations,
byDistance: _.sortBy(stations, 'distance')
};
}
/**
* @module apis/spritpreisrechner
* @description Queries data from spritpreisrechner.at
*
* @requires external:node-fetch
* @requires module:Utils
*
* @param {Object} options - Configuration.
* @param {number} options.lat - Latitude of Coordinate.
* @param {number} options.lng - Longitude of Coordinate.
* @param {int} options.radius - Lookup area for gas stations.
* @param {string} options.sortBy - Type to sort by price.
* @param {string[]} options.types - Requested fuel types.
* @param {boolean} options.showOpenOnly - Flag to show only open gas stations.
*
* @returns {Object} Object with function getData.
*/
module.exports = options => {
config = options;
return { getData };
};