]> git.r.bdr.sh - rbdr/dasein/blame - lib/twitter_helper.js
🔧 Update version in package.json
[rbdr/dasein] / lib / twitter_helper.js
CommitLineData
287fa13b
RBR
1'use strict';
2
3const Co = require('co');
4const OAuth = require('oauth');
5const Pify = require('pify');
6
7const internals = {};
8
9internals.kRequestTokenUrl = 'https://api.twitter.com/oauth/request_token';
10internals.kAccessTokenUrl = 'https://api.twitter.com/oauth/access_token';
11internals.kVerifyCredentialsUrl = 'https://api.twitter.com/1.1/account/verify_credentials.json';
12internals.kOauthVersion = '1.0A';
13internals.kOauthSignatureMethod = 'HMAC-SHA1';
14
a6ccda0f
RBR
15/**
16 * Helper to communicate with the twitter API
17 *
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}
23 *
24 */
287fa13b
RBR
25module.exports = internals.TwitterHelper = class TwitterHelper {
26
27 constructor(config) {
28
29 this._oAuth = new OAuth.OAuth(
30 internals.kRequestTokenUrl,
31 internals.kAccessTokenUrl,
32 config.consumerKey,
33 config.consumerSecret,
34 internals.kOauthVersion,
35 null,
36 internals.kOauthSignatureMethod
37 );
38 }
39
a6ccda0f
RBR
40 /**
41 * Calls the API to get a request token.
42 *
43 * @function getRequestToken
44 * @memberof TwitterHelper
45 * @instance
46 * @return {Promise<TwitterHelper.tRequestToken>} the request token response
47 */
287fa13b
RBR
48 getRequestToken() {
49
50 const self = this;
51
52 return Co(function * () {
53
54 const getOAuthRequestToken = Pify(self._oAuth.getOAuthRequestToken.bind(self._oAuth), { multiArgs: true });
55 const [oAuthToken, oAuthTokenSecret] = yield getOAuthRequestToken();
56
a6ccda0f
RBR
57 /**
58 * The request token and secret pair from the twitter API
59 *
60 * @memberof TwitterHelper
61 * @typedef {object} tRequestToken
62 * @property {string} oAuthToken The oAuth request token
63 * @property {string} oAuthTokenSecret The oAuth request token
64 * secret
65 */
287fa13b
RBR
66 return {
67 oAuthToken,
68 oAuthTokenSecret
69 };
70 });
71 }
72
a6ccda0f
RBR
73 /**
74 * Calls the API to get an access token
75 *
76 * @function getAccessToken
77 * @memberof TwitterHelper
78 * @instance
79 * @param {string} oAuthToken An oAuth request token
80 * @param {string} oAuthVerifier An oAuth verifier sent from the
81 * twitter callback
82 * @return {Promise<TwitterHelper.tAccessToken>} the acess token response
83 */
287fa13b
RBR
84 getAccessToken(oAuthToken, oAuthVerifier) {
85
86 const self = this;
87
88 return Co(function * () {
89
90 const getOAuthAccessToken = Pify(self._oAuth.getOAuthAccessToken.bind(self._oAuth), { multiArgs: true });
91 const [oAuthAccessToken, oAuthAccessTokenSecret] = yield getOAuthAccessToken(oAuthToken,
92 '',
93 oAuthVerifier);
94
a6ccda0f
RBR
95 /**
96 * The access token and secret pair from the twitter API
97 *
98 * @memberof TwitterHelper
99 * @typedef {object} tAccessToken
100 * @property {string} oAuthAccessToken The oAuth access token
101 * @property {string} oAuthAccessTokenSecret The oAuth access token
102 * secret
103 */
287fa13b
RBR
104 return {
105 oAuthAccessToken,
106 oAuthAccessTokenSecret
107 };
108 });
109 }
110
a6ccda0f
RBR
111 /**
112 * Gets a user object from twitter
113 *
114 * @function getUser
115 * @memberof TwitterHelper
116 * @instance
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
120 * twitter
121 */
287fa13b
RBR
122 getUser(oAuthAccessToken, oAuthAccessTokenSecret) {
123
124 const self = this;
125
126 return Co(function * () {
127
128 const get = Pify(self._oAuth.get.bind(self._oAuth), { multiArgs: true });
129 const [userResponse] = yield get(internals.kVerifyCredentialsUrl,
130 oAuthAccessToken,
131 oAuthAccessTokenSecret);
132
a6ccda0f
RBR
133 /**
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}
138 */
287fa13b
RBR
139 return JSON.parse(userResponse);
140 });
141 }
142};