]> git.r.bdr.sh - rbdr/dasein/blame_incremental - lib/twitter_helper.js
Add Login (#2)
[rbdr/dasein] / lib / twitter_helper.js
... / ...
CommitLineData
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
15module.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};