Skip to content

Instantly share code, notes, and snippets.

@DanAncona
Last active September 11, 2017 14:43
Show Gist options
  • Save DanAncona/a09ce375e48bfa8efeca to your computer and use it in GitHub Desktop.
Save DanAncona/a09ce375e48bfa8efeca to your computer and use it in GitHub Desktop.
Meteor vs. the Twitter API via wrapAsync and Twit

Twit and other Node libraries don't always quite drop into Meteor's reactive architecture right out of the box. Luckily Meteor makes adapting them super-duper easy with wrapAsync. It was so easy that I naturally tried all the more difficult ways first! Since I didn't find a nice gist or StackOverflow example, please enjoy this one.

In the template -

{{#with pos}}
  {{#if ready}}
    {{> singlePosition data }}
  {{/if}}
{{/with}}

In client.js -

Template.home.helpers({
  pos: function () {
    var ready = Meteor.subscribe('positions').ready();
    apos = Positions.findOne({}, {sort: {'submittedOn': -1}});

    if (apos && !apos.targetImg) {
      console.log('calling targetImg for ' + apos.target);
      var targetImg = Meteor.call("getTwitImg", apos.target, function(error, results) {
        if (results)
          Positions.update(apos._id, {$set: {'targetImg': results}});  
      });
    }

    return {
      data: apos,
      ready: ready
    };
  },
})

In server.js -

var T = new TwitMaker({
    consumer_key:         '...'
  , consumer_secret:      '...'
  , access_token:         '...'
  , access_token_secret:  '...'
})

var wrapGet = Meteor.wrapAsync(T.get, T);

Meteor.methods({

  getTwitImg: function(target) {
    data = wrapGet('users/show', {screen_name: target});
    if (data) {
      img_url = data['profile_image_url'];
      US.update({twitter: target}, {$set: {'targetImg': img_url}});
      return img_url;
    }
  }
  
});

Way easier than manually using bindEnvironment or Futures!

@illuminaut
Copy link

How do you deal with errors thrown in your wrapGet function? How do you return those as the error parameter in the callback? I tried enclosing the function in a try/catch block, which gives me a nice error message, but then how do you pass that on to the callback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment