]> git.r.bdr.sh - rbdr/dasein/blob - lib/twitter_helper.js
3ab51fa7bd516521b7d2dc4489d7bdb9caaf59ea
[rbdr/dasein] / lib / twitter_helper.js
1 'use strict';
2
3 const Co = require('co');
4 const OAuth = require('oauth');
5 const Pify = require('pify');
6
7 const internals = {};
8
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';
14
15 module.exports = internals.TwitterHelper = class TwitterHelper {
16
17 constructor(config) {
18
19 this._oAuth = new OAuth.OAuth(
20 internals.kRequestTokenUrl,
21 internals.kAccessTokenUrl,
22 config.consumerKey,
23 config.consumerSecret,
24 internals.kOauthVersion,
25 null,
26 internals.kOauthSignatureMethod
27 );
28 }
29
30 getRequestToken() {
31
32 const self = this;
33
34 return Co(function * () {
35
36 const getOAuthRequestToken = Pify(self._oAuth.getOAuthRequestToken.bind(self._oAuth), { multiArgs: true });
37 const [oAuthToken, oAuthTokenSecret] = yield getOAuthRequestToken();
38
39 return {
40 oAuthToken,
41 oAuthTokenSecret
42 };
43 });
44 }
45
46 getAccessToken(oAuthToken, oAuthVerifier) {
47
48 const self = this;
49
50 return Co(function * () {
51
52 const getOAuthAccessToken = Pify(self._oAuth.getOAuthAccessToken.bind(self._oAuth), { multiArgs: true });
53 const [oAuthAccessToken, oAuthAccessTokenSecret] = yield getOAuthAccessToken(oAuthToken,
54 '',
55 oAuthVerifier);
56
57 return {
58 oAuthAccessToken,
59 oAuthAccessTokenSecret
60 };
61 });
62 }
63
64 getUser(oAuthAccessToken, oAuthAccessTokenSecret) {
65
66 const self = this;
67
68 return Co(function * () {
69
70 const get = Pify(self._oAuth.get.bind(self._oAuth), { multiArgs: true });
71 const [userResponse] = yield get(internals.kVerifyCredentialsUrl,
72 oAuthAccessToken,
73 oAuthAccessTokenSecret);
74
75 return JSON.parse(userResponse);
76 });
77 }
78 };