-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
executable file
·106 lines (89 loc) · 2.39 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'rubygems'
require 'sinatra'
require 'haml'
require 'coffee-script'
require 'json'
# Helpers
require './lib/render_partial'
enable :logging
# Set Sinatra variables
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :views, 'views'
set :public_folder, 'public'
set :haml, {:format => :html5} # default Haml format is :xhtml
@@mtgox = {:USD => false, :GBP => false, :CHF => false, :EUR => false}
@@bitstamp = {:high => false, :low => false}
@@btc_e = {:USD => false, :EUR => false}
# Application routes
get '/' do
haml :index, :layout => :'layouts/application'
end
get '/about' do
haml :about, :layout => :'layouts/page'
end
get '/mtgox/:currency.json' do
if @@mtgox.keys.include? params[:currency].to_sym
headers({'Content-Type' => 'application/x-json'})
body({:rate => @@mtgox[params[:currency].to_sym]}.to_json)
else
halt 404
end
end
get '/btc_e/:currency.json' do
if @@btc_e.keys.include? params[:currency].to_sym
headers({'Content-Type' => 'application/x-json'})
body({:rate => @@btc_e[params[:currency].to_sym]}.to_json)
else
halt 404
end
end
get '/bitstamp.json' do
headers 'Content-Type' => 'application/x-json'
body @@bitstamp.to_json
end
require 'net/https'
require 'pp'
def update_mtgox
@@mtgox.keys.each do |k|
p "MtGox: Updating #{k} ticker..."
uri = URI("https://data.mtgox.com/api/1/BTC#{k}/ticker")
h = Net::HTTP.new uri.host, uri.port
h.use_ssl = true
resp = h.get uri.request_uri
res = JSON.parse resp.body
if res['result'] == 'success'
@@mtgox[k] = res['return']['avg']['value']
end
end
end
def update_bitstamp
p 'BitStamp: Updating ticker...'
uri = URI('https://www.bitstamp.net/api/ticker/')
h = Net::HTTP.new uri.host, uri.port
h.use_ssl = true
resp = h.get uri.request_uri
res = JSON.parse resp.body
@@bitstamp = {:high => res['high'], :low => res['low']}
end
def update_btce
@@btc_e.keys.each do |k|
p "BTC-E: Updating #{k} ticker..."
uri = URI("https://btc-e.com/api/2/btc_#{k.to_s.downcase}/ticker")
h = Net::HTTP.new uri.host, uri.port
h.use_ssl = true
resp = h.get uri.request_uri
res = JSON.parse resp.body
@@btc_e[k] = res['ticker']['avg']
end
end
%w(mtgox bitstamp btce).map do |p|
name = "update_#{p}".to_sym
send(name)
Thread.new do
while true
sleep 2
send(name)
end
end
end