Last active
March 1, 2018 22:15
-
-
Save jfdesrochers/03eb224a32d6b9037d1ad839519882be to your computer and use it in GitHub Desktop.
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
module['exports'] = function myService (hook) { | |
var request = require("request"); | |
const params = hook.params; | |
const res = hook.res; | |
const sku = hook.params.sku; | |
const zip = hook.params.zipcode; | |
res.setHeader('Content-Type', 'application/json'); | |
request(`https://www.staples.ca/fr/product_${sku}_1-CA_2_20001`, function (error, response, body) { | |
if (error) { | |
res.writeHead(500); | |
res.json({error: error}); | |
}; | |
const catRE = new RegExp(`StaplesCACAS\/fr-CA\/1-CA\/CL(\\d+)\/${sku}`, 'gi'); | |
let pricePath = catRE.exec(body); | |
if (pricePath) { | |
pricePath = `https://www.staples.ca/asgard-node/v1/nad/staplesus/price/${sku}?productDocKey=StaplesCACAS/fr-CA/1-CA/CL${pricePath[1]}/${sku}`; | |
} else { | |
res.writeHead(404); | |
res.json({error: 'Error with the supplied SKU (CAT)'}); | |
} | |
let desc = /<h1[\s\S]*?>[\s\S]*?(.+)[\s\S]*?<\/h1>/ig.exec(body); | |
if (desc) { | |
desc = desc[1]; | |
} else { | |
res.writeHead(404); | |
res.json({error: 'Error with the supplied SKU (H1)'}); | |
} | |
request({url: pricePath, headers: {'cookie': `zipcode=${zip};`}}, function (e, r, b) { | |
if (e) { | |
res.writeHead(500); | |
res.json({error: e}); | |
}; | |
b = JSON.parse(b); | |
res.json({ | |
sku: sku, | |
description: desc, | |
listPrice: b['pricing']['listPrice'], | |
finalPrice: b['pricing']['finalPrice'], | |
savings: b['pricing']['savings'] | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment