Skip to content

Commit

Permalink
update buyucoin api
Browse files Browse the repository at this point in the history
  • Loading branch information
teekenl committed May 29, 2020
1 parent e7f8daa commit c4d8ee0
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 241 deletions.
2 changes: 1 addition & 1 deletion lib/cryptoexchange/exchanges/buyucoin/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cryptoexchange::Exchanges
module Buyucoin
class Market < Cryptoexchange::Models::Market
NAME = 'buyucoin'
API_URL = 'https://www.buyucoin.com/api/v1.2'
API_URL = 'https://api.buyucoin.com/ticker/v1.0'
end
end
end
34 changes: 11 additions & 23 deletions lib/cryptoexchange/exchanges/buyucoin/services/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,17 @@ module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
false
true
end
end

def fetch
output = super(ticker_url)
adapt_all(output)
def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output['data'][0], market_pair)
end

def ticker_url
"#{Cryptoexchange::Exchanges::Buyucoin::Market::API_URL}/currency/markets"
end

def adapt_all(output)
output['data'].map do |pair, ticker|
base, target = pair.split('_')
market_pair = Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: Buyucoin::Market::NAME
)
adapt(ticker, market_pair)
end
def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Buyucoin::Market::API_URL}/liveData?symbol=#{market_pair.target}-#{market_pair.base}"
end

def adapt(output, market_pair)
Expand All @@ -36,11 +24,11 @@ def adapt(output, market_pair)
ticker.market = Buyucoin::Market::NAME
ticker.ask = NumericHelper.to_d(output['ask'])
ticker.bid = NumericHelper.to_d(output['bid'])
ticker.high = NumericHelper.to_d(output['high_24'])
ticker.low = NumericHelper.to_d(output['low_24'])
ticker.change = NumericHelper.to_d(output['change'])
ticker.last = NumericHelper.to_d(output['last_trade'])
ticker.volume = NumericHelper.to_d(output['vol'])
ticker.high = NumericHelper.to_d(output['h24'])
ticker.low = NumericHelper.to_d(output['l24'])
ticker.change = NumericHelper.to_d(output['c24'])
ticker.last = NumericHelper.to_d(output['LTRate'])
ticker.volume = NumericHelper.to_d(output['LBVol'])
ticker.timestamp = nil
ticker.payload = output
ticker
Expand Down
43 changes: 43 additions & 0 deletions lib/cryptoexchange/exchanges/buyucoin/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Cryptoexchange::Exchanges
module Buyucoin
module Services
class OrderBook < 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['data'], market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Buyucoin::Market::API_URL}/liveOrderBook?symbol=#{market_pair.target}-#{market_pair.base}"
end

def adapt(output, market_pair)
order_book = Cryptoexchange::Models::OrderBook.new

order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = Buyucoin::Market::NAME
order_book.asks = adapt_orders(output['SELL'])
order_book.bids = adapt_orders(output['BUY'])
order_book.timestamp = nil
order_book.payload = output
order_book
end

def adapt_orders(orders)
orders.collect do |order|
Cryptoexchange::Models::Order.new(price: order["price"].to_f,
amount: order["qty"].to_f,
timestamp: nil)
end
end
end
end
end
end
7 changes: 3 additions & 4 deletions lib/cryptoexchange/exchanges/buyucoin/services/pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ module Cryptoexchange::Exchanges
module Buyucoin
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::Buyucoin::Market::API_URL}/currency/markets"

PAIRS_URL = "#{Cryptoexchange::Exchanges::Buyucoin::Market::API_URL}/liveData"

def fetch
output = super
adapt(output)
end

def adapt(output)
output['data'].map do |pair, _value|
base, target = pair.split('_')
output['data'].map do |value|
target, base = value["marketName"].split('-')
Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
Expand Down

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

128 changes: 22 additions & 106 deletions spec/cassettes/vcr_cassettes/Buyucoin/integration_specs_fetch_pairs.yml

Large diffs are not rendered by default.

128 changes: 22 additions & 106 deletions spec/cassettes/vcr_cassettes/Buyucoin/integration_specs_fetch_ticker.yml

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

15 changes: 15 additions & 0 deletions spec/exchanges/buyucoin/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,19 @@
expect(ticker.timestamp).to be nil
expect(ticker.payload).to_not be nil
end

it 'fetch order book' do
order_book = client.order_book(btc_inr_pair)

expect(order_book.base).to eq 'BTC'
expect(order_book.target).to eq 'INR'
expect(order_book.market).to eq 'buyucoin'
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.price).to_not be_nil
expect(order_book.bids.first.amount).to_not be_nil
expect(order_book.asks.count).to be > 10
expect(order_book.bids.count).to be > 10
expect(order_book.payload).to_not be nil
end
end
2 changes: 1 addition & 1 deletion spec/exchanges/buyucoin/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

RSpec.describe Cryptoexchange::Exchanges::Buyucoin::Market do
it { expect(described_class::NAME).to eq 'buyucoin' }
it { expect(described_class::API_URL).to eq 'https://www.buyucoin.com/api/v1.2' }
it { expect(described_class::API_URL).to eq 'https://api.buyucoin.com/ticker/v1.0' }
end

0 comments on commit c4d8ee0

Please sign in to comment.