Skip to content

Commit

Permalink
Plain dealer
Browse files Browse the repository at this point in the history
  • Loading branch information
ltran committed Jan 26, 2017
1 parent 674e7ef commit e88b580
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
67 changes: 67 additions & 0 deletions dealer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var http = require('https');

var getDealsByCentre = function (centreId, callback) {
requestDeals(process.env["WF_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "&centre_id=" + centreId, function(err, data) {
if (err) {
return callback(err)
}

return callback(err, data)
})
}

var getDealsByCentreByStore = function (centreId, storeId, callback) {
requestDeals(process.env["WF_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "&centre_id=" + centreId + "&store_id=" + storeId, function(err, data) {
if (err) {
return callback(err)
}

return callback(err, data)
})

}

var getDealsByCentreByRetailer = function (centreId, retailerID, callback) {
requestDeals(process.env["WF_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "&centre_id=" + centreId + "&retailer_id=" + retailerId, function(err, data) {
if (err) {
return callback(err)
}

return callback(err, data)
})
}

var requestDeals = function (endpoint, queryString, callback) {
http.get(endpoint + queryString, function (res) {
var responseString = '';
console.log('Status Code: ' + res.statusCode);

if (res.statusCode != 200) {
callback(new Error("Non 200 Response"));
}

res.on('data', function (data) {
responseString += data;
});

res.on('end', function () {
var responseObject = JSON.parse(responseString);

if (responseObject.error) {
console.log("NOAA error: " + responseObject.error.message);
callback(new Error(responseObject.error.message));
} else {
callback(null, responseObject);
}
});
}).on('error', function (e) {
console.log("Communications error: " + e.message);
callback(new Error(e.message));
});
}

module.exports = {
getDealsByCentre : getDealsByCentre,
getDealsByCentreByStore : getDealsByCentreByStore,
getDealsByCentreByRetailer : getDealsByCentreByRetailer,
}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "dealer-skill",
"version": "1.0.0",
"description": "Gets the deals",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ltran/dealer-skill.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/ltran/dealer-skill/issues"
},
"homepage": "https://github.com/ltran/dealer-skill#readme"
}

0 comments on commit e88b580

Please sign in to comment.