Skip to content

Instantly share code, notes, and snippets.

@aershov24
Last active March 10, 2018 03:10
Show Gist options
  • Save aershov24/e552937064ceb47e0767045c9e5cb492 to your computer and use it in GitHub Desktop.
Save aershov24/e552937064ceb47e0767045c9e5cb492 to your computer and use it in GitHub Desktop.
NodeJS Client module for OpenUV - Global Real-Time UV Index API
/*
NodeJS Client module for OpenUV - Global Real-Time UV Index API
Installation:
npm install --save request
npm install --save query-string
Usage:
const openuv = require('./openuv.js')('v1', 'YOUR_API_KEY');
var params = {
lat: -31.67,
lng: 115.345
};
openuv.getUVIndex(params, (err, result) => {
if (err) { console.log(err); }
else { console.log(result.uv); }
});
*/
const request = require('request');
const queryString = require('query-string');
module.exports = function (version, apikey) {
var openuv = {};
var endpoint = 'https://api.openuv.io/api/'+version;
var options = {
headers: {
'x-access-token': apikey
}
};
openuv.getUVIndex = function (params, cb) {
options.url = endpoint+'/uv?'+queryString.stringify(params);
console.log(options);
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
cb(null, JSON.parse(body).result);
}
else {
cb(JSON.parse(body).error, null);
}
})
};
openuv.getForecast = function (params) {
options.url = endpoint+'/forecast?'+queryString.stringify(params);
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
cb(null, JSON.parse(body).result);
}
else {
cb(JSON.parse(body).error, null);
}
})
};
openuv.getProtection = function (params) {
options.url = endpoint+'/protection?'+queryString.stringify(params);
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
cb(null, JSON.parse(body).result);
}
else {
cb(JSON.parse(body).error, null);
}
})
};
return openuv;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment