diff --git a/index.js b/index.js index 69dff06..759790b 100644 --- a/index.js +++ b/index.js @@ -11,11 +11,13 @@ const openCatalog = require('./lib/OpenCatalog/service'); * @prototype * @class Icecat */ -const icecat = function (username, password, httpUrl = 'data.icecat.biz/xml_s3/xml_server3.cgi') { +const icecat = function (username, password, httpUri = 'data.icecat.biz', path = '/xml_s3/xml_server3.cgi') { this.VERSION = packagejson.version; - this.scheme = 'https://'; - this.httpAuth = username + ':' + encodeURIComponent(password); - this.httpUrl = httpUrl; + this.port = 443; + this.username = username; + this.password = password; + this.httpUri = httpUri; + this.path = path; this.openCatalog = new openCatalog(this); }; diff --git a/lib/OpenCatalog/product.js b/lib/OpenCatalog/product.js index 7782a9c..28e2880 100644 --- a/lib/OpenCatalog/product.js +++ b/lib/OpenCatalog/product.js @@ -84,7 +84,10 @@ icecat.prototype.getReleaseDate = function() { */ icecat.prototype.getLongDescription = function() { try { - return this.productData.ProductDescription[0].$.LongDesc; + if(typeof this.productData.ProductDescription[0].$ === 'undefined'){ + return this.productData.SummaryDescription[0].LongSummaryDescription[0]._ + } + return this.productData.ProductDescription[0].$.LongDesc } catch (e) { return false; } @@ -96,6 +99,9 @@ icecat.prototype.getLongDescription = function() { */ icecat.prototype.getShortDescription = function() { try { + if(typeof this.productData.ProductDescription[0].$ === 'undefined'){ + return this.productData.SummaryDescription[0].ShortSummaryDescitpion[0]._ + } return this.productData.ProductDescription[0].$.ShortDesc; } catch (e) { return false; @@ -262,4 +268,4 @@ icecat.prototype.getCategoryFeatureGroups = function() { } }; -module.exports = icecat; +module.exports = icecat; \ No newline at end of file diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index d70be75..8ff01b4 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -21,7 +21,7 @@ const openCatalog = function(instance) { * @returns {Promise} */ openCatalog.prototype.getProduct = function(lang, GTIN) { - const httpRequestUrl = this._getBaseUrl(lang) + ';ean_upc=' + GTIN; + const httpRequestUrl = this._getPath(lang) + ';ean_upc=' + GTIN; return this._requestProduct(httpRequestUrl); }; @@ -33,7 +33,7 @@ openCatalog.prototype.getProduct = function(lang, GTIN) { * @param {integer} productId */ openCatalog.prototype.getProductById = function(lang, productId) { - const httpRequestUrl = this._getBaseUrl(lang) + ';product_id=' + productId; + const httpRequestUrl = this._getPath(lang) + ';product_id=' + productId; return this._requestProduct(httpRequestUrl); }; @@ -48,7 +48,7 @@ openCatalog.prototype.getProductById = function(lang, productId) { * @param {string} sku */ openCatalog.prototype.getProductBySKU = function(lang, brand, sku) { - const httpRequestUrl = this._getBaseUrl(lang) + ';prod_id=' + sku + ';vendor=' + brand; + const httpRequestUrl = this._getPath(lang) + ';prod_id=' + sku + ';vendor=' + brand; return this._requestProduct(httpRequestUrl); }; @@ -89,8 +89,8 @@ openCatalog.prototype._getProductByXMLdata = function(xmlData, httpRequestUrl) { * * @param {string} lang */ -openCatalog.prototype._getBaseUrl = function(lang) { - return `${this.icecat.scheme}${this.icecat.httpAuth}@${this.icecat.httpUrl}?lang=${lang};output=productxml`; +openCatalog.prototype._getPath = function(lang) { + return `${this.icecat.path}?lang=${lang};output=productxml`; }; /** @@ -100,8 +100,16 @@ openCatalog.prototype._getBaseUrl = function(lang) { * @returns {Promise} */ openCatalog.prototype._requestProduct = function(httpRequestUrl) { + let options = { + host: this.icecat.httpUri, + path: httpRequestUrl, + headers: { + 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') + } + } + return new Promise((resolve, reject) => { - const request = this.https.get(httpRequestUrl, (response) => { + const request = this.https.get(options, (response) => { let body = ''; response.on('data', (chunk) => {