diff --git a/index.js b/index.js new file mode 100644 index 0000000..12c43a2 --- /dev/null +++ b/index.js @@ -0,0 +1,51 @@ +'use strict'; + +var https = require('https'); + +var HolidayAPI = function (key) { + if ('undefined' !== typeof key) { + HolidayAPI.prototype.key = key; + } +}; + +HolidayAPI.prototype.v1 = {}; + +HolidayAPI.prototype.v1.holidays = function (parameters, callback) { + var url = 'https://holidayapi.com/v1/holidays'; + var querystring = '?key=' + HolidayAPI.prototype.key; + + if ('object' === typeof parameters) { + for (var parameter in parameters) { + querystring += '&' + parameter + '=' + parameters[parameter]; + } + } + + url += querystring; + + https.get(url, function (res) { + res.on('data', function (data) { + try { + data = JSON.parse(data); + } catch (e) { + data = {}; + } + + var error = null; + + if (res.statusCode !== 200) { + if ('undefined' === typeof data['error']) { + error = 'Unknown error.'; + } else { + error = data.error; + } + } + + return callback(error, data); + }); + }).on('error', function (e) { + callback(e.message); + }); +}; + +module.exports = HolidayAPI; + diff --git a/package.json b/package.json new file mode 100644 index 0000000..6a415de --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "node-holidayapi", + "version": "0.0.0", + "description": "Official Node.js library for Holiday API", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/joshtronic/node-holidayapi.git" + }, + "keywords": [ "holiday", "holidays", "holidayapi" ], + "author": "Josh Sherman (https://holidayapi.com)", + "license": "MIT", + "bugs": { + "url": "https://github.com/joshtronic/node-holidayapi/issues" + }, + "homepage": "https://holidayapi.com" +}