Coverage

66% line coverage
61% statement coverage
60% block coverage
128 SLOC

index.js

66% block coverage
16 SLOC
LineHitsStatementsSourceAction
11100%'use strict';
2 not covered // twitter-rest-lite
3 not covered // =================
4 not covered //
5 not covered // A lightweight Twitter REST-API library with [OAuth](oauth.html)
6 not covered // and basic `POST`/`GET` [API](api.html) requests modules.
7 not covered //
8 not covered // For more convenient methods you should check [`twitter-rest`](https://github.com/ghostbar/twitter-rest).
9 not covered //
10 not covered
111100%var API = require('./lib/api');
121100%var OAuth = require('./lib/oauth');
13 not covered
14 not covered //
15 not covered // Quick Usage
16 not covered // -----------
17 not covered //
18 not covered // ```
19 not covered // var TwitterLib = require('twitter-rest-list'),
20 not covered // twitter = new TwitterLib({
21 not covered // consumer_key: "blahblahblah",
22 not covered // consumer_secret: "blahblahblah",
23 not covered // token: 'blah',
24 not covered // token_secret: 'blah',
25 not covered // callback: "randomurl"
26 not covered // });
27 not covered //
28 not covered // twitter.api.get('/statuses/user_timeline.json', {
29 not covered // screen_name: 'twitter',
30 not covered // count: 1
31 not covered // }, function (err, response) {
32 not covered // if (err) throw err;
33 not covered //
34 not covered // console.log(response);
35 not covered // });
36 not covered // ```
37 not covered /// #### PLEASE BE WARNED
38 not covered //
39 not covered // Using the complete `require` is only recommended if `token` and
40 not covered // `token_secret` already exists.
41 not covered //
42 not covered // Otherwise the API module will throw an Error since it does need those
43 not covered // two variables to do any of the calls./
44 not covered //
45 not covered // What's available on the initialized object?
46 not covered // -------------------------------------------
47 not covered //
48 not covered // Initializes two objects: `api` and `oauth`. You can initialize them
49 not covered // separated too (this is my preferred method).
50 not covered //
51 not covered // Parameters to initialize any of the exported Objects
52 not covered // ----------------------------------------------------
53 not covered //
54 not covered // All the exported functions expect an Object with the params:
55 not covered //
56 not covered // + `consumer_key` - (Required) consumer key given by Twitter
57 not covered // + `consumer_secret` - (Required) consumer secret given by Twitter
58 not covered // + `token` - (Optional) access_token key given by Twitter
59 not covered // + `token_secret` - (Required if `access_token_key` was given)
60 not covered // given by Twitter
61 not covered // + `callback` - (Optional) If your app is a desktop app write `oob`
62 not covered // (Out-Of-Band); if not then you should write your callback URL here
63 not covered // (which will rewrite the one configured on Twitter's developer dashboard.
64 not covered //
65 not covered
66 not covered //
67 not covered // Base URIs for Twitter API (These should be overwritten if to be used
68 not covered // with a compatible API)
69 not covered //
701100%var uri = { base: 'https://api.twitter.com/1.1', search: 'https://api.twitter.com/1.1/search' };
71 not covered
72 not covered //
73 not covered // Usage
74 not covered // -----
75 not covered //
76 not covered // ```
77 not covered // var TwitterLib = require('twitter-rest-lite'),
78 not covered // keys = {consumer_key: '...', consumer_secret: '...', token: '...', token_secret: '...' callback: '...'},
79 not covered // twitter = new TwitterLib(keys);
80 not covered //
81 not covered // /* twitter.oauth object */
82 not covered // twitter.oauth.requestToken( /* ... */ );
83 not covered // twitter.oauth.accessToken( /* ... */ );
84 not covered // twitter.oauth.authenticate( /* ... */ );
85 not covered // twitter.oauth.authorize( /* ... */ );
86 not covered //
87 not covered // /* twitter.api object */
88 not covered // twitter.api.get( /* ... */ );
89 not covered // twitter.api.post( /* ... */ );
90 not covered // ```
91 not covered //
92 not covered // #### PLEASE BE WARNED
93 not covered //
94 not covered // Using the complete `require` is only recommended if `token` and
95 not covered // `token_secret` already exists.
96 not covered //
97 not covered // Otherwise the API module will throw an Error since it does need those
98 not covered // two variables to do any of the calls.
99 not covered //
100 not covered // #### Code
1011100%module.exports = function(opts) {
1020 not covered return {
103 not covered };
104 not covered
105 not covered // OAuth Quick Usage
106 not covered // -----------------
107 not covered //
108 not covered // ```
109 not covered // var TwitterLib = require('twitter-rest-lite'),
110 not covered // toauth = new TwitterLib.OAuth({
111 not covered // consumer_key: 'blah',
112 not covered // consumer_secret: 'blah',
113 not covered // callback: 'randomurl'
114 not covered // });
115 not covered //
116 not covered // toauth.requestToken(function (err, response) {
117 not covered // if (err)
118 not covered // throw err;
119 not covered //
120 not covered // console.log(response);
121 not covered // });
122 not covered // ```
123 not covered //
124 not covered // More on the [OAuth module](oauth.html) documentation.
125 not covered //
126 not covered // #### Code
1271100%module.exports.OAuth = module.exports.oauth = function(opts) {
1284100% return new OAuth(uri, opts);
129 not covered };
130 not covered
131 not covered // API Quick Usage
132 not covered // ---------------
133 not covered //
134 not covered // ```
135 not covered // var TwitterLib = require('twitter-rest-lite'),
136 not covered // tapi = new TwitterLib.API({
137 not covered // consumer_key: 'blah',
138 not covered // consumer_secret: 'blah',
139 not covered // token: 'blah',
140 not covered // token_secret: 'blah',
141 not covered // callback: 'randomurl'
142 not covered // });
143 not covered //
144 not covered // tapi.get('/statuses/user_timeline.json', {
145 not covered // screen_name: 'twitter'
146 not covered // }, function (err, response) {
147 not covered // if (err)
148 not covered // throw err;
149 not covered //
150 not covered // console.log(response);
151 not covered // });
152 not covered // ```
153 not covered //
154 not covered // More on the [API module](api.html) documentation.
155 not covered //
156 not covered // #### Code
1571100%module.exports.API = module.exports.api = function(opts) {
1584100% return new API(uri, opts);
159 not covered };
160 not covered
1611100%module.exports.helper = require('./lib/helper');

lib/api.js

70% block coverage
34 SLOC
LineHitsStatementsSourceAction
11100%'use strict';
2 not covered
3 not covered //
4 not covered // Module: API
5 not covered // ===========
6 not covered //
7 not covered // Abstraction for the basic `GET`/`POST` operations of Twitter's REST API.
8 not covered //
9 not covered // Methods
10 not covered // -------
11 not covered //
12 not covered // + [Constructor/Initialize](#constructor)
13 not covered // + [GET](#get)
14 not covered // + [POST](#post)
15 not covered //
16 not covered // Usage
17 not covered // -----
18 not covered //
19 not covered // ```
20 not covered // var TwitterLib = require('twitter-rest-lite'),
21 not covered // api = new TwitterLib.API(var_with_keys);
22 not covered //
23 not covered // api.get(url, params, callback);
24 not covered //
25 not covered // api.post(url, data, callback);
26 not covered // ```
27 not covered //
28 not covered // <a name='constructor'></a>
29 not covered // Constructor
30 not covered // -----------
31 not covered //
32 not covered // #### Parameters
33 not covered //
34 not covered // + `uri` - base URI's to use (this should be provided by the
35 not covered // library itself)
36 not covered // + `opts` - `Object` with user-provided params
37 not covered // - `consumer_key` - required
38 not covered // - `consumer_secret` - required
39 not covered // - `token` - required
40 not covered // - `token_secret` - required
41 not covered //
42 not covered // #### Returns
43 not covered //
44 not covered // An `Object` with methods `get` and `post`.
45 not covered //
46 not covered // #### Code
47 not covered function API(uri, opts) {
484100% this.uri = uri;
49 not covered
50 not covered /* checking the required arguments */
514100% [ 'consumer_key', 'consumer_secret', 'token', 'token_secret' ].forEach(function (item) {
5214100% if (opts[item] == null)
531100% throw new Error('There\'s a required argument missing: ' + item);
54 not covered });
55 not covered
563100% this.opts = opts;
57 not covered }
58 not covered
59 not covered //
60 not covered // <a name='get'></a>
61 not covered // Public: Abstract GET request to the API
62 not covered // ---------------------------------------
63 not covered //
64 not covered // #### Parameters
65 not covered //
66 not covered // + `url` - String
67 not covered // + `params` - [Optional] Object with params to be passed
68 not covered // + `callback` - Callback Function
69 not covered //
70 not covered // #### Returns
71 not covered //
72 not covered // A `Callback` with two parameters. First is an `Error Object` and second
73 not covered // the body of the response in an `Object`.
74 not covered //
75 not covered // #### Example
76 not covered //
77 not covered // ```js
78 not covered // api.get('/statuses/user_timeline.json', {
79 not covered // screen_name: 'random',
80 not covered // count: 1
81 not covered // }, function (err, response) {
82 not covered // if (err)
83 not covered // throw err;
84 not covered //
85 not covered // console.log(response);
86 not covered // });
87 not covered // ```
88 not covered // #### Code
891100%API.prototype.get = function(url, params, callback) {
902100% var self = this;
912100% var request = require('request');
922100% var helper = require('./helper');
93 not covered
942100% helper.check(url, 'string', '', 'Missing URL parameter', callback);
95 not covered
961100% url = self.uri.base + url;
97 not covered
98160% if ((params != null) && not covered typeof params === 'object') {
990 not covered var qs = require('querystring');
1000 not covered url += '?' + qs.stringify(params);
101 not covered }
1021100% request({ method: 'GET', uri: url, oauth: self.opts, json: true, }, function (err, response, body) {
1030 not covered return callback(err, body);
104 not covered });
105 not covered };
106 not covered
107 not covered //
108 not covered // <a name='post'></a>
109 not covered // Public: abstract POST request to the API
110 not covered // ----------------------------------------
111 not covered //
112 not covered // #### Parameters
113 not covered //
114 not covered // + `url` - String
115 not covered // + `data` - [Required] Object with data
116 not covered // + `callback` - Callback Function
117 not covered //
118 not covered // #### Returns
119 not covered //
120 not covered // A `Callback` with two parameters: `Error Object` and `Object` with
121 not covered // body response from Twitter's API server.
122 not covered //
123 not covered // #### Example
124 not covered //
125 not covered // ```js
126 not covered // api.post('/statuses/update.json', {
127 not covered // status: "This is an update to twitter!"
128 not covered // }, function (err, response) {
129 not covered // if (err)
130 not covered // throw err;
131 not covered //
132 not covered // console.log(response);
133 not covered // });
134 not covered // ```
135 not covered //
136 not covered // #### Code
1371100%API.prototype.post = function(url, data, callback) {
1384100% var self = this;
1394100% var request = require('request');
1404100% var helper = require('./helper');
141 not covered
1423100% if (helper.check(url, 'string', '', 'Missing URL parameter', callback))
1431100% return;
144 not covered
1451100% if (helper.check(data, 'object', {}, 'Missing data parameter', callback))
1461100% return;
147 not covered
1480 not covered request({
1490 not covered uri: self.uri.base + url,
150 not covered oauth: self.opts,
151 not covered form: data
1520 not covered return callback(err, body);
153 not covered });
154 not covered };
155 not covered
156 not covered
1571100%module.exports = API;

lib/oauth.js

31% block coverage
57 SLOC
LineHitsStatementsSourceAction
11100%'use strict';
21100%var helper = require('./helper');
3 not covered
4 not covered //
5 not covered // Module: OAuth
6 not covered // =============
7 not covered //
8 not covered // Abstraction for the authentication methods of Twitter's API.
9 not covered //
10 not covered // **Notice**: At the moment this is depending on `request`'s ability to create OAuth
11 not covered // signatures, but in the future this should have it's own OAuth signing with
12 not covered // OAuth2 support.
13 not covered //
14 not covered // Methods
15 not covered // -------
16 not covered //
17 not covered // + [Constructor](#constructor)
18 not covered // + [Request Token](#requestToken)
19 not covered // + [Access Token](#accessToken)
20 not covered // + [Authenticate](#authenticate)
21 not covered // + [Authorize](#authorize)
22 not covered //
23 not covered // Usage
24 not covered // -----
25 not covered //
26 not covered // ```
27 not covered // var TwitterLib = require('twitter-rest-lite'),
28 not covered // oauth = new TwitterLib.OAuth(var_with_keys);
29 not covered //
30 not covered // api.requestToken(callback);
31 not covered //
32 not covered // api.accessToken(token, verifier, callback);
33 not covered //
34 not covered // api.authenticate(callback);
35 not covered //
36 not covered // api.authorize(callback);
37 not covered // ```
38 not covered //
39 not covered // <a name='constructor'></a>
40 not covered // Constructor
41 not covered // -----------
42 not covered //
43 not covered // #### Parameters
44 not covered //
45 not covered // + `uri` - Object with the basic API URI's
46 not covered // + `opts` - Object with the following params
47 not covered // - `consumer_key` - [Required] consumer_key from Twitter
48 not covered // - `consumer_secret` - [Required] consumer_secret from Twitter
49 not covered // - `callback` - [Optional]
50 not covered //
51 not covered // #### Returns
52 not covered //
53 not covered // An `Object` with methods `requestToken`, `accessToken`, `authenticate`
54 not covered // and `authorize`.
55 not covered //
56 not covered // #### Code
57 not covered function OAuth(uri, opts) {
584100% this.uri = uri;
59 not covered
60 not covered /* Extending `uri` with oauth URI's */
614100% this.uri.requestToken = 'https://api.twitter.com/oauth/request_token';
624100% this.uri.accessToken = 'https://api.twitter.com/oauth/access_token';
634100% this.uri.authenticate = 'https://api.twitter.com/oauth/authenticate';
644100% this.uri.authorize = 'https://api.twitter.com/oauth/authorize';
65 not covered
66 not covered /* checking the required arguments */
674100% ['consumer_key', 'consumer_secret'].forEach(function (item) {
688100% if (opts[item] == null)
691100% throw new Error('There\'s a required argument missing: ' + item);
70 not covered });
71 not covered
723100% this.opts = opts;
73 not covered }
74 not covered
75 not covered //
76 not covered // <a name='requestToken'></a>
77 not covered // Public: get a request token
78 not covered // ---------------------------
79 not covered //
80 not covered // #### Parameters
81 not covered //
82 not covered // + `callback` - `Callback` Function
83 not covered //
84 not covered // #### Returns
85 not covered //
86 not covered // Returns a callback with an `Error Object` as first parameter if there was
87 not covered // (otherwise just `null`) and an `Object` with the response with the model:
88 not covered //
89 not covered // ```json
90 not covered // {
91 not covered // token: String,
92 not covered // token_secret: String,
93 not covered // oauth_callback_confirmed: Boolean
94 not covered // }
95 not covered // ```
96 not covered //
97 not covered // #### Example
98 not covered //
99 not covered // ```js
100 not covered // oauth.requestToken(function (err, response) {
101 not covered // if (err)
102 not covered // throw err;
103 not covered //
104 not covered // console.log(response);
105 not covered // });
106 not covered // ```
107 not covered //
108 not covered // `response.token` is used by [`oauth.authenticate`](#authenticate) and
109 not covered // [`oauth.authorize`](#authorize).
110 not covered //
111 not covered // #### Code
1121100%OAuth.prototype.requestToken = function(callback) {
1130 not covered var self = this;
1140 not covered var request = require('request');
1150 not covered var oauth = {};
116 not covered
1170 not covered ['consumer_key', 'consumer_secret', 'callback'].forEach(function (e) {
1180 not covered if (self.opts[e] != null)
1190 not covered oauth[e] = self.opts[e];
120 not covered });
121 not covered
1220 not covered request({
1230 not covered if (err)
1240 not covered return callback(err);
125 not covered
1260 not covered if (response.statusCode !== 200) {
1270 not covered return callback(new Error(
128 not covered 'Twitter:OAuth.requestToken received an status differente than 200: \n' +
1290 not covered 'Status Code: ' + response.statusCode + '\n' +
1300 not covered 'Body: \n' + body
131 not covered ));
132 not covered }
133 not covered
1340 not covered var qs = require('querystring');
135 not covered
1360 not covered return callback(null, qs.parse(body));
137 not covered
138 not covered };
139 not covered
140 not covered //
141 not covered // <a name='accessToken'></a>
142 not covered // Public: get an access token
143 not covered // ---------------------------
144 not covered //
145 not covered // #### Parameters
146 not covered //
147 not covered // + `token` - `String` with `oauth_token`
148 not covered // + `verifier` - `String` with `oauth_verifier`
149 not covered // + `callback` - `Callback` Function
150 not covered //
151 not covered // #### Returns
152 not covered //
153 not covered // A `Callback` with an `Error` object as first parameter if there was
154 not covered // (otherwise just `null`) and an `Object` with the response with the model:
155 not covered //
156 not covered // ```js
157 not covered // {
158 not covered // oauth_token: String,
159 not covered // oauth_token_secret: String,
160 not covered // user_id: String,
161 not covered // screen_name: String
162 not covered // }
163 not covered // ```
164 not covered //
165 not covered // #### Example
166 not covered //
167 not covered // After running either `oauth.authenticate` or `oauth.authorize` and
168 not covered // making the proper request to twitter's servers you will end up with
169 not covered // a `token` and a `verifier`. Suppose they are stored each in a variable
170 not covered // of the same name, then:
171 not covered //
172 not covered // ```
173 not covered // oauth.accessToken(token, verifier, function (err, response) {
174 not covered // if (err)
175 not covered // throw (err);
176 not covered //
177 not covered // console.log(response);
178 not covered // });
179 not covered // ```
180 not covered //
181 not covered // With the data from that response you can initialize the API module and
182 not covered // start `GET`'ing and `POST`'ing with *user context* as Twitter calls it.
183 not covered //
184 not covered // #### Code
1851100%OAuth.prototype.accessToken = function(token, verifier, callback) {
1860 not covered var self = this;
1870 not covered var request = require('request');
188 not covered
1890 not covered [token, verifier].forEach(function (item) {
1900 not covered if (item == null) {
1910 not covered return callback(new Error(
192 not covered 'Twitter:OAuth.accessToken requires all the arguments to work.'
193 not covered }
194 not covered
1950 not covered var oauth = {
196 not covered
1970 not covered request({
1980 not covered if (err)
1990 not covered return callback(err);
200 not covered
2010 not covered var qs = require('querystring');
202 not covered
2030 not covered return callback(null, qs.parse(body));
204 not covered });
205 not covered
206 not covered
207 not covered //
208 not covered // <a name='authenticate'></a>
209 not covered // Public: get authenticate URL
210 not covered // ----------------------------
211 not covered //
212 not covered // #### Parameters
213 not covered //
214 not covered // + `token` - [Required] `String` with `oauth_token` from
215 not covered // `OAuth.requestToken`.
216 not covered // + `cb` - `Callback` Function
217 not covered //
218 not covered // #### Returns
219 not covered //
220 not covered // A `Callback` with an `Error` object as the first parameter and a `String`
221 not covered // with the URL to which redirect users as second parameter.
222 not covered //
223 not covered // #### Example
224 not covered //
225 not covered // ```js
226 not covered // oauth.authenticate(token, function (err, response) {
227 not covered // if (err)
228 not covered // throw err;
229 not covered //
230 not covered // console.log(response);
231 not covered // /* https://api.twitter.com/oauth/authenticate?oauth_token= + token provided */
232 not covered // });
233 not covered // ```
234 not covered //
235 not covered // #### Code
2361100%OAuth.prototype.authenticate = function(token, cb) {
2371100% helper.authyThing.call(this, 'authenticate', token, cb);
238 not covered };
239 not covered
240 not covered //
241 not covered // <a name='authorize'></a>
242 not covered // Public: get authorize URL
243 not covered // -------------------------
244 not covered //
245 not covered // #### Parameters
246 not covered //
247 not covered // + `token` - [Required] `String` with `oauth_token` from
248 not covered // `OAuth.requestToken`.
249 not covered // + `cb` - `Callback` Function
250 not covered //
251 not covered // #### Returns
252 not covered //
253 not covered // A `Callback` with an `Error` object as the first parameter and a `String`
254 not covered // with the URL to which redirect users as second parameter.
255 not covered //
256 not covered // #### Example
257 not covered //
258 not covered // ```js
259 not covered // oauth.authorize(token, function (err, response) {
260 not covered // if (err)
261 not covered // throw err;
262 not covered //
263 not covered // console.log(response);
264 not covered // /* https://api.twitter.com/oauth/authorize?oauth_token= + token provided */
265 not covered // });
266 not covered // ```
267 not covered // #### Code
2681100%OAuth.prototype.authorize = function(token, cb) {
2691100% helper.authyThing.call(this, 'authorize', token, cb);
270 not covered };
271 not covered
2721100%module.exports = OAuth;

lib/helper.js

100% line coverage
100% statement coverage
100% block coverage
21 SLOC
LineHitsStatementsSourceAction
11100%'use strict';
2 not covered
31100%var callError = module.exports.callError = function callError (errMsg, cb) {
411100% if (cb != null)
57100% return cb(new Error(errMsg));
6 not covered else
74100% throw new Error(errMsg);
8 not covered };
9 not covered
101100%var check = module.exports.check = function check (obj, type, empty, errMsg, cb) {
1117100% if (errMsg == null || typeof errMsg !== 'string' || errMsg === '') {
121100% callError('errMsg needs to be defined', cb);
131100% return true;
14 not covered }
1516100% if (obj == null || typeof(obj) !== type) {
166100% callError(errMsg, cb);
174100% return true;
18 not covered }
1910100% if (empty != null && obj === empty) {
203100% callError(errMsg, cb);
211100% return true;
22 not covered }
23 not covered
241100%module.exports.authyThing = function authyThing (what, token, cb) {
253100% if (!check(token, 'string', '', 'Requires a token', cb))
263100% return cb(null, this.uri[what] + '?oauth_token=' + token);
27 not covered };