mj-pkg
Version:
A Node.js library that abstracts away differences among popular marijuana POS providers.
156 lines (107 loc) • 3.75 kB
JavaScript
;
var request = require('request'),
Promise = require('promise');
var Result = function(data) {
this.data = data;
};
Result.prototype.getCategories = function() {
return this.data.response_details.product_categories;
};
module.exports = MJ.Drivers.Create({
_data: {},
init: function(options) {
if (!options.apiId) {
throw new MJ.Errors.InvalidCredentials('apiId');
}
if (!options.apiKey) {
throw new MJ.Errors.InvalidCredentials('apiKey');
}
this.apiId = options.apiId;
this.apiKey = options.apiKey;
this.url = options.url || 'https://i.gomjfreeway.com/Training77/api/menu';
this.locationNid = options.locationNid || 45;
},
getLocations: function() {
var _this = this;
return new Promise(function(resolve, reject) {
_this.run('post').then(function(data) {
return resolve(MJ.utils.map(data.response_details.locations, function(val, key) {
return val;
}));
}, function(err) {
return reject(err);
});
});
},
getCategories: function() {
var _this = this;
return new Promise(function(resolve, reject) {
_this.run('post').then(function(data) {
return resolve(MJ.utils.map(data.response_details.product_categories, function(val, key) {
return val;
}));
}, function(err) {
return reject(err);
});
});
},
getProducts: function() {
var _this = this;
return new Promise(function(resolve, reject) {
_this.run('post').then(function(data) {
return resolve(MJ.utils.map(data.response_details.menu_items, function(val, key) {
return val;
}));
}, function(err) {
return reject(err);
});
});
},
run: function(method, path, data) {
var _this = this;
return new Promise(function(resolve, reject) {
if (Object.keys(_this._data).length > 0) {
return resolve(_this._data);
}
var content = false,
headers = {};
var options = {
strictSSL: false,
rejectUnauthorized: false,
pool: false,
method: method.toUpperCase(),
url: _this.url,
form: {
api_key: _this.apiKey,
api_id: _this.apiId,
format: 'JSON',
version: 3,
location_nid: _this.locationNid
}
};
if (data) {
MJ.utils.forEach(data, function(val, key) {
options.form[key] = val;
});
}
request(options, function(err, res, body) {
if (!err) {
var b = body;
try {
b = JSON.parse(body);
} catch(e) {}
if (b.response_header && b.response_header.response_code == 'OK') {
_this._data = b;
return resolve(b);
} else {
return reject(new MJ.Errors.MessedUp(b.response_header.response_code, b.response_details.error_details));
}
}
return reject(new MJ.Errors.MessedUp('Error', err));
});
});
},
connect: function() {
return this;
}
});