Skip to content

Commit

Permalink
feat(location): add filter option on cities (#25)
Browse files Browse the repository at this point in the history
* Ignore .idea folder

* Use getLocation() to filter on cities + update Readme

* Add tests for location and department
  • Loading branch information
Nico Cheutin authored and tdurieux committed Aug 4, 2018
1 parent e3bbcf1 commit ad8858b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
/coverage
/coverage
.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var search = new leboncoin.Search()
.setCategory("locations")
.setRegion("ile_de_france")
.setDepartment("yvelines")
.setLocation([
{"zipcode": "78100"},
{"zipcode": "78000"},
])
.addSearchExtra("price", {min: 1500, max: 2000}) // will add a range of price
.addSearchExtra('furnished', ["1", "Non meublé"]); // will add enums for Meublé and Non meublé

Expand Down
8 changes: 6 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ const getSubCategoriesExtras = (currentCategoryId, searchExtras) => {
return enums;
};

const getLocation = (region, department) => {
const getLocation = (region, department, city_zipcodes) => {
var location = {};

if (region) {
Expand All @@ -273,6 +273,10 @@ const getLocation = (region, department) => {
location.department = department;
}

if (city_zipcodes) {
location.city_zipcodes = city_zipcodes;
}

return location;
}

Expand All @@ -286,7 +290,7 @@ Search.prototype.getBodyParams = function () {
"filters": {
"category": { "id": this.category },
"enums": getSubCategoriesExtras(this.category, this.searchExtras),
"location": getLocation(this.region, this.department),
"location": getLocation(this.region, this.department, this.location),
"keywords": getKeywords(this.query),
"ranges": getSubCategoriesRanges(this.category, this.searchExtras)
},
Expand Down
22 changes: 22 additions & 0 deletions test/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ describe('Search', function() {

done();
});

it('check search with location', function (done) {
var s = new search.Search()
.setRegion("ile_de_france")
.setDepartment("yvelines")
.setPage(1);

var bodyParams = JSON.stringify(s.getBodyParams())
var expectedBodyParams = "{\"limit\":35,\"filters\":{\"category\":{\"id\":null},\"enums\":{\"ad_type\":[\"offer\"]},\"location\":{\"region\":\"12\",\"department\":\"78\"},\"keywords\":{},\"ranges\":{}},\"offset\":0}";
bodyParams.should.equal(expectedBodyParams);

var s = new search.Search()
.setRegion("ile_de_france")
.setLocation([{"zipcode":"78100"}])
.setPage(1);

var bodyParams = JSON.stringify(s.getBodyParams())
var expectedBodyParams = "{\"limit\":35,\"filters\":{\"category\":{\"id\":null},\"enums\":{\"ad_type\":[\"offer\"]},\"location\":{\"region\":\"12\",\"city_zipcodes\":[{\"zipcode\":\"78100\"}]},\"keywords\":{},\"ranges\":{}},\"offset\":0}";
bodyParams.should.equal(expectedBodyParams);

done();
});
});

// describe('Parsers', function() {
Expand Down

0 comments on commit ad8858b

Please sign in to comment.