diff options
| author | Adam Soltys <[email protected]> | 2014-06-14 23:07:26 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2014-06-14 23:07:26 -0700 |
| commit | eeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9 (patch) | |
| tree | 019a98ab99002e750328e8e2546ce4b2b8a0d421 | |
| parent | 26a8814cb9a267e3d44fb9d09f02da4c70af8600 (diff) | |
| download | coinos-server-eeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9.tar.gz coinos-server-eeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9.zip | |
Get rates from bitcoinaverage.com
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | app.coffee | 1 | ||||
| -rwxr-xr-x | fetch_rates.sh | 2 | ||||
| -rw-r--r-- | public/js/calculator.coffee | 12 | ||||
| -rw-r--r-- | public/js/setup.coffee | 56 | ||||
| -rw-r--r-- | routes/calculator.coffee | 28 | ||||
| -rw-r--r-- | views/calculator/setup.html | 6 |
7 files changed, 64 insertions, 43 deletions
@@ -5,4 +5,4 @@ builtAssets database.json sequelize.coffee npm-debug.log -rates.json +public/js/rates.json @@ -58,6 +58,7 @@ app.get('/:user', users.show) app.use((err, req, res, next) -> res.status(500) + res.send('An error occurred'); console.log(err) res.end() ) diff --git a/fetch_rates.sh b/fetch_rates.sh index 83d60a3e..9c463c0b 100755 --- a/fetch_rates.sh +++ b/fetch_rates.sh @@ -1,2 +1,2 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -curl -k https://api.bitcoinaverage.com/exchanges/all > $DIR/rates.tmp && mv $DIR/rates.tmp $DIR/rates.json +curl -k https://api.bitcoinaverage.com/exchanges/all > $DIR/rates.tmp && mv $DIR/rates.tmp $DIR/public/js/rates.json diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index 72af1987..b0c69300 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -20,6 +20,7 @@ $(-> g.title = get('title') g.address = get('address') g.symbol = get('symbol') + g.currency = get('currency') g.commission = parseFloat(get('commission')) g.logo = get('logo') g.errors = [] @@ -33,6 +34,7 @@ $(-> if data? g.title = data.title g.address = data.address + g.currency = data.currency g.symbol = data.symbol g.commission = data.commission g.logo = data.logo @@ -64,7 +66,7 @@ activateTip = (p) -> setup = -> g.address or= '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr' g.commission or= 0 - g.symbol or= 'mtgoxUSD' + g.symbol or= 'quadrigacx' if g.title $('#title').html(g.title).show() @@ -83,10 +85,8 @@ setup = -> else fail(ADDRESS_FAIL) - symbol = g.symbol - - $('#symbol').html(symbol) - $('#currency').html(g.symbol.slice(-3)) + $('#symbol').html(g.currency) + $('#currency').html(g.currency) $('#received').hide() setupSocket() @@ -94,7 +94,7 @@ setup = -> fetchExchangeRate = -> $.ajax( - url: "ticker?symbol=#{g.symbol}&type=bid&amount=1000", + url: "ticker?currency=#{g.currency}&symbol=#{g.symbol}&type=bid", success: (exchange) -> if exchange? clear(EXCHANGE_FAIL) diff --git a/public/js/setup.coffee b/public/js/setup.coffee index a204dc55..c9cb9cb3 100644 --- a/public/js/setup.coffee +++ b/public/js/setup.coffee @@ -5,30 +5,44 @@ #= require check_address.js $(-> - symbols = ['mtgoxUSD', 'btceUSD', 'bitstampUSD', 'virwoxSLL', 'btcdeEUR', 'mtgoxEUR', 'btc24EUR', 'mtgoxAUD', 'cryptoxAUD', 'mtgoxGBP', 'btcnCNY', 'intrsngEUR', 'virtexCAD', 'mtgoxPLN', 'cbxUSD', 'bitcurexPLN', 'bitmarketEUR', 'bitfloorUSD', 'mrcdBRL', 'bcEUR', 'lybitCAD'] - symbols = symbols.sort() + $.getJSON("/js/rates.json", (data) -> + currencies = Object.keys(data) + currencies = currencies.sort() + currencies.pop() - user = $('#username').val() + + $.each(currencies, (i, v) -> + $('#currency').append("<option value='#{v}'>#{v}</option>") + ) + $("#currency option[value='CAD']").attr('selected', 'selected') - $.each(symbols, (i, v) -> - $('#symbol').append("<option value='#{v}'>#{v}</option>") - ) - $("#symbol option[value='mtgoxUSD']").attr('selected', 'selected') - $('#address').change(-> - if check_address($(this).val()) - $(this).css('color', 'black') - else - $(this).css('color', 'red') - ) + user = $('#username').val() - if user - $('#setup').attr('action', "/#{user}/update").attr('method', 'post') - $.getJSON("/#{user}.json", (data) -> - $('#title').val(data.title) - $('#logo').val(data.logo) - $('#address').val(data.address) - $("#symbol option[value='#{data.symbol}']").attr('selected', 'selected') - $('#commission').val(data.commission) + $('#currency').change(-> + $('#symbol option').remove() + symbols = Object.keys(data[$(this).val()]) + $.each(symbols, (i, v) -> + $('#symbol').append("<option value='#{v}'>#{v}</option>") + ) + $("#symbol option[value='quadrigacx']").attr('selected', 'selected') + ).change() + + $('#address').change(-> + if check_address($(this).val()) + $(this).css('color', 'black') + else + $(this).css('color', 'red') ) + + if user + $('#setup').attr('action', "/#{user}/update").attr('method', 'post') + $.getJSON("/#{user}.json", (data) -> + $('#title').val(data.title) + $('#logo').val(data.logo) + $('#address').val(data.address) + $("#symbol option[value='#{data.symbol}']").attr('selected', 'selected') + $('#commission').val(data.commission) + ) + ) ) diff --git a/routes/calculator.coffee b/routes/calculator.coffee index 8cc6aa49..dc9a6d43 100644 --- a/routes/calculator.coffee +++ b/routes/calculator.coffee @@ -8,19 +8,21 @@ exports.show = (req, res) -> ) exports.ticker = (req, res) -> - options = - host: 'api.bitcoinaverage.com', - path: "/exchanges/CAD" + fs = require('fs') + fs.readFile("./public/js/rates.json", (err, data) -> + req.query.currency ||= 'CAD' + req.query.symbol ||= 'quadrigacx' + req.query.type ||= 'bid' - require('https').get(options, (r) -> - r.setEncoding('utf-8') - r.on('data', (chunk) -> - exchange = JSON.parse(chunk).cavirtex.rates[req.query.type].toString() + try + exchange = JSON.parse(data)[req.query.currency][req.query.symbol]['rates'][req.query.type].toString() + catch e + exchange = "0" - res.writeHead(200, - 'Content-Length': exchange.length, - 'Content-Type': 'text/plain') - res.write(exchange) - res.end() - ) + res.writeHead(200, + 'Content-Length': exchange.length, + 'Content-Type': 'text/plain') + res.write(exchange) + res.end() ) + diff --git a/views/calculator/setup.html b/views/calculator/setup.html index 6f932fb6..e77c5cd1 100644 --- a/views/calculator/setup.html +++ b/views/calculator/setup.html @@ -24,7 +24,11 @@ <label for="address">Bitcoin Address:</label> <input type="text" id="address" name="address" /> - <label for="symbol">Exchange <small>(15 minute delay)</small>:</label> + <label for="currency">Currency:</label> + <select id="currency" name="currency"> + </select> + + <label for="symbol">Exchange:</label> <select id="symbol" name="symbol"> </select> |
