Skip to content

Instantly share code, notes, and snippets.

@carlzulauf
Created November 14, 2013 08:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlzulauf/7463352 to your computer and use it in GitHub Desktop.
Save carlzulauf/7463352 to your computer and use it in GitHub Desktop.
Watch the price of Bitcoin on Coinbase, and alert me.
require 'logger'
require 'net/http'
require 'json'
require 'mail' # gem 'mail'
dir = File.dirname(__FILE__)
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
# credentials.yml example:
# ---
# :user_name: some+user@gmail.com
# :password: muchdoge
#
credentials = YAML.load_file(File.join(dir, "credentials.yml"))
Mail.defaults do
delivery_method :smtp, credentials.merge(
address: "smtp.gmail.com",
port: "587",
authentication: :plain,
enable_starttls_auto: true
)
end
def send_notice(to, price)
Mail.deliver do
from "BTC Communicator <#{to}>"
to to
subject "BTC is TOO DAMN HIGH! ($#{price})"
body "Get out now, while you still can!"
end
end
target = 990.0
seconds = 30 # seconds between API calls
loop do
json = Net::HTTP.get URI("https://coinbase.com/api/v1/prices/spot_rate")
logger.debug "JSON Response: #{json}"
data = JSON.parse json
price = data["amount"].to_f
logger.debug "Price: $ #{price}"
if price > target
logger.info "Price ($#{price}) above target ($#{target}). Sending alert."
send_notice credentials[:user_name], price
break
else
logger.info "Price ($#{price}) below target ($#{target}). Doing nothing."
end
sleep seconds
end
@jrenouard
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment