aboutsummaryrefslogtreecommitdiffstats
path: root/routes/calculator.coffee
blob: aa4fb063bf783ee1bbb54820b9e5ea9a490b0bc4 (plain) (blame)
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
exports.new = (req, res) ->
  res.render('calculator/setup',  js: (-> global.js), css: (-> global.css))

exports.show = (req, res) ->
  res.render('calculator/show', 
    js: (-> global.js), 
    css: (-> global.css),
  )

exports.ticker = (req, res) ->
  options = 
    host: 'bitcoincharts.com', 
    path: "/t/depthcalc.json?symbol=#{req.query.symbol}&type=#{req.query.type}&amount=#{req.query.amount}&currency=true"

  require('http').get(options, (r) ->
    r.setEncoding('utf-8')
    r.on('data', (chunk) ->
      try
        exchange = req.query.amount / JSON.parse(chunk).out
        exchange = (Math.ceil(exchange * 100) / 100).toString()
      catch e
        exchange = ""

      res.writeHead(200, 
        'Content-Length': exchange.length,
        'Content-Type': 'text/plain')
      res.write(exchange)
      res.end()
    )
  )