-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], "¢re_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"], "¢re_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"], "¢re_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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |