'use strict';
Abstraction for the basic GET
/POST
operations of Twitter’s REST API.
var TwitterLib = require('twitter-rest-lite'),
api = new TwitterLib.API(var_with_keys);
api.get(url, params, callback);
api.post(url, data, callback);
uri
- base URI’s to use (this should be provided by the
library itself)opts
- Object
with user-provided paramsconsumer_key
- requiredconsumer_secret
- requiredtoken
- requiredtoken_secret
- requiredAn Object
with methods get
and post
.
function API(uri, opts) {
this.uri = uri;
/* checking the required arguments */
[
'consumer_key',
'consumer_secret',
'token',
'token_secret'
].forEach(function (item) {
if (opts[item] == null)
throw new Error('There\'s a required argument missing: ' + item);
});
this.opts = opts;
}
url
- Stringparams
- [Optional] Object with params to be passedcallback
- Callback FunctionA Callback
with two parameters. First is an Error Object
and second
the body of the response in an Object
.
api.get('/statuses/user_timeline.json', {
screen_name: 'random',
count: 1
}, function (err, response) {
if (err)
throw err;
console.log(response);
});
API.prototype.get = function(url, params, callback) {
var self = this;
var request = require('request');
var helper = require('./helper');
helper.check(url, 'string', '', 'Missing URL parameter', callback);
url = self.uri.base + url;
if ((params != null) && (typeof params === 'object')) {
var qs = require('querystring');
url += '?' + qs.stringify(params);
}
request({
method: 'GET',
uri: url,
oauth: self.opts,
json: true,
}, function (err, response, body) {
return callback(err, body);
});
};
url
- Stringdata
- [Required] Object with datacallback
- Callback FunctionA Callback
with two parameters: Error Object
and Object
with
body response from Twitter’s API server.
api.post('/statuses/update.json', {
status: "This is an update to twitter!"
}, function (err, response) {
if (err)
throw err;
console.log(response);
});
API.prototype.post = function(url, data, callback) {
var self = this;
var request = require('request');
var helper = require('./helper');
if (helper.check(url, 'string', '', 'Missing URL parameter', callback))
return;
if (helper.check(data, 'object', {}, 'Missing data parameter', callback))
return;
request({
method: 'POST',
uri: self.uri.base + url,
oauth: self.opts,
form: data
}, function (err, response, body) {
return callback(err, body);
});
};
module.exports = API;