Created
May 29, 2014 01:55
-
-
Save spikegrobstein/7de7f88ac2dee662bfcf to your computer and use it in GitHub Desktop.
quick and dirty bittrex client in ruby that spits market data to graphite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'faraday' | |
require 'oj' | |
require 'openssl' | |
require 'base64' | |
require 'digest/md5' | |
require 'ostruct' | |
require 'pry' | |
API_HOST = 'https://bittrex.com' | |
API_VERSION = 'v1.1' | |
API_NAMESPACE = "api/#{ API_VERSION }" | |
CONFIG = OpenStruct.new( | |
:api_key => API_KEY, # PUT YOUR API KEY | |
:api_secret => API_SECRET,# PUT YOUR SECRET | |
:version => API_VERSION | |
) | |
class BittrexMiddleware < Faraday::Middleware | |
attr_accessor :config | |
def initialize( app, config=nil ) | |
super app | |
@config = config | |
end | |
def call(env) | |
signature = self.class.sign( @config.api_secret, self.class.string_to_sign(@config.api_key, env) ) | |
env[:request_headers]['apisign'] = signature | |
env[:request_headers]['User-Agent'] = 'Ruby Bittrex Client' | |
@app.call(env) | |
end | |
def self.string_to_sign( api_key, env ) | |
path = "#{ env[:url].scheme }://#{ env[:url].host }#{ env[:url].request_uri }" | |
nonce = Time.now.to_i | |
"#{ path }?apikey=#{ api_key }&nonce=#{ nonce }" | |
end | |
def self.sign( secret, string_to_sign ) | |
Base64.encode64 OpenSSL::HMAC.digest( | |
OpenSSL::Digest::Digest.new('sha512'), | |
secret, | |
string_to_sign | |
) | |
end | |
end | |
CLIENT = Faraday.new( :url => "#{ API_HOST }/api/#{ API_VERSION }" ) do |f| | |
f.use BittrexMiddleware, CONFIG | |
f.adapter :net_http | |
end | |
def make_request( method ) | |
response = CLIENT.get( method ) | |
raise "error: #{ response.status }" unless response.success? | |
data = Oj.load( response.body ) | |
end | |
summaries = make_request('public/getmarketsummaries') | |
summaries['result'].each do |market| | |
name = market['MarketName'] | |
fields = %w( High Low Volume Last BaseVolume Bid Ask OpenBuyOrders OpenSellOrders PrevDay ) | |
fields.each do |field| | |
next if market[field].nil? | |
value = "%f" % market[field] | |
puts "coin.market.bittrex.#{ name }.#{ field } #{ value } #{ Time.now.to_i }" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment