From 35615bec52caab63016660b6a9e173404b18a9f1 Mon Sep 17 00:00:00 2001 From: Yuri Tytarenko Date: Wed, 3 Jul 2019 14:12:45 +0300 Subject: [PATCH] New Capital exchange integration. --- .../exchanges/new_capital/market.rb | 12 +++++ .../exchanges/new_capital/services/market.rb | 51 +++++++++++++++++++ .../exchanges/new_capital/services/pairs.rb | 24 +++++++++ .../integration_specs_fetch_pairs.yml | 45 ++++++++++++++++ .../new_capital/integration/market_spec.rb | 31 +++++++++++ spec/exchanges/new_capital/market_spec.rb | 6 +++ 6 files changed, 169 insertions(+) create mode 100644 lib/cryptoexchange/exchanges/new_capital/market.rb create mode 100644 lib/cryptoexchange/exchanges/new_capital/services/market.rb create mode 100644 lib/cryptoexchange/exchanges/new_capital/services/pairs.rb create mode 100644 spec/cassettes/vcr_cassettes/NewCapital/integration_specs_fetch_pairs.yml create mode 100644 spec/exchanges/new_capital/integration/market_spec.rb create mode 100644 spec/exchanges/new_capital/market_spec.rb diff --git a/lib/cryptoexchange/exchanges/new_capital/market.rb b/lib/cryptoexchange/exchanges/new_capital/market.rb new file mode 100644 index 000000000..84f6fcf10 --- /dev/null +++ b/lib/cryptoexchange/exchanges/new_capital/market.rb @@ -0,0 +1,12 @@ +module Cryptoexchange::Exchanges + module NewCapital + class Market < Cryptoexchange::Models::Market + NAME = 'new_capital' + API_URL = 'https://api.new.capital/v1' + + def self.trade_page_url(args={}) + "https://new.capital/exchange/trade/#{args[:target].downcase}_#{args[:base].downcase}" + end + end + end +end diff --git a/lib/cryptoexchange/exchanges/new_capital/services/market.rb b/lib/cryptoexchange/exchanges/new_capital/services/market.rb new file mode 100644 index 000000000..775801f31 --- /dev/null +++ b/lib/cryptoexchange/exchanges/new_capital/services/market.rb @@ -0,0 +1,51 @@ +module Cryptoexchange::Exchanges + module NewCapital + module Services + class Market < Cryptoexchange::Services::Market + class << self + def supports_individual_ticker_query? + false + end + end + + def fetch + output = super(ticker_url) + adapt_all(output) + end + + def ticker_url + "#{Cryptoexchange::Exchanges::NewCapital::Market::API_URL}/ticker" + end + + def adapt_all(output) + output.map do |pair| + base, target = pair["symbol"].split('_') + market_pair = Cryptoexchange::Models::MarketPair.new( + base: base, + target: target, + market: NewCapital::Market::NAME + ) + adapt(market_pair, pair) + end + end + + def adapt(market_pair, output) + ticker = Cryptoexchange::Models::Ticker.new + ticker.base = market_pair.base + ticker.target = market_pair.target + ticker.market = NewCapital::Market::NAME + ticker.last = NumericHelper.to_d(output['lastPrice']) + ticker.bid = NumericHelper.to_d(output['bidPrice']) + ticker.ask = NumericHelper.to_d(output['askPrice']) + ticker.high = NumericHelper.to_d(output['highPrice']) + ticker.low = NumericHelper.to_d(output['lowPrice']) + ticker.volume = NumericHelper.to_d(output['volume']) + ticker.change = NumericHelper.to_d(output['priceChangePercent']) + ticker.timestamp = nil + ticker.payload = output + ticker + end + end + end + end +end diff --git a/lib/cryptoexchange/exchanges/new_capital/services/pairs.rb b/lib/cryptoexchange/exchanges/new_capital/services/pairs.rb new file mode 100644 index 000000000..4aa4faed6 --- /dev/null +++ b/lib/cryptoexchange/exchanges/new_capital/services/pairs.rb @@ -0,0 +1,24 @@ +module Cryptoexchange::Exchanges + module NewCapital + module Services + class Pairs < Cryptoexchange::Services::Pairs + PAIRS_URL = "#{Cryptoexchange::Exchanges::NewCapital::Market::API_URL}/ticker" + + def fetch + output = super + market_pairs = [] + output.each do |pair| + base, target = pair["symbol"].split('_') + market_pairs << Cryptoexchange::Models::MarketPair.new( + base: base, + target: target, + market: NewCapital::Market::NAME + ) + end + market_pairs + end + + end + end + end +end diff --git a/spec/cassettes/vcr_cassettes/NewCapital/integration_specs_fetch_pairs.yml b/spec/cassettes/vcr_cassettes/NewCapital/integration_specs_fetch_pairs.yml new file mode 100644 index 000000000..e48cbd598 --- /dev/null +++ b/spec/cassettes/vcr_cassettes/NewCapital/integration_specs_fetch_pairs.yml @@ -0,0 +1,45 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.new.capital/v1/ticker + body: + encoding: UTF-8 + string: '' + headers: + Connection: + - close + Host: + - api.new.capital + User-Agent: + - http.rb/5.0.0.pre + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 03 Jul 2019 11:08:00 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - close + Set-Cookie: + - __cfduid=d44daa61adc8118f22f007d7f34c457241562152080; expires=Thu, 02-Jul-20 + 11:08:00 GMT; path=/; domain=.new.capital; HttpOnly; Secure + X-Frame-Options: + - SAMEORIGIN + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 4f086628d985bedd-FRA + body: + encoding: UTF-8 + string: '[{"symbol":"BCH_TWINS","priceChange":"0","priceChangePercent":"0","lastPrice":"0","bidPrice":0,"askPrice":0,"openPrice":"0","highPrice":"0","lowPrice":"0","volume":"0.00000000","quoteVolume":"0.00000000","openTime":0,"closeTime":0,"firstId":0,"lastId":0,"count":0},{"symbol":"BTC_TWINS","priceChange":"-12409800","priceChangePercent":"-41.36627577","lastPrice":"17590000","bidPrice":"18000000","askPrice":"22000000","openPrice":"29999800","highPrice":"29999999","lowPrice":"17500000","volume":"0.41555907","quoteVolume":"9099404.59082880","openTime":1562073590,"closeTime":1562138583,"firstId":196,"lastId":276,"count":81},{"symbol":"FIX_TWINS","priceChange":"0.0000","priceChangePercent":"0","lastPrice":"0.0000","bidPrice":"1000.0000","askPrice":0,"openPrice":"0.0000","highPrice":"0.0000","lowPrice":"0.0000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":0,"closeTime":0,"firstId":0,"lastId":0,"count":0},{"symbol":"LTC_TWINS","priceChange":"0.0000","priceChangePercent":"0","lastPrice":"0.0000","bidPrice":0,"askPrice":0,"openPrice":"0.0000","highPrice":"0.0000","lowPrice":"0.0000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":0,"closeTime":0,"firstId":0,"lastId":0,"count":0},{"symbol":"XEM_TWINS","priceChange":"0.0000","priceChangePercent":"0","lastPrice":"0.0000","bidPrice":0,"askPrice":0,"openPrice":"0.0000","highPrice":"0.0000","lowPrice":"0.0000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":0,"closeTime":0,"firstId":0,"lastId":0,"count":0}]' + http_version: + recorded_at: Wed, 03 Jul 2019 11:08:01 GMT +recorded_with: VCR 4.0.0 diff --git a/spec/exchanges/new_capital/integration/market_spec.rb b/spec/exchanges/new_capital/integration/market_spec.rb new file mode 100644 index 000000000..cbdd46e1b --- /dev/null +++ b/spec/exchanges/new_capital/integration/market_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +RSpec.describe 'NewCapital integration specs' do + let(:client) {Cryptoexchange::Client.new} + let(:twins_btc_pair) {Cryptoexchange::Models::MarketPair.new(base: 'BTC', target: 'TWINS', market: 'new_capital')} + + it 'fetch pairs' do + pairs = client.pairs('new_capital') + expect(pairs).not_to be_empty + pair = pairs.first + expect(pair.base).to_not be nil + expect(pair.target).to_not be nil + expect(pair.market).to eq 'new_capital' + end + + it 'fetch ticker' do + ticker = client.ticker(twins_btc_pair) + expect(ticker.base).to eq 'BTC' + expect(ticker.target).to eq 'TWINS' + expect(ticker.market).to eq 'new_capital' + expect(ticker.last).to be_a Numeric + expect(ticker.bid).to be_a Numeric + expect(ticker.ask).to be_a Numeric + expect(ticker.low).to be_a Numeric + expect(ticker.high).to be_a Numeric + expect(ticker.volume).to be_a Numeric + expect(ticker.timestamp).to be nil + expect(ticker.payload).to_not be nil + end + +end \ No newline at end of file diff --git a/spec/exchanges/new_capital/market_spec.rb b/spec/exchanges/new_capital/market_spec.rb new file mode 100644 index 000000000..38a779662 --- /dev/null +++ b/spec/exchanges/new_capital/market_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +RSpec.describe Cryptoexchange::Exchanges::NewCapital::Market do + it { expect(described_class::NAME).to eq 'new_capital' } + it { expect(described_class::API_URL).to eq 'https://api.new.capital/v1' } +end