Skip to content

Commit

Permalink
Add user-agent option, use current run cache setting for cache lookup…
Browse files Browse the repository at this point in the history
… expiry, remove metadata from cache files
  • Loading branch information
withaspark committed May 6, 2019
1 parent ce3fb2e commit b438026
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -92,6 +92,7 @@ path/to/src/withaspark.weather.cli.js --station=KJAX
| `cache_dir` | "/tmp" | Directory to write cache files too. |
| `cache_prefix` | "withaspark.weather." | Prefix for cache file names. |
| `cache_lifetime` | 5 | Number of minutes to cache results before refetching. |
| `user_agent` | "@withaspark/weather User" | User agent to use when making requests to weather API. If experiencing rate-limiting issues, set to something unique. |
| `unknown` | "?" | Symbol for unknown value. |


Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@withaspark/weather",
"version": "1.2.1",
"version": "1.3.0",
"description": "Current weather and forecast fetching script",
"main": "src/withaspark.weather.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/withaspark.weather.cli.js
Expand Up @@ -13,6 +13,9 @@ var Weather = require("../src/withaspark.weather.js")({
cache_lifetime: args.hasOwnProperty("cache_lifetime")
? args.cache_lifetime
: null,
user_agent: args.hasOwnProperty("user_agent")
? args.user_agent
: null,
unknown: args.hasOwnProperty("unknown")
? args.unknown
: null
Expand Down
59 changes: 20 additions & 39 deletions src/withaspark.weather.js
Expand Up @@ -22,6 +22,7 @@ function Weather(options) {
this.config.station = options.station || "KJAX";
this.config.weather_url = "https://api.weather.gov/stations/{station}/observations/latest";
this.config.alert_url = "https://api.weather.gov/alerts/active?point={point}";
this.config.user_agent = options.user_agent || "@withaspark/weather User";
this.config.cache_dir = (options.cache_dir || "/tmp").replace(new RegExp("/" + path.sep + "$/"));
this.config.cache_prefix = options.cache_prefix || ("withaspark.weather." + this.config.station + ".");
this.config.cache_lifetime = (options.hasOwnProperty("cache_lifetime"))
Expand Down Expand Up @@ -74,13 +75,18 @@ function Weather(options) {
: null,
value = null;

// If value is cached, use it
if (cached !== null) {
value = cached;
}

if (saved !== null) {
// If value is saved, use it
else if (saved !== null) {
value = saved;
}
// If value is the name of a method, call it
else if (typeof that[key] === 'function') {
value = that[key]();
}

// Round all numbers to integers
if (that.isNumber(value)) {
Expand Down Expand Up @@ -113,10 +119,9 @@ function Weather(options) {
*
* @param {string} key
* @param {*} value
* @param {integer} expiry
* @returns {*} Value of value
*/
function cache(key, value, expiry) {
function cache(key, value) {
if (typeof value === "undefined") {
if (isCacheExpired(key)) {
return null;
Expand All @@ -125,11 +130,7 @@ function Weather(options) {
return getFromCache(key);
}

if (typeof expiry === "undefined") {
expiry = that.config.cache_lifetime;
}

setInCache(key, value, expiry);
setInCache(key, value);

return value;
}
Expand All @@ -141,14 +142,11 @@ function Weather(options) {
* @returns {boolean}
*/
function isCacheExpired(key) {
var data = (getFromCache(key, true) || "").split("\n", 2);

// Invalid cache file, just say it is expired
if (data.length !== 2) {
if (!fs.existsSync(getKeyCacheFile(key))) {
return true;
}

var expiry = data[0].split(":", 2)[1],
var expiry = that.config.cache_lifetime,
age = getCacheAge(key);

return (age >= expiry);
Expand All @@ -175,23 +173,18 @@ function Weather(options) {
*
* @param {string} key
* @param {*} value
* @param {*} expiry
* @returns {void}
*/
function setInCache(key, value, expiry) {
function setInCache(key, value, ) {
if (typeof key !== "string" || typeof value === "undefined") {
throw "Unable to set value in cache. String type key and any value is required.";
}

if (typeof expiry === 'undefined') {
expiry = that.cache_lifetime;
}

that.data[key] = value;

fs.writeFileSync(
getKeyCacheFile(key),
"expires:" + expiry + "\n" + value,
value,
function (err) {
if (err) {
return console.error(err);
Expand All @@ -204,29 +197,18 @@ function Weather(options) {
* Get the data for key from cache.
*
* @param {string} key
* @param {boolean} include_headers If true, will include metadata in return value
* @returns {string}
*/
function getFromCache(key, include_headers) {
function getFromCache(key) {
if (typeof key !== "string") {
throw "Unable to get value from cache. String type key is required.";
}

if (typeof include_headers !== "boolean") {
include_headers = false;
}

if (!fs.existsSync(getKeyCacheFile(key))) {
return null;
}

var data = fs.readFileSync(getKeyCacheFile(key), { encoding: "utf8" });

if (include_headers) {
return data;
}

return data.split("\n", 2)[1];
return fs.readFileSync(getKeyCacheFile(key), { encoding: "utf8" });
}

/**
Expand Down Expand Up @@ -263,7 +245,6 @@ function Weather(options) {
*/
function fetchWeatherForStation() {
if (!isCacheExpiredMany([
'station',
'timestamp', 'raw', 'coordinates', 'elevation', 'text',
'temperature', 'dewpoint', 'windDirection', 'windSpeed',
'pressure', 'visibility', 'precipitation', 'humidity',
Expand All @@ -277,13 +258,13 @@ function Weather(options) {
that.config.weather_url.replace('{station}', that.config.station),
{
headers: {
'user-agent': '@withaspark/weather User',
'user-agent': that.config.user_agent,
},
}
);
response = JSON.parse(response.getBody("UTF-8"));

cache('timestamp', response.properties.timestamp);
cache('timestamp', moment(response.properties.timestamp).toISOString(true));
cache('raw', response.properties.rawMessage);
cache(
'coordinates',
Expand Down Expand Up @@ -319,7 +300,7 @@ function Weather(options) {
that.config.alert_url.replace('{point}', that.getCoordinates()),
{
headers: {
'user-agent': '@withaspark/weather User',
'user-agent': that.config.user_agent,
},
}
);
Expand All @@ -338,7 +319,7 @@ function Weather(options) {
* @returns {void}
*/
function calcSecondaryProperties() {
var now = that.now,
var now = that.getNow(),
coordinates = that.getCoordinates().split(",");

if (coordinates && coordinates.length == 2) {
Expand Down

0 comments on commit b438026

Please sign in to comment.