-1

I want to use this Node code https://arian.io/how-to-use-yelps-api-with-node/ in my Meteor app, to interface with Yelp's 2.0 API and return results that I can use.

I have installed meteorhacks:npm and added the appropriate node packages to packages.json:

{
"oauth-signature" : "1.3.0",
"nonce" : "1.0.3",
"request" : "2.58.0",
"lodash" : "3.10.0"
}

This is my .js file

//Start IsCLient
if (Meteor.isClient) {

//Call Yelp Helper
  Template.yelper.events({
    'click button': function () {
    data = callYelp();
    }
  });
//Call Yelp Helper


//Call Yelp
  callYelp = function callYelp(user) {
    Meteor.call('callYelp', user);
  }
//End Call Yelp

}
//End IsClient



if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}



//Methods
Meteor.methods({
'callYelp': function callYelp(user) {


/* require the modules needed */
var oauthSignature = Meteor.npmRequire('oauth-signature');  
var n = Meteor.npmRequire('nonce')();  
var request = Meteor.npmRequire('request');  
var qs = Meteor.npmRequire('querystring');  
var _ = Meteor.npmRequire('lodash');

/* Function for yelp call
 * ------------------------
 * set_parameters: object with params to search
 * callback: callback(error, response, body)
 */


request_yelp = function(set_parameters, callback) {


  /* The type of request */
  var httpMethod = 'GET';

  /* The url we are using for the request */
  var url = 'http://api.yelp.com/v2/search';

  /* We can setup default parameters here */
  var default_parameters = {
    location: 'San+Francisco',
    sort: '2'
  };

  /* We set the require parameters here */
  var required_parameters = {
    oauth_consumer_key : process.env.oauth_consumer_key,
    oauth_token : process.env.oauth_token,
    oauth_nonce : n(),
    oauth_timestamp : n().toString().substr(0,10),
    oauth_signature_method : 'HMAC-SHA1',
    oauth_version : '1.0'
  };

  /* We combine all the parameters in order of importance */ 
  var parameters = _.assign(default_parameters, set_parameters, required_parameters);

  /* We set our secrets here */
  var consumerSecret = process.env.consumerSecret;
  var tokenSecret = process.env.tokenSecret;

  /* Then we call Yelp's Oauth 1.0a server, and it returns a signature */
  /* Note: This signature is only good for 300 seconds after the oauth_timestamp */
  var signature = oauthSignature.generate(httpMethod, url, 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);

  /* Add the query string to the url */
  var apiURL = url+'?'+paramURL;

  /* Then we use request to send make the API Request */
  request(apiURL, function(error, response, body){
    return callback(error, response, body);
  });
};


}

})

Everything seems to include properly, and I believe I have things set up right, adding the code to a Method and then calling it with the button press.

My problem is that within the callYelp method, there is a function created that makes the actual request to Yelp, and I'm not sure how to fire that.

You can see in the comments in my code where I've tried to make it work.

Is this is a scope issue? An issue with Node?

Also I know I don't have my Yelp API parameters (token, etc) set yet, I will add those, I'm just trying to get it to at least connect and throw an error first.

request(apiURL, function(error, response, body){
    return callback(error, response, body);
  });
BSMP
  • 3,862
  • 8
  • 31
  • 41

1 Answers1

0

I believe you can make use of http package http://docs.meteor.com/#/full/http to make a call, remember to add the package before using it:

meteor add http

And remember to unblock before making a call or your meteor process will be block by it. This is example from meteor documentation page:

Meteor.methods({checkTwitter: function (userId) {
  check(userId, String);
  this.unblock();
  try {
    var result = HTTP.call("GET", "http://api.twitter.com/xyz",
                           {params: {user: userId}});
    return true;
  } catch (e) {
    // Got a network error, time-out or HTTP error in the 400 or 500 range.
    return false;
  }
}});
jwall
  • 761
  • 1
  • 8
  • 17