summaryrefslogtreecommitdiff
path: root/examples/lib/okdumpfm/index.js
blob: 4dc546166aba905a0f5210c56818bd8251fc3edf (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
var request = require('request')

/**
 * Example service which queries the Dump search.
 */
function OKDumpfm (options) {
  if (!(this instanceof OKDumpfm)) return new OKDumpfm(options)
  options = options || {}
  if (!options.express)
    throw new Error('Express not provided to OKDumpfm');

  var express = options.express
  var router = express.Router()

  router.get('*', function (req, res) {
    var query = req.query.q
    request('http://dump.fm/cmd/search/' + query, function (err, response, body) {
      if (err || response.statusCode !== 200) {
        res.status(response.statusCode)
        res.send(err)
      } else {
        res.set('Content-Type', 'application/json; charset=utf-8')
        res.send(body)
      }
    })
  })

  router.post('*', function (req, res) {
    throw new Error('OKDumpfm POST requests not implemented')
  })

  this._router = router
}

OKDumpfm.prototype.middleware = function () {
  return this._router
}

module.exports = OKDumpfm