mirror of
https://github.com/cmur2/munin-bitcoin.git
synced 2025-01-24 03:27:07 +01:00
Wrap data retrieval into block to be only called when needed
This commit is contained in:
parent
4dcf1d5efa
commit
b570fb112b
7
bitcoin_
7
bitcoin_
@ -29,7 +29,7 @@ def uri(currency)
|
|||||||
"https://api.bitcoinaverage.com/ticker/#{currency}"
|
"https://api.bitcoinaverage.com/ticker/#{currency}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def echo_price(currency, data)
|
def echo_price(currency, &get_data)
|
||||||
case @cmd
|
case @cmd
|
||||||
when 'config'
|
when 'config'
|
||||||
puts "graph_title Bitcoin (in #{currency})"
|
puts "graph_title Bitcoin (in #{currency})"
|
||||||
@ -43,6 +43,7 @@ def echo_price(currency, data)
|
|||||||
puts "#{x}.type GAUGE"
|
puts "#{x}.type GAUGE"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
data = get_data.call
|
||||||
%w{last ask bid total_vol 24h_avg}.each do |x|
|
%w{last ask bid total_vol 24h_avg}.each do |x|
|
||||||
puts "#{x}.value #{data[x]}"
|
puts "#{x}.value #{data[x]}"
|
||||||
end
|
end
|
||||||
@ -50,10 +51,12 @@ def echo_price(currency, data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
echo_price(@currency) do
|
||||||
raw_data = open(uri(@currency)).read
|
raw_data = open(uri(@currency)).read
|
||||||
data = JSON.parse(raw_data)
|
data = JSON.parse(raw_data)
|
||||||
#puts data.inspect
|
#puts data.inspect
|
||||||
echo_price(@currency, data)
|
data
|
||||||
|
end
|
||||||
rescue => ex
|
rescue => ex
|
||||||
$stderr.puts "Cannot connect to bitcoinaverage.com: #{ex.to_s}"
|
$stderr.puts "Cannot connect to bitcoinaverage.com: #{ex.to_s}"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user