twitter-rest-lite
Advanced tools
Comparing version 0.3.5 to 0.3.6
276
lib/api.js
@@ -28,147 +28,165 @@ 'use strict'; | ||
// | ||
// <a name='constructor'></a> | ||
// Constructor | ||
// ----------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `uri` - base URI's to use (this should be provided by the | ||
// library itself) | ||
// + `opts` - `Object` with user-provided params | ||
// - `consumer_key` - required | ||
// - `consumer_secret` - required | ||
// - `token` - required | ||
// - `token_secret` - required | ||
// | ||
// #### Returns | ||
// | ||
// An `Object` with methods `get` and `post`. | ||
// | ||
// #### Code | ||
var API; | ||
function API(uri, opts) { | ||
this.uri = uri; | ||
module.exports = API = (function() { | ||
/* 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); | ||
}); | ||
// | ||
// <a name='constructor'></a> | ||
// Constructor | ||
// ----------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `uri` - base URI's to use (this should be provided by the | ||
// library itself) | ||
// + `opts` - `Object` with user-provided params | ||
// - `consumer_key` - required | ||
// - `consumer_secret` - required | ||
// - `token` - required | ||
// - `token_secret` - required | ||
// | ||
// #### Returns | ||
// | ||
// An `Object` with methods `get` and `post`. | ||
// | ||
// #### Code | ||
function API(uri, opts) { | ||
this.uri = uri; | ||
this.opts = opts; | ||
} | ||
/* 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); | ||
}); | ||
// | ||
// <a name='get'></a> | ||
// Public: Abstract GET request to the API | ||
// --------------------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `url` - String | ||
// + `params` - [Optional] Object with params to be passed | ||
// + `callback` - Callback Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with two parameters. First is an `Error Object` and second | ||
// the body of the response in an `Object`. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// api.get('/statuses/user_timeline.json', { | ||
// screen_name: 'random', | ||
// count: 1 | ||
// }, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// #### Code | ||
API.prototype.get = function(url, params, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
this.opts = opts; | ||
if (url == null || url === '' || typeof(url) !== 'string') { | ||
if (callback != null) { | ||
return callback(new Error( | ||
'Missing URL parameter' | ||
)); | ||
} else { | ||
throw new Error('Missing URL parameter'); | ||
} | ||
} | ||
// | ||
// <a name='get'></a> | ||
// Public: Abstract GET request to the API | ||
// --------------------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `url` - String | ||
// + `params` - [Optional] Object with params to be passed | ||
// + `callback` - Callback Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with two parameters. First is an `Error Object` and second | ||
// the body of the response in an `Object`. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// api.get('/statuses/user_timeline.json', { | ||
// screen_name: 'random', | ||
// count: 1 | ||
// }, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// #### Code | ||
API.prototype.get = function(url, params, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
url = self.uri.base + url; | ||
url = self.uri.base + url; | ||
if ((params != null) && (typeof params === 'object')) { | ||
var qs = require('querystring'); | ||
url += '?' + qs.stringify(params); | ||
} | ||
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) { | ||
if (err) | ||
return callback(err); | ||
request({ | ||
method: 'GET', | ||
uri: url, | ||
oauth: self.opts, | ||
json: true, | ||
}, function (err, response, body) { | ||
if (err) | ||
return callback(err); | ||
return callback(null, body); | ||
}); | ||
}; | ||
return callback(null, body); | ||
}); | ||
}; | ||
// | ||
// <a name='post'></a> | ||
// Public: abstract POST request to the API | ||
// ---------------------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `url` - String | ||
// + `data` - [Required] Object with data | ||
// + `callback` - Callback Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with two parameters: `Error Object` and `Object` with | ||
// body response from Twitter's API server. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// api.post('/statuses/update.json', { | ||
// status: "This is an update to twitter!" | ||
// }, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// #### Code | ||
API.prototype.post = function(url, data, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
var body = null; | ||
// | ||
// <a name='post'></a> | ||
// Public: abstract POST request to the API | ||
// ---------------------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `url` - String | ||
// + `data` - [Required] Object with data | ||
// + `callback` - Callback Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with two parameters: `Error Object` and `Object` with | ||
// body response from Twitter's API server. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// api.post('/statuses/update.json', { | ||
// status: "This is an update to twitter!" | ||
// }, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// #### Code | ||
API.prototype.post = function(url, data, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
var body = null; | ||
if (url == null || url === '' || typeof(url) !== 'string') { | ||
if (callback != null) { | ||
return callback(new Error( | ||
'Missing URL parameter' | ||
)); | ||
} else { | ||
throw new Error('Missing URL parameter'); | ||
} | ||
} | ||
url = self.uri.base + url; | ||
url = self.uri.base + url; | ||
if (data != null) | ||
body = JSON.stringify(data); | ||
if (data != null) | ||
body = JSON.stringify(data); | ||
request({ | ||
method: 'POST', | ||
uri: url, | ||
oauth: self.opts, | ||
form: data | ||
}, function (err, response, body) { | ||
if (err) | ||
return callback(err); | ||
request({ | ||
method: 'POST', | ||
uri: url, | ||
oauth: self.opts, | ||
form: data | ||
}, function (err, response, body) { | ||
if (err) | ||
return callback(err); | ||
return callback(null, body); | ||
}); | ||
}; | ||
return callback(null, body); | ||
}); | ||
}; | ||
return API; | ||
})(); | ||
module.exports = API; |
503
lib/oauth.js
@@ -38,284 +38,277 @@ 'use strict'; | ||
// | ||
// <a name='constructor'></a> | ||
// Constructor | ||
// ----------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `uri` - Object with the basic API URI's | ||
// + `opts` - Object with the following params | ||
// - `consumer_key` - [Required] consumer_key from Twitter | ||
// - `consumer_secret` - [Required] consumer_secret from Twitter | ||
// - `callback` - [Optional] | ||
// | ||
// #### Returns | ||
// | ||
// An `Object` with methods `requestToken`, `accessToken`, `authenticate` | ||
// and `authorize`. | ||
// | ||
// #### Code | ||
var OAuth; | ||
function OAuth(uri, opts) { | ||
this.uri = uri; | ||
module.exports = OAuth = (function() { | ||
/* Extending `uri` with oauth URI's */ | ||
this.uri.request_token = 'https://api.twitter.com/oauth/request_token'; | ||
this.uri.access_token = 'https://api.twitter.com/oauth/access_token'; | ||
this.uri.authenticate = 'https://api.twitter.com/oauth/authenticate'; | ||
this.uri.authorize = 'https://api.twitter.com/oauth/authorize'; | ||
// | ||
// <a name='constructor'></a> | ||
// Constructor | ||
// ----------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `uri` - Object with the basic API URI's | ||
// + `opts` - Object with the following params | ||
// - `consumer_key` - [Required] consumer_key from Twitter | ||
// - `consumer_secret` - [Required] consumer_secret from Twitter | ||
// - `callback` - [Optional] | ||
// | ||
// #### Returns | ||
// | ||
// An `Object` with methods `requestToken`, `accessToken`, `authenticate` | ||
// and `authorize`. | ||
// | ||
// #### Code | ||
function OAuth(uri, opts) { | ||
this.uri = uri; | ||
/* checking the required arguments */ | ||
['consumer_key', 'consumer_secret'].forEach(function (item) { | ||
if (opts[item] == null) | ||
throw new Error('There\'s a required argument missing: ' + item); | ||
}); | ||
/* Extending `uri` with oauth URI's */ | ||
this.uri.request_token = 'https://api.twitter.com/oauth/request_token'; | ||
this.uri.access_token = 'https://api.twitter.com/oauth/access_token'; | ||
this.uri.authenticate = 'https://api.twitter.com/oauth/authenticate'; | ||
this.uri.authorize = 'https://api.twitter.com/oauth/authorize'; | ||
this.opts = opts; | ||
} | ||
/* checking the required arguments */ | ||
['consumer_key', 'consumer_secret'].forEach(function (item) { | ||
if (opts[item] == null) | ||
throw new Error('There\'s a required argument missing: ' + item); | ||
}); | ||
// | ||
// <a name='requestToken'></a> | ||
// Public: get a request token | ||
// --------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// Returns a callback with an `Error Object` as first parameter if there was | ||
// (otherwise just `null`) and an `Object` with the response with the model: | ||
// | ||
// ```json | ||
// { | ||
// token: String, | ||
// token_secret: String, | ||
// oauth_callback_confirmed: Boolean | ||
// } | ||
// ``` | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.requestToken(function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// `response.token` is used by [`oauth.authenticate`](#authenticate) and | ||
// [`oauth.authorize`](#authorize). | ||
// | ||
// #### Code | ||
OAuth.prototype.requestToken = function(callback) { | ||
var self = this; | ||
var request = require('request'); | ||
var oauth = { | ||
consumer_key: self.opts.consumer_key, | ||
consumer_secret: self.opts.consumer_secret | ||
}; | ||
this.opts = opts; | ||
if (self.opts.callback != null) { | ||
oauth['callback'] = self.opts.callback; | ||
} | ||
// | ||
// <a name='requestToken'></a> | ||
// Public: get a request token | ||
// --------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// Returns a callback with an `Error Object` as first parameter if there was | ||
// (otherwise just `null`) and an `Object` with the response with the model: | ||
// | ||
// ```json | ||
// { | ||
// token: String, | ||
// token_secret: String, | ||
// oauth_callback_confirmed: Boolean | ||
// } | ||
// ``` | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.requestToken(function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// `response.token` is used by [`oauth.authenticate`](#authenticate) and | ||
// [`oauth.authorize`](#authorize). | ||
// | ||
// #### Code | ||
OAuth.prototype.requestToken = function(callback) { | ||
var self = this; | ||
var request = require('request'); | ||
var oauth = { | ||
consumer_key: self.opts.consumer_key, | ||
consumer_secret: self.opts.consumer_secret | ||
}; | ||
request({ | ||
method: 'POST', | ||
uri: self.uri.request_token, | ||
oauth: oauth | ||
}, function (err, response, body) { | ||
if (err) | ||
return callback(err); | ||
if (self.opts.callback != null) { | ||
oauth['callback'] = self.opts.callback; | ||
if (response.statusCode !== 200) { | ||
return callback(new Error( | ||
'Twitter:OAuth.requestToken received an status differente than 200: \n' + | ||
'Status Code: ' + response.statusCode + '\n' + | ||
'Body: \n' + body | ||
)); | ||
} | ||
request({ | ||
method: 'POST', | ||
uri: self.uri.request_token, | ||
oauth: oauth | ||
}, function (err, response, body) { | ||
if (err) | ||
return callback(err); | ||
var qs = require('querystring'); | ||
if (response.statusCode !== 200) { | ||
return callback(new Error( | ||
'Twitter:OAuth.requestToken received an status differente than 200: \n' + | ||
'Status Code: ' + response.statusCode + '\n' + | ||
'Body: \n' + body | ||
)); | ||
} | ||
return callback(null, qs.parse(body)); | ||
var qs = require('querystring'); | ||
}); | ||
}; | ||
return callback(null, qs.parse(body)); | ||
// | ||
// <a name='accessToken'></a> | ||
// Public: get an access token | ||
// --------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - `String` with `oauth_token` | ||
// + `verifier` - `String` with `oauth_verifier` | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as first parameter if there was | ||
// (otherwise just `null`) and an `Object` with the response with the model: | ||
// | ||
// ```js | ||
// { | ||
// oauth_token: String, | ||
// oauth_token_secret: String, | ||
// user_id: String, | ||
// screen_name: String | ||
// } | ||
// ``` | ||
// | ||
// #### Example | ||
// | ||
// After running either `oauth.authenticate` or `oauth.authorize` and | ||
// making the proper request to twitter's servers you will end up with | ||
// a `token` and a `verifier`. Suppose they are stored each in a variable | ||
// of the same name, then: | ||
// | ||
// ``` | ||
// oauth.accessToken(token, verifier, function (err, response) { | ||
// if (err) | ||
// throw (err); | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// With the data from that response you can initialize the API module and | ||
// start `GET`'ing and `POST`'ing with *user context* as Twitter calls it. | ||
// | ||
// #### Code | ||
OAuth.prototype.accessToken = function(token, verifier, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
}); | ||
[token, verifier].forEach(function (item) { | ||
if (item == null) { | ||
return callback(new Error( | ||
'Twitter:OAuth.accessToken requires all the arguments to work.' | ||
)); | ||
} | ||
}); | ||
var oauth = { | ||
consumer_key: self.opts.consumer_key, | ||
consumer_secret: self.opts.consumer_secret, | ||
token: token, | ||
verifier: verifier | ||
}; | ||
// | ||
// <a name='accessToken'></a> | ||
// Public: get an access token | ||
// --------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - `String` with `oauth_token` | ||
// + `verifier` - `String` with `oauth_verifier` | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as first parameter if there was | ||
// (otherwise just `null`) and an `Object` with the response with the model: | ||
// | ||
// ```js | ||
// { | ||
// oauth_token: String, | ||
// oauth_token_secret: String, | ||
// user_id: String, | ||
// screen_name: String | ||
// } | ||
// ``` | ||
// | ||
// #### Example | ||
// | ||
// After running either `oauth.authenticate` or `oauth.authorize` and | ||
// making the proper request to twitter's servers you will end up with | ||
// a `token` and a `verifier`. Suppose they are stored each in a variable | ||
// of the same name, then: | ||
// | ||
// ``` | ||
// oauth.accessToken(token, verifier, function (err, response) { | ||
// if (err) | ||
// throw (err); | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// With the data from that response you can initialize the API module and | ||
// start `GET`'ing and `POST`'ing with *user context* as Twitter calls it. | ||
// | ||
// #### Code | ||
OAuth.prototype.accessToken = function(token, verifier, callback) { | ||
var self = this; | ||
var request = require('request'); | ||
request({ | ||
method: 'POST', | ||
uri: self.uri.access_token, | ||
oauth: oauth | ||
}, function(err, response, body) { | ||
if (err) | ||
return callback(err); | ||
[token, verifier].forEach(function (item) { | ||
if (item == null) { | ||
return callback(new Error( | ||
'Twitter:OAuth.accessToken requires all the arguments to work.' | ||
)); | ||
} | ||
}); | ||
var oauth = { | ||
consumer_key: self.opts.consumer_key, | ||
consumer_secret: self.opts.consumer_secret, | ||
token: token, | ||
verifier: verifier | ||
}; | ||
var qs = require('querystring'); | ||
request({ | ||
method: 'POST', | ||
uri: self.uri.access_token, | ||
oauth: oauth | ||
}, function(err, response, body) { | ||
if (err) | ||
return callback(err); | ||
return callback(null, qs.parse(body)); | ||
}); | ||
var qs = require('querystring'); | ||
}; | ||
return callback(null, qs.parse(body)); | ||
}); | ||
// | ||
// <a name='authenticate'></a> | ||
// Public: get authenticate URL | ||
// ---------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - [Required] `String` with `oauth_token` from | ||
// `OAuth.requestToken`. | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as the first parameter and a `String` | ||
// with the URL to which redirect users as second parameter. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.authenticate(token, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// /* https://api.twitter.com/oauth/authenticate?oauth_token= + token provided */ | ||
// }); | ||
// ``` | ||
// | ||
// #### Code | ||
OAuth.prototype.authenticate = function(token, callback) { | ||
var self = this; | ||
}; | ||
if ((token == null) || (typeof token !== 'string')) { | ||
return callback(new Error( | ||
'Error: Twitter:OAuth.authenticate requires a token as first argument.' | ||
)); | ||
} | ||
// | ||
// <a name='authenticate'></a> | ||
// Public: get authenticate URL | ||
// ---------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - [Required] `String` with `oauth_token` from | ||
// `OAuth.requestToken`. | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as the first parameter and a `String` | ||
// with the URL to which redirect users as second parameter. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.authenticate(token, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// /* https://api.twitter.com/oauth/authenticate?oauth_token= + token provided */ | ||
// }); | ||
// ``` | ||
// | ||
// #### Code | ||
OAuth.prototype.authenticate = function(token, callback) { | ||
var self = this; | ||
return callback( | ||
null, | ||
self.uri.authenticate + '?oauth_token=' + token | ||
); | ||
if ((token == null) || (typeof token !== 'string')) { | ||
return callback(new Error( | ||
'Error: Twitter:OAuth.authenticate requires a token as first argument.' | ||
)); | ||
} | ||
}; | ||
return callback( | ||
null, | ||
self.uri.authenticate + '?oauth_token=' + token | ||
); | ||
// | ||
// <a name='authorize'></a> | ||
// Public: get authorize URL | ||
// ------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - [Required] `String` with `oauth_token` from | ||
// `OAuth.requestToken`. | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as the first parameter and a `String` | ||
// with the URL to which redirect users as second parameter. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.authorize(token, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// /* https://api.twitter.com/oauth/authorize?oauth_token= + token provided */ | ||
// }); | ||
// ``` | ||
// #### Code | ||
OAuth.prototype.authorize = function(token, callback) { | ||
var self = this; | ||
}; | ||
if ((token == null) || (typeof token !== 'string')) { | ||
return callback(new Error( | ||
'Error: Twitter:OAuth.authorize requires a token as first argument.' | ||
)); | ||
} | ||
// | ||
// <a name='authorize'></a> | ||
// Public: get authorize URL | ||
// ------------------------- | ||
// | ||
// #### Parameters | ||
// | ||
// + `token` - [Required] `String` with `oauth_token` from | ||
// `OAuth.requestToken`. | ||
// + `callback` - `Callback` Function | ||
// | ||
// #### Returns | ||
// | ||
// A `Callback` with an `Error` object as the first parameter and a `String` | ||
// with the URL to which redirect users as second parameter. | ||
// | ||
// #### Example | ||
// | ||
// ```js | ||
// oauth.authorize(token, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// /* https://api.twitter.com/oauth/authorize?oauth_token= + token provided */ | ||
// }); | ||
// ``` | ||
// #### Code | ||
OAuth.prototype.authorize = function(token, callback) { | ||
var self = this; | ||
return callback( | ||
null, | ||
self.uri.authorize + '?oauth_token=' + token | ||
); | ||
}; | ||
if ((token == null) || (typeof token !== 'string')) { | ||
return callback(new Error( | ||
'Error: Twitter:OAuth.authorize requires a token as first argument.' | ||
)); | ||
} | ||
return callback( | ||
null, | ||
self.uri.authorize + '?oauth_token=' + token | ||
); | ||
}; | ||
return OAuth; | ||
})(); | ||
module.exports = OAuth; |
{ | ||
"name": "twitter-rest-lite", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "Twitter's REST API Lite", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"start": "./node_modules/.bin/gulp", | ||
"test": "./node_modules/.bin/gulp test" | ||
}, | ||
@@ -24,6 +25,13 @@ "keywords": [ | ||
"devDependencies": { | ||
"express": "~3.4.8", | ||
"gulp": "^3.8.6", | ||
"gulp-coverage": "^0.1.24", | ||
"gulp-docco": "0.0.4", | ||
"gulp-gh-pages": "^0.3.3", | ||
"gulp-mocha": "^0.5.0", | ||
"gulp-rimraf": "^0.1.0", | ||
"gulp-subtree": "^0.1.0", | ||
"mocha": "~1.17.0", | ||
"should": "~3.1.0", | ||
"express": "~3.4.8", | ||
"optional": "~0.1.0-2" | ||
"optional": "~0.1.0-2", | ||
"should": "~3.1.0" | ||
}, | ||
@@ -30,0 +38,0 @@ "engines": { |
100
README.md
@@ -1,28 +0,63 @@ | ||
twitter-rest-lite [![Build Status](https://secure.travis-ci.org/ghostbar/twitter-rest-lite.png)](http://travis.ci.org/ghostbar/twitter-rest-lite) | ||
twitter-rest-lite [![Build Status](https://secure.travis-ci.org/ghostbar/twitter-rest-lite.png)](http://travis-ci.org/ghostbar/twitter-rest-lite) | ||
================= | ||
[![NPM](https://nodei.co/npm/twitter-rest-lite.png?stars&downloads)](https://nodei.co/npm/twitter-rest-lite/) | ||
A lite Twitter's API library for Node.js. It has two interfaces: a simple API interface for interacting via `GET` and `POST` requests with Twitter's API and an OAuth interface that takes care of authentication related stuff with Twitter's API. | ||
[![NPM](https://nodei.co/npm-dl/twitter-rest-lite.png)](https://nodei.co/npm/twitter-rest-lite/) | ||
API Interface | ||
------------- | ||
Yet another Twitter's API library for Node.js, yes. | ||
### Brief | ||
var tlite = require('twitter-rest-lite'); | ||
var twitter = new tlite.API(variableWithDevTwitterKeys); | ||
// You could use new tlite(keys) too but you would need to access to object in the form | ||
// 'twitter.api.get' instead of 'twitter.get'. | ||
twitter.get(url, params, callback); | ||
twitter.post(url, data, callback); | ||
### API Initialization | ||
The constructor expects to receive the keys in a JSON that will be used to interact with the API, this includes `consumer_key`, `consumer_secret`, `token` and `token_secret`. The last two can be found as well in the developers website of Twitter for doing requests with the app's credentials. | ||
### API.get | ||
The `get` method expects to receive an URL for the API as first parameter, then the parameters that the URL accepts in a JSON and a callback as third parameters with the regular «Error first, response after». | ||
This is an example of how it would look with a call to URL [`https://api.twitter.com/1.1/statuses/mentions_timeline.json`](https://dev.twitter.com/docs/api/1.1/get/statuses/mentions_timeline): | ||
twitter.get('/statuses/mentions_timeline.json', { | ||
count: 100 | ||
}, function (err, response) { | ||
// do whatever you want | ||
}); | ||
### API.post | ||
The `post` method expects to receive an URL for the API as first parameter, then the data (at the moment only JSON) to be sent and a callback as third paramenter with the regular «Error first, response after». | ||
This is an example of how it would look with a call to the URL [`https://api.twitter.com/1.1/statuses/update.json`](https://dev.twitter.com/docs/api/1.1/post/statuses/update): | ||
twitter.post('/statuses/update.json', { | ||
status: 'I\'m tweeting!' | ||
}, function (err, response) { | ||
// do whatever you want | ||
}); | ||
Testing | ||
------- | ||
In order to get full testing done (with `make test-all`), first create the file `test/config.json` with the following format: | ||
In order to get full testing done (with `gulp test-all`), first create the file `test/config.json` with the following format: | ||
```js | ||
{ | ||
"consumer_key": "Your credential from Twitter's Developer Interface", | ||
"consumer_secret": "Your credential from Twitter's Developer Interface", | ||
"token": "Your credential from Twitter's Developer Interface", | ||
"token_secret": "Your credential from Twitter's Developer Interface", | ||
"callback": "Either your callback or `oob` if is a desktop app" | ||
} | ||
``` | ||
{ | ||
"consumer_key": "Your credential from Twitter's Developer Interface", | ||
"consumer_secret": "Your credential from Twitter's Developer Interface", | ||
"token": "Your credential from Twitter's Developer Interface", | ||
"token_secret": "Your credential from Twitter's Developer Interface", | ||
"callback": "Either your callback or 'oob' if is a desktop app" | ||
} | ||
Now run: | ||
make test-all | ||
gulp test-all | ||
@@ -37,26 +72,22 @@ Known Issues | ||
```js | ||
var Twitter = require('twitter-rest-lite'), | ||
keys, | ||
tt, ttoauth, ttapi; | ||
var tlite = require('twitter-rest-lite'); | ||
keys = { consumer_key: 'blahblahblah', consumer_secret: 'blahblahblah', callback: '...' }; | ||
var keys = { consumer_key: 'blahblahblah', consumer_secret: 'blahblahblah', callback: '...' }; | ||
tt = new Twitter(keys); | ||
var tt = new Twitter(keys); | ||
// Just Twitter's OAuth REST interface | ||
ttoauth = new Twitter.OAuth(keys); | ||
// Just Twitter's OAuth REST interface | ||
var TwitterOAuth = new Twitter.OAuth(keys); | ||
// Using API module required `token` and `token_secret` on `keys`. | ||
keys['token'] = '...'; | ||
keys['token_secret'] = '...'; | ||
// Using API module required 'token' and 'token_secret' on 'keys'. | ||
keys['token'] = '...'; | ||
keys['token_secret'] = '...'; | ||
// Just Twitter's basic GET/POST interface | ||
ttapi = new Twitter.API(keys); | ||
// Just Twitter's basic GET/POST interface | ||
var twitter = new Twitter.API(keys); | ||
ttapi.get('/statuses/mentions_timeline.json', params, function(err, response) { | ||
... | ||
}); | ||
twitter.get('/statuses/mentions_timeline.json', params, function(err, response) { | ||
... | ||
}); | ||
``` | ||
@@ -71,2 +102,5 @@ Documentation | ||
------------------ | ||
© 2013-2014, Jose Luis Rivas `<me@ghostbar.co>`. Licensed under the MIT terms. A copy of the license is on the file `LICENSE`. | ||
© 2013-2014, Jose Luis Rivas `<me@ghostbar.co>`. | ||
Licensed under the MIT terms. A copy of the license is on the file `LICENSE`. |
@@ -22,3 +22,52 @@ /* global describe */ | ||
}); | ||
it('should throw an exception on missing arguments', function () { | ||
try { | ||
var api = new Twitter.API({ | ||
'consumer_key': config.consumer_key | ||
}); | ||
} catch (err) { | ||
should.exist(err); | ||
should.not.exist(api); | ||
} | ||
}); | ||
}); | ||
describe('API.proto.get()', function () { | ||
var api = new Twitter.API(config); | ||
it('should send an error if fails to get an URL (callback)', function (done) { | ||
api.get([], null, function (err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
}); | ||
it('should send an error if fails to get an URL (no-callback)', function () { | ||
try { | ||
api.get(''); | ||
} catch (err) { | ||
should.exist(err); | ||
} | ||
}); | ||
}); | ||
describe('API.proto.post()', function () { | ||
var api = new Twitter.API(config); | ||
it('should send an error if fails to get an URL (callback', function (done) { | ||
api.post([], null, function (err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
}); | ||
it('should send an error if fails to get an URL (no-callback)', function () { | ||
try { | ||
api.post(''); | ||
} catch (err) { | ||
should.exist(err); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -18,2 +18,13 @@ /* global describe */ | ||
}); | ||
it('should throw an exception on missing params', function () { | ||
try { | ||
var oauth = new Twitter.OAuth({ | ||
'consumer_key': config.consumer_key | ||
}); | ||
} catch (err) { | ||
should.exist(err); | ||
should.not.exist(oauth); | ||
} | ||
}); | ||
}); | ||
@@ -20,0 +31,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
786073
34
1545
105
11
1