Created
June 13, 2012 08:35
-
-
Save nachiket-p/2922814 to your computer and use it in GitHub Desktop.
Meteor: Make HTTP calls to remote services (http://www.jumpbyte.com/2012/meteor-make-ht…remote-service/)
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
if (Meteor.is_client) { | |
var userName = "PatelNachiket"; | |
Template.hello.greeting = function () { | |
return "Fetch recent tweets from Twitter stream of user : " ; | |
}; | |
Template.hello.events = { | |
'click #fetchButton' : function () { | |
console.log("Recent tweets from stream!"); | |
$('#fetchButton').attr('disabled','true').val('loading...'); | |
userName = $('#userName').val(); | |
Meteor.call('fetchFromService', userName, function(err, respJson) { | |
if(err) { | |
window.alert("Error: " + err.reason); | |
console.log("error occured on receiving data on server. ", err ); | |
} else { | |
console.log("respJson: ", respJson); | |
//window.alert(respJson.length + ' tweets received.'); | |
Session.set("recentTweets",respJson); | |
} | |
$('#fetchButton').removeAttr('disabled').val('Fetch'); | |
}); | |
} | |
}; | |
Template.hello.recentTweets = function() { | |
return Session.get("recentTweets") || []; | |
} | |
Template.hello.userName = function() { | |
return userName; | |
} | |
} | |
if (Meteor.is_server) { | |
Meteor.methods({ | |
fetchFromService: function(userName) { | |
var url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name="+userName+"&count=10"; | |
//synchronous GET | |
var result = Meteor.http.get(url, {timeout:30000}); | |
if(result.statusCode==200) { | |
var respJson = JSON.parse(result.content); | |
console.log("response received."); | |
return respJson; | |
} else { | |
console.log("Response issue: ", result.statusCode); | |
var errorJson = JSON.parse(result.content); | |
throw new Meteor.Error(result.statusCode, errorJson.error); | |
} | |
} | |
}); | |
} |
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
<head> | |
<title>Meteor - twitter public timeline demo</title> | |
</head> | |
<body> | |
{{> hello}} | |
</body> | |
<template name="hello"> | |
<h3>Meteor: calling 3rd party service</h3> | |
{{greeting}} <input type="text" id="userName" value="{{userName}}"></input> | |
<input type="button" value="Fetch" id="fetchButton" /> | |
<ul> | |
{{#each recentTweets}} | |
<li>{{text}}</li> | |
{{/each}} | |
</ul> | |
</template> |
Thanks for sharing. It is helpful
seems Meteor.http
is deprecated, you can use meteor add http
package to HTTP.get
Thanks @luckyyang
Thanks for sharing this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog about this gist: http://www.jumpbyte.com/2012/meteor-make-ht…remote-service/
live demo: http://remoteservice.meteor.com
Steps to run this sample code