twitter-rest-lite
Advanced tools
Comparing version 0.3.0 to 0.3.2
170
index.js
@@ -0,22 +1,104 @@ | ||
// twitter-rest-lite | ||
// ================= | ||
// | ||
// A lightweight Twitter REST-API library with [OAuth](oauth.html) | ||
// and basic `POST`/`GET` [API](api.html) requests modules. | ||
// | ||
// For more convenient methods you should check [`twitter-rest`](https://github.com/ghostbar/twitter-rest). | ||
// | ||
var API = require('./lib/api'), | ||
OAuth = require('./lib/oauth'), | ||
uri = { | ||
base: 'https://api.twitter.com/1.1', | ||
search: 'https://api.twitter.com/1.1/search' | ||
}; | ||
uri; | ||
/** | ||
* Main | ||
* ==== | ||
* | ||
* All the exported functions expect an Object with the params: | ||
* | ||
* consumer_key - [Required] | ||
* consumer_secret - [Required] | ||
* access_token_key - [Optional] | ||
* access_token_secret - [Optional, but required if `access_token_key` was given] | ||
* callback - [Optional] | ||
* | ||
* The main will export all the functions implemented. | ||
**/ | ||
// | ||
// Quick Usage | ||
// ----------- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-list'), | ||
// twitter = new TwitterLib({ | ||
// consumer_key: "blahblahblah", | ||
// consumer_secret: "blahblahblah", | ||
// token: 'blah', | ||
// token_secret: 'blah', | ||
// callback: "randomurl" | ||
// }); | ||
// | ||
// twitter.api.get('/statuses/user_timeline.json', { | ||
// screen_name: 'twitter', | ||
// count: 1 | ||
// }, function (err, response) { | ||
// if (err) throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
/// #### PLEASE BE WARNED | ||
// | ||
// Using the complete `require` is only recommended if `token` and | ||
// `token_secret` already exists. | ||
// | ||
// Otherwise the API module will throw an Error since it does need those | ||
// two variables to do any of the calls./ | ||
// | ||
// What's available on the initialized object? | ||
// ------------------------------------------- | ||
// | ||
// Initializes two objects: `api` and `oauth`. You can initialize them | ||
// separated too (this is my preferred method). | ||
// | ||
// Parameters to initialize any of the exported Objects | ||
// ---------------------------------------------------- | ||
// | ||
// All the exported functions expect an Object with the params: | ||
// | ||
// + `consumer_key` - (Required) consumer key given by Twitter | ||
// + `consumer_secret` - (Required) consumer secret given by Twitter | ||
// + `token` - (Optional) access_token key given by Twitter | ||
// + `token_secret` - (Required if `access_token_key` was given) | ||
// given by Twitter | ||
// + `callback` - (Optional) If your app is a desktop app write `oob` | ||
// (Out-Of-Band); if not then you should write your callback URL here | ||
// (which will rewrite the one configured on Twitter's developer dashboard. | ||
// | ||
// | ||
// Base URIs for Twitter API (These should be overwritten if to be used | ||
// with a compatible API) | ||
// | ||
uri = { | ||
base: 'https://api.twitter.com/1.1', | ||
search: 'https://api.twitter.com/1.1/search' | ||
}; | ||
// | ||
// Usage | ||
// ----- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-lite'), | ||
// keys = {consumer_key: '...', consumer_secret: '...', token: '...', token_secret: '...' callback: '...'}, | ||
// twitter = new TwitterLib(keys); | ||
// | ||
// /* twitter.oauth object */ | ||
// twitter.oauth.requestToken( /* ... */ ); | ||
// twitter.oauth.accessToken( /* ... */ ); | ||
// twitter.oauth.authenticate( /* ... */ ); | ||
// twitter.oauth.authorize( /* ... */ ); | ||
// | ||
// /* twitter.api object */ | ||
// twitter.api.get( /* ... */ ); | ||
// twitter.api.post( /* ... */ ); | ||
// ``` | ||
// | ||
// #### PLEASE BE WARNED | ||
// | ||
// Using the complete `require` is only recommended if `token` and | ||
// `token_secret` already exists. | ||
// | ||
// Otherwise the API module will throw an Error since it does need those | ||
// two variables to do any of the calls. | ||
// | ||
// #### Code | ||
module.exports = function(opts) { | ||
@@ -29,8 +111,56 @@ return { | ||
module.exports.OAuth = function(opts) { | ||
// OAuth Quick Usage | ||
// ----------------- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-lite'), | ||
// toauth = new TwitterLib.OAuth({ | ||
// consumer_key: 'blah', | ||
// consumer_secret: 'blah', | ||
// callback: 'randomurl' | ||
// }); | ||
// | ||
// toauth.requestToken(function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// More on the [OAuth module](oauth.html) documentation. | ||
// | ||
// #### Code | ||
module.exports.OAuth = module.exports.oauth = function(opts) { | ||
return new OAuth(uri, opts); | ||
}; | ||
module.exports.API = function(opts) { | ||
// API Quick Usage | ||
// --------------- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-lite'), | ||
// tapi = new TwitterLib.API({ | ||
// consumer_key: 'blah', | ||
// consumer_secret: 'blah', | ||
// token: 'blah', | ||
// token_secret: 'blah', | ||
// callback: 'randomurl' | ||
// }); | ||
// | ||
// tapi.get('/statuses/user_timeline.json', { | ||
// screen_name: 'twitter' | ||
// }, function (err, response) { | ||
// if (err) | ||
// throw err; | ||
// | ||
// console.log(response); | ||
// }); | ||
// ``` | ||
// | ||
// More on the [API module](api.html) documentation. | ||
// | ||
// #### Code | ||
module.exports.API = module.exports.api = function(opts) { | ||
return new API(uri, opts); | ||
}; |
144
lib/api.js
@@ -0,22 +1,64 @@ | ||
// | ||
// Module: API | ||
// =========== | ||
// | ||
// Abstraction for the basic `GET`/`POST` operations of Twitter's REST API. | ||
// | ||
// Methods | ||
// ------- | ||
// | ||
// + [Constructor/Initialize](#constructor) | ||
// + [GET](#get) | ||
// + [POST](#post) | ||
// | ||
// Usage | ||
// ----- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-lite'), | ||
// api = new TwitterLib.API(var_with_keys); | ||
// | ||
// api.get(url, params, callback); | ||
// | ||
// api.post(url, data, callback); | ||
// ``` | ||
// | ||
// #### Code | ||
var API, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
/** | ||
* Module: API | ||
* =========== | ||
* | ||
* Abstraction for the basic GET/POST operations. | ||
**/ | ||
module.exports = API = (function() { | ||
/** | ||
* <a name='constructor'></a> | ||
* Constructor | ||
* ----------- | ||
**/ | ||
// | ||
// <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; | ||
/* checking the required arguments */ | ||
['consumer_key', 'consumer_secret', 'token', 'token_secret'].forEach(function (item, index) { | ||
if (opts[item] == null) | ||
throw new Error("There's a required argument missing: " + item); | ||
}); | ||
this.opts = opts; | ||
// bindings | ||
/* bindings */ | ||
this.get = __bind(this.get, this); | ||
@@ -26,11 +68,32 @@ this.post = __bind(this.post, this); | ||
/** | ||
* <a name='get'></a> | ||
* Public: Abstract GET request to the API | ||
* --------------------------------------- | ||
* | ||
* url - String | ||
* params - [Optional] Object with params to be passed | ||
* callback - Callback Function | ||
**/ | ||
// | ||
// <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) { | ||
@@ -60,11 +123,32 @@ var self = this, | ||
/** | ||
* <a name='post'></a> | ||
* Public: abstract POST request to the API | ||
* ---------------------------------------- | ||
* | ||
* url - String | ||
* data - [Required] Object with data | ||
* callback - Callback Function | ||
**/ | ||
// | ||
// <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) { | ||
@@ -71,0 +155,0 @@ var self = this, |
282
lib/oauth.js
@@ -0,31 +1,65 @@ | ||
// | ||
// Module: OAuth | ||
// ============= | ||
// | ||
// Abstraction for the authentication methods of Twitter's API. | ||
// | ||
// **Notice**: At the moment this is depending on `request`'s ability to create OAuth | ||
// signatures, but in the future this should have it's own OAuth signing with | ||
// OAuth2 support. | ||
// | ||
// Methods | ||
// ------- | ||
// | ||
// + [Constructor](#constructor) | ||
// + [Request Token](#requestToken) | ||
// + [Access Token](#accessToken) | ||
// + [Authenticate](#authenticate) | ||
// + [Authorize](#authorize) | ||
// | ||
// Usage | ||
// ----- | ||
// | ||
// ``` | ||
// var TwitterLib = require('twitter-rest-lite'), | ||
// oauth = new TwitterLib.OAuth(var_with_keys); | ||
// | ||
// api.requestToken(callback); | ||
// | ||
// api.accessToken(token, verifier, callback); | ||
// | ||
// api.authenticate(callback); | ||
// | ||
// api.authorize(callback); | ||
// ``` | ||
// | ||
// #### Code | ||
var OAuth, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
/** | ||
* Module: OAuth | ||
* ============= | ||
* | ||
* Abstraction for the authentication | ||
* | ||
* At the moment this is depending on `request`'s ability to create OAuth | ||
* signatures, but in the future this should have it's own OAuth signing with | ||
* OAuth2 support. | ||
**/ | ||
module.exports = OAuth = (function() { | ||
/** | ||
* <a name='constructor'></a> | ||
* Constructor | ||
* ----------- | ||
* | ||
* 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] | ||
**/ | ||
// | ||
// <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; | ||
// Extending `uri` with oauth URI's | ||
/* Extending `uri` with oauth URI's */ | ||
this.uri.request_token = 'https://api.twitter.com/oauth/request_token'; | ||
@@ -36,3 +70,3 @@ this.uri.access_token = 'https://api.twitter.com/oauth/access_token'; | ||
// checking the required arguments | ||
/* checking the required arguments */ | ||
['consumer_key', 'consumer_secret'].forEach(function (item, index) { | ||
@@ -45,3 +79,3 @@ if (opts[item] == null) | ||
// bindings | ||
/* bindings */ | ||
this.requestToken = __bind(this.requestToken, this); | ||
@@ -53,20 +87,39 @@ this.accessToken = __bind(this.accessToken, this); | ||
/** | ||
* <a name='requestToken'></a> | ||
* Public: get a request token | ||
* --------------------------- | ||
* | ||
* 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, | ||
* oauth_callback_confirmed: Boolena | ||
* } | ||
* ``` | ||
**/ | ||
// | ||
// <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) { | ||
@@ -107,21 +160,47 @@ var self = this, | ||
/** | ||
* <a name='accessToken'></a> | ||
* Public: get an access token | ||
* --------------------------- | ||
* | ||
* 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_ FIXME | ||
* } | ||
* ``` | ||
**/ | ||
// | ||
// <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) { | ||
@@ -161,14 +240,31 @@ var self = this, | ||
/** | ||
* <a name='authenticate'></a> | ||
* Public: get authenticate URL | ||
* ---------------------------- | ||
* | ||
* 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. | ||
**/ | ||
// | ||
// <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) { | ||
@@ -190,14 +286,30 @@ var self = this; | ||
/** | ||
* <a name='authorize'></a> | ||
* Public: get authorize URL | ||
* ------------------------- | ||
* | ||
* 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. | ||
**/ | ||
// | ||
// <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) { | ||
@@ -204,0 +316,0 @@ var self = this; |
{ | ||
"name": "twitter-rest-lite", | ||
"version": "0.3.0", | ||
"version": "0.3.2", | ||
"description": "Twitter's REST API Lite", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
twitter-rest-lite | ||
================= | ||
[![NPM](https://nodei.co/npm/twitter-rest-lite.png?stars&downloads)](https://nodei.co/npm/twitter-rest-lite/) | ||
[![NPM](https://nodei.co/npm-dl/twitter-rest-lite.png)](https://nodei.co/npm/twitter-rest-lite/) | ||
Yet another Twitter's API library for Node.js, yes. | ||
@@ -45,4 +49,5 @@ | ||
keys['access_token'] = '...'; | ||
keys['access_token_secret'] = '...'; | ||
// Using API module required `token` and `token_secret` on `keys`. | ||
keys['token'] = '...'; | ||
keys['token_secret'] = '...'; | ||
@@ -58,4 +63,10 @@ // Just Twitter's basic GET/POST interface | ||
Documentation | ||
------------- | ||
[http://ghostbar.github.io/twitter-rest-lite](http://ghostbar.github.io/twitter-rest-lite) and/or | ||
`docs/` and/or the code itself. It's the same documentation. | ||
License and author | ||
------------------ | ||
© 2013, 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`. |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
612909
29
1372
71
1