Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit fedce12

Browse files
committed
chore(*): es6, promise based API
Write tests, move to es6, promise based API BREAKING CHANGE: phone_search -> phoneSearch
1 parent 97c8ccf commit fedce12

12 files changed

+401
-110
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [ "es2015" ]
3+
}

.eslintrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Use this file as a starting point for your project's .eslintrc.
2+
// Copy this file, and add rule overrides as needed.
3+
{
4+
"extends": "airbnb/base",
5+
"parser": "babel-eslint"
6+
}
7+

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
npm-debug.log
3+
.envrc

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- "4.1"
4+
- "4.0"
5+
- "0.12"
6+
- "0.11"
7+
- "0.10"
8+
- "iojs"

README.md

+40-16
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,66 @@
1-
[![NPM version](https://badge.fury.io/js/yelp.png)](http://badge.fury.io/js/yelp)
1+
[![Build Status](https://travis-ci.org/olalonde/node-yelp.svg?branch=master)](https://travis-ci.org/olalonde/node-yelp) [![NPM version](https://badge.fury.io/js/yelp.png)](http://badge.fury.io/js/yelp)
22

3-
Node.js module for interfacing with [Yelp](http://www.yelp.com)'s API v2.0
3+
Node.js module for interfacing with [Yelp](http://www.yelp.com)'s API
4+
v2.0. Supports both promises and callbacks.
45

5-
# Install #
6+
# Install
67

7-
npm install --save yelp
8+
```
9+
npm install --save yelp
10+
```
811

9-
# Usage #
12+
# Usage
1013

1114
```javascript
1215
// Request API access: http://www.yelp.com/developers/getting_started/api_access
16+
var Yelp = require('yelp');
1317

14-
var yelp = require("yelp").createClient({
15-
consumer_key: "consumer-key",
16-
consumer_secret: "consumer-secret",
17-
token: "token",
18-
token_secret: "token-secret"
18+
var yelp = new Yelp({
19+
consumer_key: 'consumer-key',
20+
consumer_secret: 'consumer-secret',
21+
token: 'token',
22+
token_secret: 'token-secret',
1923
});
2024

2125
// See http://www.yelp.com/developers/documentation/v2/search_api
22-
yelp.search({term: "food", location: "Montreal"}, function(error, data) {
23-
console.log(error);
26+
yelp.search({ term: 'food', location: 'Montreal' })
27+
.then(function (data) {
2428
console.log(data);
29+
})
30+
.catch(function (err) {
31+
console.error(err);
2532
});
2633

2734
// See http://www.yelp.com/developers/documentation/v2/business
28-
yelp.business("yelp-san-francisco", function(error, data) {
29-
console.log(error);
35+
yelp.business('yelp-san-francisco')
36+
.then(console.log)
37+
.catch(console.error);
38+
39+
yelp.phoneSearch({ phone: '+15555555555' })
40+
.then(console.log)
41+
.catch(console.error);
42+
43+
// A callback based API is also available:
44+
yelp.business('yelp-san-francisco', function(err, data) {
45+
if (err) return console.log(error);
3046
console.log(data);
3147
});
3248
```
3349

34-
# References #
50+
See [./test](./test) for more usage examples.
51+
52+
# References
3553

3654
- [Search API](http://www.yelp.com/developers/documentation/v2/search_api)
3755
- [Business API](http://www.yelp.com/developers/documentation/v2/business)
3856

39-
# License #
57+
# Test
58+
59+
```bash
60+
CONSUMER_KEY="" CONSUMER_SECRET="" TOKEN="" TOKEN_SECRET="" npm test
61+
```
62+
63+
# License
4064

4165
Copyright (c) 2012 Olivier Lalonde <[email protected]>
4266

demo/.gitignore

-1
This file was deleted.

demo/yelp.js

-19
This file was deleted.

lib/index.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
4+
5+
Object.defineProperty(exports, "__esModule", {
6+
value: true
7+
});
8+
9+
var _querystring = require('querystring');
10+
11+
var _querystring2 = _interopRequireDefault(_querystring);
12+
13+
var _oauth = require('oauth');
14+
15+
var _oauth2 = _interopRequireDefault(_oauth);
16+
17+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18+
19+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20+
21+
var OAuth = _oauth2.default.OAuth;
22+
23+
var baseUrl = 'http://api.yelp.com/v2/';
24+
25+
var Yelp = (function () {
26+
function Yelp(opts) {
27+
_classCallCheck(this, Yelp);
28+
29+
this.oauthToken = opts.token;
30+
this.oauthTokenSecret = opts.token_secret;
31+
this.oauth = new OAuth(null, null, opts.consumer_key, opts.consumer_secret, opts.version || '1.0', null, 'HMAC-SHA1');
32+
}
33+
34+
_createClass(Yelp, [{
35+
key: 'get',
36+
value: function get(resource, params, cb) {
37+
var _this = this;
38+
39+
var promise = new Promise(function (resolve, reject) {
40+
var debug = params.debug;
41+
delete params.debug;
42+
43+
_this.oauth.get(baseUrl + resource + '?' + _querystring2.default.stringify(params), _this.oauthToken, _this.oauthTokenSecret, function (err, _data, response) {
44+
if (err) return reject(err);
45+
var data = JSON.parse(_data);
46+
if (debug) return resolve([data, response]);
47+
resolve(data);
48+
});
49+
});
50+
if (typeof cb === 'function') {
51+
promise.then(function (res) {
52+
return cb(null, res);
53+
}).catch(cb);
54+
return null;
55+
}
56+
return promise;
57+
}
58+
}, {
59+
key: 'search',
60+
value: function search(params, callback) {
61+
return this.get('search', params, callback);
62+
}
63+
}, {
64+
key: 'business',
65+
value: function business(id, callback) {
66+
return this.get('business/' + id, null, callback);
67+
}
68+
69+
/**
70+
* Exampe:
71+
* yelp.phone_search({phone: "+12223334444"}, function(error, data) {});
72+
*/
73+
74+
}, {
75+
key: 'phoneSearch',
76+
value: function phoneSearch(params, callback) {
77+
return this.get('phone_search', params, callback);
78+
}
79+
}]);
80+
81+
return Yelp;
82+
})();
83+
84+
exports.default = Yelp;

lib/yelp.js

+83-61
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,84 @@
1-
var querystring = require('querystring');
2-
var OAuth = require('oauth').OAuth;
3-
4-
var Client = function(oauth_config) {
5-
this.oauthToken = oauth_config.token;
6-
this.oauthTokenSecret = oauth_config.token_secret;
7-
8-
this.oauth = new OAuth(
9-
null,
10-
null,
11-
oauth_config.consumer_key,
12-
oauth_config.consumer_secret,
13-
oauth_config.version || "1.0",
14-
null,
15-
'HMAC-SHA1'
16-
);
17-
18-
return this;
19-
};
20-
21-
var base_url = "http://api.yelp.com/v2/";
22-
23-
Client.prototype.get = function(resource, params, callback) {
24-
return this.oauth.get(
25-
base_url + resource + '?' + querystring.stringify(params),
26-
this.oauthToken,
27-
this.oauthTokenSecret,
28-
function(error, data, response) {
29-
if(!error) data = JSON.parse(data);
30-
callback(error, data, response);
1+
'use strict';
2+
3+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
4+
5+
Object.defineProperty(exports, "__esModule", {
6+
value: true
7+
});
8+
9+
var _querystring = require('querystring');
10+
11+
var _querystring2 = _interopRequireDefault(_querystring);
12+
13+
var _oauth = require('oauth');
14+
15+
var _oauth2 = _interopRequireDefault(_oauth);
16+
17+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18+
19+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20+
21+
var OAuth = _oauth2.default.OAuth;
22+
23+
var baseUrl = 'http://api.yelp.com/v2/';
24+
25+
var Yelp = (function () {
26+
function Yelp(opts) {
27+
_classCallCheck(this, Yelp);
28+
29+
this.oauthToken = opts.token;
30+
this.oauthTokenSecret = opts.token_secret;
31+
this.oauth = new OAuth(null, null, opts.consumer_key, opts.consumer_secret, opts.version || '1.0', null, 'HMAC-SHA1');
32+
}
33+
34+
_createClass(Yelp, [{
35+
key: 'get',
36+
value: function get(resource, params, cb) {
37+
var _this = this;
38+
39+
var promise = new Promise(function (resolve, reject) {
40+
var debug = params.debug;
41+
delete params.debug;
42+
43+
_this.oauth.get(baseUrl + resource + '?' + _querystring2.default.stringify(params), _this.oauthToken, _this.oauthTokenSecret, function (err, _data, response) {
44+
if (err) return reject(err);
45+
var data = JSON.parse(_data);
46+
if (debug) return resolve([data, response]);
47+
resolve(data);
48+
});
49+
});
50+
if (typeof cb === 'function') {
51+
promise.then(function (res) {
52+
return cb(null, res);
53+
}).catch(cb);
54+
return null;
55+
}
56+
return promise;
57+
}
58+
}, {
59+
key: 'search',
60+
value: function search(params, callback) {
61+
return this.get('search', params, callback);
62+
}
63+
}, {
64+
key: 'business',
65+
value: function business(id, callback) {
66+
return this.get('business/' + id, null, callback);
3167
}
32-
);
33-
}
34-
35-
/*
36-
Exampe:
37-
yelp.search({term: "food", location: "Montreal"}, function(error, data) {});
38-
*/
39-
Client.prototype.search = function(params, callback) {
40-
return this.get('search', params, callback);
41-
}
42-
43-
/*
44-
Example:
45-
yelp.business("yelp-san-francisco", function(error, data) {});
46-
*/
47-
Client.prototype.business = function(id, callback) {
48-
return this.get('business/' + id, null, callback);
49-
}
50-
51-
/*
52-
Exampe:
53-
yelp.phone_search({phone: "+12223334444"}, function(error, data) {});
54-
*/
55-
Client.prototype.phone_search = function(params, callback) {
56-
return this.get('phone_search', params, callback);
57-
}
58-
59-
// @see http://www.yelp.com/developers/documentation/v2/authentication
60-
module.exports.createClient = function(oauth_config) {
61-
return new Client(oauth_config);
62-
};
68+
69+
/**
70+
* Exampe:
71+
* yelp.phone_search({phone: "+12223334444"}, function(error, data) {});
72+
*/
73+
74+
}, {
75+
key: 'phoneSearch',
76+
value: function phoneSearch(params, callback) {
77+
return this.get('phone_search', params, callback);
78+
}
79+
}]);
80+
81+
return Yelp;
82+
})();
83+
84+
exports.default = Yelp;

0 commit comments

Comments
 (0)