var request = require('request'); /** * ## Responses * Responses from this API with be wrapped in an envelope to assist the caller * in dealing with the various responses from the service. There are no types * to check against in JavaScript, so this facilitates that. * * { * status: 200, * data: { name: "John", age: 29 } * } * * ## Logging * To enable logging, use the NODE_DEBUG environment variable. * * NODE_DEBUG=apidoc * * @constructor */ function Giltpublicapi (host) { var showLogMessages = process.env.NODE_DEBUG ? process.env.NODE_DEBUG.indexOf('apidoc') !== -1 : false; function log (message) { if (showLogMessages) { console.log('[apidoc] ' + message); } } function getQueryString (options) { var params = []; var qs = options.qs || {}; for (var key in qs) { if (qs[key]) { params.push(key + '=' + qs[key]); } } return params.join('&'); } function doRequest (options) { var startMs = Date.now(); options = options || {}; if (!options.json) { options.json = true; } options.qsStringifyOptions = { arrayFormat: 'repeat' }; var queryString = getQueryString(options); var url = options.uri + (queryString ? '?' + queryString : ''); log(options.method + ' ' + url); if (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') { log('|--> With body:\n\t\t' + JSON.stringify(options.json)); } return new Promise(function (resolve, reject) { request(options, function (err, response) { if (err) { reject(err); return; } var duration = Date.now() - startMs; var requestHref = response.request ? response.request.href : ''; log('Completed ' + options.method + ' ' + requestHref + ' ' + response.statusCode + ' ' + duration + 'ms'); resolve(response); }); }); } /** * Simple plain object check. This is actually really involved to be 100% * accurate */ function isObject (obj) { return typeof obj === 'object' && !Array.isArray(obj); } var Products = (function Products() { /** * * * @param {string} productId * @param {Object} options * @param {string} options.apikey */ function getDetailJsonByProductId(productId, options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/products/' + productId + '/detail.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } /** * * * @param {Object} options * @param {string} options.apikey */ function getCategoriesJson(options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/products/categories.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } return { getDetailJsonByProductId: getDetailJsonByProductId, getCategoriesJson: getCategoriesJson, }; }()); var SaleDetails = (function SaleDetails() { /** * * * @param {string} store * @param {string} sale * @param {Object} options * @param {string} options.apikey */ function getByStoreAndSaledetailJson(store, sale, options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/sales/' + store + '/' + saledetailJson + ''; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } return { getByStoreAndSaledetailJson: getByStoreAndSaledetailJson, }; }()); var SaleList = (function SaleList() { /** * * * @param {Object} options * @param {string} options.apikey */ function getActiveJson(options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/sales/active.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } /** * * * @param {string} store * @param {Object} options * @param {string} options.apikey */ function getActiveJsonByStore(store, options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/sales/' + store + '/active.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } /** * * * @param {Object} options * @param {string} options.apikey */ function getUpcomingJson(options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/sales/upcoming.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } /** * * * @param {string} store * @param {Object} options * @param {string} options.apikey */ function getUpcomingJsonByStore(store, options) { // if options is not defined, set it options = options || {}; // otherwise if it's not the kind of object we want... if (!isObject(options)) { return Promise.reject(new Error('options parameter is required and must be a plain object')); } var url = host + '/sales/' + store + '/upcoming.json'; var queryString = { apikey: options.apikey, }; var requestOpts = { qs: queryString, uri: url, method: 'GET' }; return doRequest(requestOpts).then(function (serverResponse) { switch (serverResponse.statusCode) { case 200: return Promise.resolve(serverResponse.body); default: return Promise.reject(new Error('Unknown response code: ' + serverResponse.statusCode)); } }); } return { getActiveJson: getActiveJson, getActiveJsonByStore: getActiveJsonByStore, getUpcomingJson: getUpcomingJson, getUpcomingJsonByStore: getUpcomingJsonByStore, }; }()); this.Products = Products; this.SaleDetails = SaleDetails; this.SaleList = SaleList; } module.exports = Giltpublicapi;