Created
April 16, 2016 21:33
-
-
Save jacobscarter/831f96317d079880f2d753f0c414f125 to your computer and use it in GitHub Desktop.
Using Twitter.prototype.postTweet
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
//TwitterService will now look like this | |
app.factory('TwitterService', function($http, $q){ | |
var createTweet = function(tweet){ | |
//we need to URL encode our string. | |
var urlEncodedTweet = encodeURI(tweet); | |
var d = $q.defer(); | |
$http.post('/twitter/tweet', {tweet: urlEncodedTweet}) | |
.success(function(data){ | |
return d.resolve(data); | |
}) | |
.error(function(error){ | |
return d.reject(error); | |
}); | |
return d.promise; | |
} | |
var getUser = function(username){ | |
var d = $q.defer(); | |
$http.post('/twitter/user', {username : username}) | |
.success(function(data){ | |
return d.resolve(data); | |
}) | |
.error(function(error){ | |
return d.reject(error); | |
}); | |
return d.promise; | |
}; | |
return { | |
getUser : getUser, | |
createTweet : createTweet | |
} | |
}); |
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
//Add the following to index.js | |
//post to update status (create a tweet) | |
//Note! params must be URL encoded, we will encode things in app.js | |
app.post('/twitter/tweet', function (req, res) { | |
var params = req.body.tweet; | |
var tweet = twitter.postTweet(params, function(error, response, body){ | |
res.status(400).send({ | |
"error" : "Oops something went wrong!" | |
}); | |
}, function(data){ | |
res.send({ | |
result : data | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment