Skip to content

Commit

Permalink
feat(stores): list stores per country
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Feb 8, 2017
1 parent e930bef commit 840fb46
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The default reports prints the results as a human readable table.
## Plans

- [ ] include store names
- [ ] include country names
- [X] include country names
- [ ] add command for listing stores in countries
`cli.js stores de`
- [ ] add command to list / search for products by id / name using http://www.ikea.com/de/de/catalog/productsaz/0/ where 0 = A and 25 = Z
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dependencies": {
"chai": "^3.5.0",
"chalk": "^1.1.3",
"cheerio": "^0.22.0",
"cli-table": "^0.3.1",
"commander": "^2.9.0",
"debug": "^2.6.0",
Expand Down
57 changes: 57 additions & 0 deletions source/cli-stores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env node
'use strict';

let program = require('commander');
let cheerio = require('cheerio');
let pkg = require('./../package.json');

program
.version(pkg.version)
.arguments('[countryCodes...]')
.description(
'Will list the IKEA stores found in a country by scraping an example ' +
'detail page from ikea.com or other domains and extracting the store ' +
'buCodes and names'
)
.action(function(countryCodes) {
// make countryCodes unique and lowercase
countryCodes = countryCodes
.map(function(countryCode) {
return countryCode.toLowerCase();
})
.filter(function(cur, i, arr) {
return arr.indexOf(cur, i + 1) === -1;
});

countryCodes.forEach(function(countryCode) {
// trying to use a fixed productId of a product which must be popular in
// all countries
let productId = '30275861';
let languageCode = countryCode;
let url = `http://www.ikea.com/${countryCode}/${languageCode}/catalog/products/${productId}/`;

let reporter = require('./lib/reporter/stores-table');
let request = require('request');
request.get(url, function(err, response) {
let $ = cheerio.load(response.body);
let storeDropdownElm = $('#ikeaStoreNumber1');
let stores = storeDropdownElm.find('option')
.map(function(index, elm) {
return {
buCode: $(elm).attr('value'),
name: $(elm).text().replace(/IKEA\s+/, ''),
countryCode: countryCode,
};
})
.toArray()
// filter stores where buCode is 3-letter digit and store name is
// not empty
.filter(function(store) {
return store.buCode && store.buCode.match(/^\d+$/) && store.name;
});

console.log(reporter.show(stores));
}); // forEach
}); // action
})
.parse(process.argv);
1 change: 1 addition & 0 deletions source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ let pkg = require('./../package.json');
program
.version(pkg.version)
.command('stock', 'check the availability of one or multiple products')
.command('stores', 'list stores in a specific country')
.parse(process.argv);
46 changes: 46 additions & 0 deletions source/lib/reporter/stores-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

let Table = require('cli-table');
let countries = require('i18n-iso-countries');

module.exports = {
show: function(stores) {
let table = new Table({
head: [
'countryCode',
'country',
'buCode',
'name',
],
});

// sort data by countryCode and buCode in ascending order
stores.sort(function(a, b) {
if (a.countryCode > b.countryCode) {
return 1;
} else if (a.countryCode < b.countryCode) {
return -1;
}
return 0;
});
stores.sort(function(a, b) {
if (a.buCode > b.buCode) {
return 1;
} else if (a.buCode < b.buCode) {
return -1;
}
return 0;
});

stores.forEach(function(item) {
table.push([
item.countryCode,
countries.getName(item.countryCode, 'en'),
item.buCode,
item.name,
]);
});

return table.toString();
},
};

0 comments on commit 840fb46

Please sign in to comment.