Last active
August 29, 2015 14:23
-
-
Save franklingu/acc39375c669f9f64352 to your computer and use it in GitHub Desktop.
Yelp API Node v2
This file contains hidden or 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
| /* require the modules needed */ | |
| var oauthSignature = require('oauth-signature'); | |
| var n = require('nonce')(); | |
| var request = require('request'); | |
| var qs = require('querystring'); | |
| var _ = require('lodash'); | |
| var base_url = 'http://api.yelp.com/v2/'; | |
| var yelpConsumerKey = '<YOUR CONSUMER KEY>'; | |
| var yelpConsumerSecret = '<YOUR CONSUMER SECRET>'; | |
| var yelpToken = '<YOUR TOKEN>'; | |
| var yelpTokenSecret = '<YOUR TOKEN SECRET>'; | |
| /* Function for yelp search call | |
| * ------------------------ | |
| * params: object with params to search | |
| * callback: callback(error, response, body) | |
| */ | |
| module.exports.search = function (params, callback) { | |
| var httpMethod = 'GET'; | |
| var url = base_url + 'search'; | |
| makeRequestToYelp(httpMethod, url, params, callback); | |
| }; | |
| /* Function for yelp business call | |
| * ------------------------ | |
| * businessId: yelp businessId | |
| * params: object with params for API | |
| * callback: callback(error, response, body) | |
| */ | |
| module.exports.getBusiness = function (businessId, params, callback) { | |
| var httpMethod = 'GET'; | |
| var url = base_url + 'business/' + businessId; | |
| makeRequestToYelp(httpMethod, url, params, callback); | |
| }; | |
| function makeRequestToYelp(method, apiUrl, params, callback) { | |
| var required_params = { | |
| oauth_consumer_key : yelpConsumerKey, | |
| oauth_token : yelpToken, | |
| oauth_nonce : n(), | |
| oauth_timestamp : n().toString().substr(0,10), | |
| oauth_signature_method : 'HMAC-SHA1', | |
| oauth_version : '1.0' | |
| }; | |
| var parameters = _.assign(params, required_params); | |
| var consumerSecret = yelpConsumerSecret; | |
| var tokenSecret = yelpTokenSecret; | |
| var signature = oauthSignature.generate(method, apiUrl, parameters, consumerSecret, tokenSecret, { encodeSignature: false}); | |
| /* We add the signature to the list of paramters */ | |
| parameters.oauth_signature = signature; | |
| /* Then we turn the paramters object, to a query string */ | |
| var paramURL = qs.stringify(parameters); | |
| var apiURL = apiUrl + '?' + paramURL; | |
| /* Then we use request to send make the API Request */ | |
| request(apiURL, function(error, response, body){ | |
| return callback(error, response, body); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment