Skip to content

Commit

Permalink
Zb integration (coingecko#310)
Browse files Browse the repository at this point in the history
* Added market.rb

* Added specs for OrderBook and Trades

* Removed redundant slash in ticker_url
  • Loading branch information
tmlee authored Feb 1, 2018
1 parent 27039a3 commit 221748b
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cryptoexchange/exchanges/zb/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cryptoexchange::Exchanges
module Zb
class Market
NAME = 'zb'
API_URL = 'http://api.zb.com/data/v1'
end
end
end
39 changes: 39 additions & 0 deletions lib/cryptoexchange/exchanges/zb/services/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Cryptoexchange::Exchanges
module Zb
module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output, market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Zb::Market::API_URL}/ticker?market=#{market_pair.base.downcase}_#{market_pair.target.downcase}"
end

def adapt(output, market_pair)
ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = Zb::Market::NAME
ticker.ask = NumericHelper.to_d(output["ticker"]["sell"])
ticker.bid = NumericHelper.to_d(output["ticker"]["buy"])
ticker.last = NumericHelper.to_d(output["ticker"]["last"])
ticker.high = NumericHelper.to_d(output["ticker"]["high"])
ticker.low = NumericHelper.to_d(output["ticker"]["low"])
ticker.volume = NumericHelper.to_d(output["ticker"]["vol"])
ticker.timestamp = Time.now.to_i
ticker.payload = output
ticker
end

end
end
end
end
44 changes: 44 additions & 0 deletions lib/cryptoexchange/exchanges/zb/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Cryptoexchange::Exchanges
module Zb
module Services
class OrderBook < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(orderbook_url(market_pair))
adapt(output, market_pair)
end

def orderbook_url(market_pair)
"#{Cryptoexchange::Exchanges::Zb::Market::API_URL}/depth?market=#{market_pair.base.downcase}_#{market_pair.target.downcase}"
end

def adapt(output, market_pair)
order_book = Cryptoexchange::Models::OrderBook.new
timestamp = output["timestamp"].to_i

order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = Zb::Market::NAME
order_book.asks = adapt_orders(output["asks"])
order_book.bids = adapt_orders(output["bids"])
order_book.timestamp = timestamp
order_book.payload = output
order_book
end

def adapt_orders(orders)
orders.collect do |order_entry|
Cryptoexchange::Models::Order.new(price: order_entry[0],
amount: order_entry[1],
timestamp: nil)
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/cryptoexchange/exchanges/zb/services/pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Cryptoexchange::Exchanges
module Zb
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::Zb::Market::API_URL}/markets"

def fetch
output = super
market_pairs = []
output.each do |pair|
pair_split = pair[0].split('_')
market_pairs << Cryptoexchange::Models::MarketPair.new(
base: pair_split[0],
target: pair_split[1],
market: Zb::Market::NAME
)
end
market_pairs
end

end
end
end
end
32 changes: 32 additions & 0 deletions lib/cryptoexchange/exchanges/zb/services/trades.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Cryptoexchange::Exchanges
module Zb
module Services
class Trades < Cryptoexchange::Services::Market
def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output, market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Zb::Market::API_URL}/trades?market=#{market_pair.base.downcase}_#{market_pair.target.downcase}"
end

def adapt(output, market_pair)
output.collect do |trade|
tr = Cryptoexchange::Models::Trade.new
tr.trade_id = trade["tid"]
tr.base = market_pair.base
tr.target = market_pair.target
tr.market = Zb::Market::NAME
tr.type = trade["type"]
tr.price = trade["price"]
tr.amount = trade["amount"]
tr.timestamp = trade["date"].to_i
tr.payload = trade
tr
end
end
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions spec/cassettes/vcr_cassettes/ZB/integration_specs_fetch_pairs.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions spec/cassettes/vcr_cassettes/ZB/integration_specs_fetch_ticker.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions spec/cassettes/vcr_cassettes/ZB/integration_specs_fetch_trade.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 221748b

Please sign in to comment.