aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2014-06-14 23:07:26 -0700
committerAdam Soltys <[email protected]>2014-06-14 23:07:26 -0700
commiteeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9 (patch)
tree019a98ab99002e750328e8e2546ce4b2b8a0d421
parent26a8814cb9a267e3d44fb9d09f02da4c70af8600 (diff)
downloadcoinos-server-eeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9.tar.gz
coinos-server-eeb39fc0d0f3c47faf6a4a721ce0c203a7832dc9.zip
Get rates from bitcoinaverage.com
-rw-r--r--.gitignore2
-rw-r--r--app.coffee1
-rwxr-xr-xfetch_rates.sh2
-rw-r--r--public/js/calculator.coffee12
-rw-r--r--public/js/setup.coffee56
-rw-r--r--routes/calculator.coffee28
-rw-r--r--views/calculator/setup.html6
7 files changed, 64 insertions, 43 deletions
diff --git a/.gitignore b/.gitignore
index ba50368a..112d5e7d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,4 @@ builtAssets
database.json
sequelize.coffee
npm-debug.log
-rates.json
+public/js/rates.json
diff --git a/app.coffee b/app.coffee
index 0ad8e790..9a5e2d1f 100644
--- a/app.coffee
+++ b/app.coffee
@@ -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>