Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
10 changes: 8 additions & 2 deletions lib/OpenCatalog/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -262,4 +268,4 @@ icecat.prototype.getCategoryFeatureGroups = function() {
}
};

module.exports = icecat;
module.exports = icecat;
20 changes: 14 additions & 6 deletions lib/OpenCatalog/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand Down Expand Up @@ -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`;
};

/**
Expand All @@ -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) => {
Expand Down