diff options
| author | Sean Fridman <mail@seanfridman.com> | 2015-12-12 16:21:35 +0100 |
|---|---|---|
| committer | Sean Fridman <mail@seanfridman.com> | 2015-12-12 16:21:35 +0100 |
| commit | eb2d4cd758eb06abfe9387a33f1e0f0898980d0d (patch) | |
| tree | c927f97a6a43b0254af5fc7f386c30af9aa9c9a7 /app/node_modules/okservices/oktwitter/index.js | |
| parent | 59d9b7e777b1b684aae6d85951c95d29450aee12 (diff) | |
Implement Twitter service
Diffstat (limited to 'app/node_modules/okservices/oktwitter/index.js')
| -rw-r--r-- | app/node_modules/okservices/oktwitter/index.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/app/node_modules/okservices/oktwitter/index.js b/app/node_modules/okservices/oktwitter/index.js new file mode 100644 index 0000000..ec4945d --- /dev/null +++ b/app/node_modules/okservices/oktwitter/index.js @@ -0,0 +1,48 @@ +var Twit = require('twit') + +/** + * Proxy to Twitter API adding auth creds + * TODO Technically can be abused by anyone right now. + * Should add some sort of same origin policy. + */ +function OKTwitter (options) { + if (!(this instanceof OKTwitter)) return new OKTwitter(options) + options = options || {} + if (!options.express) + throw new Error('Express not provided to OKTwitter'); + if (!options.credentials) + throw new Error('Twitter credentials not provided to OKTwitter'); + + var express = options.express + var router = express.Router() + var creds = options.credentials + var twitter = new Twit({ + consumer_key: creds.consumerKey, + consumer_secret: creds.consumerSecret, + access_token: creds.accessToken, + access_token_secret: creds.accessTokenSecret, + }) + + router.get('*', function (req, res) { + twitter.get(req.path.slice(1), req.query, function (err, data) { + if (err) { + res.status(err.statusCode) + res.send(err.twitterReply) + } else { + res.json(data) + } + }) + }) + + router.post('*', function (req, res) { + throw new Error('Twitter POST requests not implemented') + }) + + this._router = router +} + +OKTwitter.prototype.middleware = function () { + return this._router +} + +module.exports = OKTwitter |
