Skip to content

Commit

Permalink
Merge pull request #26 from eengoron/master
Browse files Browse the repository at this point in the history
Add ability to use a lead search query string instead of an itemized list of search keywords
  • Loading branch information
pcothenet authored Nov 11, 2019
2 parents 1c344be + 90b3211 commit 9c8e4b8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,26 @@ closeio.lead.create({name: "Spider Man"})
console.log(err);
});
```

**Searching for Leads**

The `lead.search` method accepts either a string or a dictionary of search keywords as valid parameters.

To use a string to specify your search query, pass a `query` parameter to the `lead.search` method:

```javascript
closeio.lead.search({query: 'name:"Bruce Wayne" email_address:[email protected]'})
.then(function(search_results){
console.log(search_results.total_results);
});
```

To use a dictionary of search keywords to specify your search query, structure your parameters as follows:
```javascript
closeio.lead.search({name: "Bruce Wayne", email_address: '[email protected]'})
.then(function(search_results){
console.log(search_results.total_results);
});
```

**Note**: The `query` parameter will override any other search keywords present in your dictionary.
19 changes: 11 additions & 8 deletions lib/close.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ var Closeio = function(apiKey) {
parameters._fields = options.fields;
delete options.fields;
}

var option_keys = Object.keys(options);
if (option_keys.length > 0) {
parameters.query = '';
option_keys.forEach(function(option) {
var option_wrapper = (!/^".*"$/.test(option) && / +/.test(option)) ? '"' : '';
parameters.query += option_wrapper + option + option_wrapper + ':' + options[option] + ' ';
});
if ('query' in options) {
parameters.query = options.query;
} else {
var option_keys = Object.keys(options);
if (option_keys.length > 0) {
parameters.query = '';
option_keys.forEach(function(option) {
var option_wrapper = (!/^".*"$/.test(option) && / +/.test(option)) ? '"' : '';
parameters.query += option_wrapper + option + option_wrapper + ':' + options[option] + ' ';
});
}
}

return closeio._get('/lead/', parameters);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "close.io",
"preferGlobal": "false",
"version": "1.2.1",
"version": "1.3.0",
"author": "John Wehr <[email protected]>",
"description": "",
"contributors": [
Expand Down
10 changes: 9 additions & 1 deletion test/close.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ describe('Close.io API', function () {
});
});


it('should search a lead by query', function () {
var lead_id;
return closeio.lead.search({ query: 'name:"John Wehr"'})
.then(function (data) {
// console.log(data)
assert(data.data.length > 0);
});
});

after(function () {
return closeio.lead.delete(lead_id)
})
Expand Down

0 comments on commit 9c8e4b8

Please sign in to comment.