Skip to content

Instantly share code, notes, and snippets.

@josephdunn
Created May 26, 2011 00:18
Show Gist options
  • Save josephdunn/992287 to your computer and use it in GitHub Desktop.
Save josephdunn/992287 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'em-http-request'
require 'json'
EventMachine.run {
bid = 0.0
ask = 0.0
spread = 0.0
http = EventMachine::HttpRequest.new("ws://websocket.mtgox.com/mtgox").get :timeout => 0
http.errback { puts "oops" }
http.callback {
puts "WebSocket connected!"
}
http.stream { |msg|
msg = JSON.parse(msg)
if msg['channel'] = 'dbf1dee9-4f2e-4a08-8cb7-748919a71b21'
if msg['private'] != 'ticker' && msg['private'] != 'depth'
msg['trade']['date'] = Time.at(msg['trade']['date']) if msg.has_key?('trade')
p msg
end
elsif msg['channel'] == 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f'
if msg['private'] == 'ticker'
bid = msg['ticker']['buy']
ask = msg['ticker']['sell']
spread = ask - bid
puts "bid #{bid} ask #{ask} spread #{spread}"
end
end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment