@repetere/node-holidayapi
Version:
Node.js library for Holiday API
133 lines (107 loc) • 3.33 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const https = require('https');
const qs = require('querystring');
/**
* Returns data from the holiday api from a set of parameters
* @param {String} [parameters.country] - Country is required, e.g. US,
* @param {Number} [parameters.year] - Year is required, e.g. 2018
* @param {Number} [parameters.month = 7] - month of the year
* @param {Number} [parameters.day = 4] - day of the month
* @param {Boolean} [parameters.previous = true]
* @param {Boolean} [parameters.upcoming = true]
* @param {Boolean} [parameters.public = true]
* @param {Boolean} [parameters.pretty = true]
* @returns {Promise} resolves API response data
*/
function holidays(parameters = {}) {
const querystringObject = Object.assign(
{},
{ key: this.key },
parameters
);
const querystring = qs.stringify(querystringObject);
const url = `https://holidayapi.com/v1/holidays?${querystring}`;
return new Promise((resolve, reject) => {
https.get(url, function (res) {
res.on('data', function (data) {
let error = null;
try {
data = JSON.parse(data);
} catch (e) {
data = {};
}
if (res.statusCode !== 200) {
if ('undefined' === typeof data[ 'error' ]) {
error = 'Unknown error.';
} else {
error = data.error;
}
}
if (error) {
reject(error);
} else {
resolve(data);
}
})
.on('error', reject);
})
.on('error', reject);
});
}
/**
* class for querying the holiday api
* @class HolidayAPI
* @property {String} this.key - API Key for holidayapi.com
* @property {Object} this.v1 - api methods for v1
* @param {String} options.key - API Key
*/
class HolidayAPI {
constructor(options = {}) {
this.key = (typeof options === 'string')
? options
: options.key;
this.v1 = {
holidays: holidays.bind(this),
};
}
}
/**
* @static {Object} v1 - Version 1 of the holiday api
* @memberOf HolidayAPI
*/
HolidayAPI.v1 = {
holidays,
};
module.exports = HolidayAPI;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="HolidayAPI.html">HolidayAPI</a></li></ul><h3>Global</h3><ul><li><a href="global.html#holidays">holidays</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 25 2018 22:51:02 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>