forked from ameba23/btc_rates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_rates.rb
73 lines (49 loc) · 1.69 KB
/
btc_rates.rb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'openssl'
require 'date'
require 'open-uri'
require 'json'
require 'yaml'
require 'slim'
require 'sinatra'
def btc_average_api(url)
# grab data from bitcoinaverage api
#keys = YAML.load_file('keys.yaml')
keys['public_key'] = ENV['PUBLIC_BCA']
keys['secret_key'] = ENV['SECRET_BCA']
timestamp = Time.now.to_i
payload = timestamp.to_s + "." + keys['public_key']
hex_hash = OpenSSL::HMAC.hexdigest('sha256', keys['secret_key'], payload)
signature = payload + '.' + hex_hash
response = open(url, 'X-Signature' => signature).read
return JSON.parse(response)
end
def list_exchanges
# list exchanges which list BTC price in euros.
exchanges = []
btc_average_api('https://apiv2.bitcoinaverage.com/symbols/exchanges/ticker')['exchanges'].each do | key, value |
if value['symbols'].include?('BTCEUR')
exchanges.push(value['display_name'])
end
end
return exchanges
end
def week_average(currency)
# give weekly btc/eur average, convert to float
return btc_average_api("https://apiv2.bitcoinaverage.com/indices/global/ticker/BTC#{currency}")['averages']['week'].to_f
end
def historical_average
# this gives us hourly rates, highs, lows and averages each hour over the last month
# could potentially use this to give us something better than 'weekly average'
return btc_average_api('https://apiv2.bitcoinaverage.com/indices/global/history/BTCEUR?period=monthly&?format=json')
end
get '/' do
@today = Date.today
@payroll = YAML.load_file('payroll.yaml')
@week_av = week_average("EUR")
@currencies = {}
["NZD","AUD","GBP","USD"].each do |curr|
@currencies[curr] = week_average(curr)
end
@exchanges = list_exchanges
slim :index
end