From 16062edf148ea44bc8a5f40732c0dc1d363d588c Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 13 Nov 2012 19:27:09 -0800 Subject: Allow market selection --- app.coffee | 32 ++++++++++++++++++++++---------- public/js/calculator.coffee | 21 ++++++++++++--------- public/js/index.coffee | 9 +++++++++ views/calculator.html | 6 +++--- views/index.html | 5 +++++ 5 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 public/js/index.coffee diff --git a/app.coffee b/app.coffee index cdc8c313..44c95739 100644 --- a/app.coffee +++ b/app.coffee @@ -1,3 +1,4 @@ + express = require('express') http = require('http') path = require('path') @@ -54,13 +55,10 @@ app.get('/:user/transactions', (req, res) -> app.get('/ticker', (req, res) -> - symbol = req.params.symbol - symbol or= 'virtexCAD' - options = host: 'bitcoincharts.com', - path: '/t/depthcalc.json?symbol=' + symbol + '&type=bid&amount=1000¤cy=true' - + path: '/t/depthcalc.json?symbol=' + req.query.symbol + '&type=bid&amount=1000¤cy=true' + require('http').get(options, (r) -> r.setEncoding('utf-8') r.on('data', (chunk) -> @@ -70,11 +68,17 @@ app.get('/ticker', (req, res) -> ) app.post('/users', (req, res) -> - db = require("redis").createClient() - db.hmset(req.body.login, req.body, -> - res.writeHead(302, 'Location': '/' + req.body.login) - res.end() - ) + if req.body.login + db = require("redis").createClient() + db.hmset(req.body.login, req.body, -> + res.redirect(req.body.login) + ) + else + params = [] + for k,v of req.body + params.push(encodeURIComponent(k), '=', encodeURIComponent(v), '&') + params.pop() if (params.length) + res.redirect('calculator?' + params.join('')) ) app.post('/:user/transactions', (req, res) -> @@ -90,6 +94,13 @@ app.post('/:user/transactions', (req, res) -> ) ) +app.get('/calculator', (req, res) -> + res.render('calculator', + js: (-> global.js), + css: (-> global.css) + ) +) + app.get('/:user', (req, res) -> res.render('calculator', user: req.params.user, @@ -102,4 +113,5 @@ app.use((err, req, res, next) -> console.log(err) ) + app.listen(3000) diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index 01523d9a..3d1d61b4 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -8,8 +8,9 @@ g = exports ? this $(-> g.websocket = null g.user = $('#user').val() - g.title = get('title') + g.name = get('name') g.address = get('address') + g.symbol = get('symbol') g.commission = get('commission') g.logo = get('logo') g.exchange = 0 @@ -20,7 +21,7 @@ $(-> if user? and user $.getJSON(user + '.json', (data) -> - g.title = data.name + g.name = data.name g.address = data.address g.commission = data.commission g.logo = data.logo @@ -53,7 +54,7 @@ exchangeFail = -> $('#calculator').hide() fetchExchangeRate = -> - $.getJSON('ticker') + $.getJSON('ticker?symbol=' + g.symbol) .success((data) -> unless data? exchangeFail() @@ -71,18 +72,20 @@ fetchExchangeRate = -> setTimeout(fetchExchangeRate, 900000) setupPage = -> - unless g.address - g.address = '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr' - - unless g.commission - g.commission = 3 + g.address or= '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr' + g.commission or= 3 + g.symbol or= 'virtexCAD' if g.logo $('#logo').attr('src', g.logo).show() + else if not g.name + $('#logo').attr('src', 'img/bitcoin.png').show() else - $('#title').html(title).show() + $('#name').html(g.name).show() $('#address').html(g.address) + $('#symbol').html(g.symbol + " - " + commission.toFixed(0) + "%") + $('#currency').html(g.symbol.slice(-3)) $('#received').hide() fetchExchangeRate() diff --git a/public/js/index.coffee b/public/js/index.coffee new file mode 100644 index 00000000..d832c4ed --- /dev/null +++ b/public/js/index.coffee @@ -0,0 +1,9 @@ +#= require jquery-1.8.2.min.js + +$(-> + symbols = ['mtgoxUSD', 'btceUSD', 'bitstampUSD', 'virwoxSLL', 'btcdeEUR', 'mtgoxEUR', 'btc24EUR', 'mtgoxAUD', 'cryptoxAUD', 'mtgoxGBP', 'btcnCNY', 'intrsngEUR', 'virtexCAD', 'mtgoxPLN', 'cbxUSD', 'bitcurexPLN', 'bitmarketEUR', 'bitfloorUSD', 'mrcdBRL', 'bcEUR'] + + $.each(symbols, (i, v) -> + $('#symbol').append("") + ) +) diff --git a/views/calculator.html b/views/calculator.html index 87042825..8acc0276 100644 --- a/views/calculator.html +++ b/views/calculator.html @@ -13,16 +13,16 @@