Skip to content

Instantly share code, notes, and snippets.

@hamstarr
Last active December 29, 2015 23:18
Show Gist options
  • Save hamstarr/7741507 to your computer and use it in GitHub Desktop.
Save hamstarr/7741507 to your computer and use it in GitHub Desktop.
Dirty wee Bitcoin cli ticker
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'open-uri'
require 'json'
def fetch_ticker
content = open("https://mtgox.com/api/1/BTCUSD/ticker").read
result = JSON.parse(content)
result['return']['last']
end
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
EventMachine.run do
current_value = nil
previous_value = nil
EventMachine.add_periodic_timer(5) do
current = fetch_ticker
current_value = current['value_int'].to_i
display = current['display']
ticker = nil
if previous_value
if current_value < previous_value
ticker = red display
elsif current_value > previous_value
ticker = green display
end
else
ticker = display
end
puts "#{Time.now}: #{ticker}" if ticker
previous_value = current_value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment