summaryrefslogtreecommitdiff
path: root/app/node_modules/okservices/oktwitter/index.js
blob: ec4945d993a9ca692419d30f02bd400d9291207e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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