]>
git.r.bdr.sh - rbdr/dasein/blob - lib/twitter_helper.js
6edf030d0b3775b17997f7a0ba1473b19d83ecd1
3 const Co
= require('co');
4 const OAuth
= require('oauth');
5 const Pify
= require('pify');
9 internals
.kRequestTokenUrl
= 'https://api.twitter.com/oauth/request_token';
10 internals
.kAccessTokenUrl
= 'https://api.twitter.com/oauth/access_token';
11 internals
.kVerifyCredentialsUrl
= 'https://api.twitter.com/1.1/account/verify_credentials.json';
12 internals
.kOauthVersion
= '1.0A';
13 internals
.kOauthSignatureMethod
= 'HMAC-SHA1';
16 * Helper to communicate with the twitter API
18 * @class TwitterHelper
19 * @param {Dasein.tTwitterConfiguration} config the configuration to
20 * initialize the twitter API
21 * @see {@link https://dev.twitter.com/web/sign-in/implementing|Implementing
22 * Sign in with Twitter}
25 module
.exports
= internals
.TwitterHelper
= class TwitterHelper
{
29 this._oAuth
= new OAuth
.OAuth(
30 internals
.kRequestTokenUrl
,
31 internals
.kAccessTokenUrl
,
33 config
.consumerSecret
,
34 internals
.kOauthVersion
,
36 internals
.kOauthSignatureMethod
41 * Calls the API to get a request token.
43 * @function getRequestToken
44 * @memberof TwitterHelper
46 * @return {Promise<TwitterHelper.tRequestToken>} the request token response
52 return Co(function * () {
54 const getOAuthRequestToken
= Pify(self
._oAuth
.getOAuthRequestToken
.bind(self
._oAuth
), { multiArgs: true });
55 const [oAuthToken
, oAuthTokenSecret
] = yield getOAuthRequestToken();
58 * The request token and secret pair from the twitter API
60 * @memberof TwitterHelper
61 * @typedef {object} tRequestToken
62 * @property {string} oAuthToken The oAuth request token
63 * @property {string} oAuthTokenSecret The oAuth request token
74 * Calls the API to get an access token
76 * @function getAccessToken
77 * @memberof TwitterHelper
79 * @param {string} oAuthToken An oAuth request token
80 * @param {string} oAuthVerifier An oAuth verifier sent from the
82 * @return {Promise<TwitterHelper.tAccessToken>} the acess token response
84 getAccessToken(oAuthToken
, oAuthVerifier
) {
88 return Co(function * () {
90 const getOAuthAccessToken
= Pify(self
._oAuth
.getOAuthAccessToken
.bind(self
._oAuth
), { multiArgs: true });
91 const [oAuthAccessToken
, oAuthAccessTokenSecret
] = yield getOAuthAccessToken(oAuthToken
,
96 * The access token and secret pair from the twitter API
98 * @memberof TwitterHelper
99 * @typedef {object} tAccessToken
100 * @property {string} oAuthAccessToken The oAuth access token
101 * @property {string} oAuthAccessTokenSecret The oAuth access token
106 oAuthAccessTokenSecret
112 * Gets a user object from twitter
115 * @memberof TwitterHelper
117 * @param {string} oAuthAccessToken An oAuth access token
118 * @param {string} oAuthAccessTokenSecret An oAuth access token secret
119 * @return {Promise<external:TwitterUser>} the user object from
122 getUser(oAuthAccessToken
, oAuthAccessTokenSecret
) {
126 return Co(function * () {
128 const get = Pify(self
._oAuth
.get.bind(self
._oAuth
), { multiArgs: true });
129 const [userResponse
] = yield get(internals
.kVerifyCredentialsUrl
,
131 oAuthAccessTokenSecret
);
134 * The twitter user from the API
135 * @external TwitterUser
136 * @see {@link https://dev.twitter.com/overview/api/users|Twitter
137 * Api Overview: Users}
139 return JSON
.parse(userResponse
);