Skip to content

Commit

Permalink
100% tests again
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjmp committed May 7, 2021
1 parent 0442b32 commit 3110514
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
*.gem

.idea/
.byebug*
2 changes: 1 addition & 1 deletion binance-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'timecop', '~> 0.9'
spec.add_development_dependency "webmock", '~> 3.0'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'pry', '~> 0.11.3'
spec.add_development_dependency 'byebug', '~> 11.1.3'
spec.add_development_dependency 'simplecov', '~> 0.20.0'

spec.add_dependency 'activesupport', '>= 5.1.0'
Expand Down
19 changes: 12 additions & 7 deletions lib/binance/websocket.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Binance
class WebSocket < Faye::WebSocket::Client
class Error < StandardError; end

def initialize(on_open: nil, on_close: nil)
super "wss://stream.binance.com:9443/stream", nil, ping: 180

Expand Down Expand Up @@ -78,12 +80,15 @@ def subscribe(streams)
}.to_json)
end

def unsubscribe(streams)
send({
method: "UNSUBSCRIBE",
params: streams,
id: request_id,
}.to_json)
end
# Terminating socket connection achieves the same result.
# If you have a use-case for this, please create a GitHub issue.
#
# def unsubscribe(streams)
# send({
# method: "UNSUBSCRIBE",
# params: streams,
# id: request_id,
# }.to_json)
# end
end
end
60 changes: 60 additions & 0 deletions spec/binance/websocket_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "spec_helper"

RSpec.describe Binance::WebSocket do
let(:interval) { "1h" }
let(:json_string) { '{ "result": null, "id": 312 }' }
let(:stream_name) { nil }
let(:symbols) { %w(ETHBTC) }
let(:websocket) { Binance::WebSocket.new }

describe "#candlesticks" do
let(:stream_name) { "#{symbols.first.downcase}@kline_#{interval}" }

before do
stub_request(:any, "wss://stream.binance.com:9443/stream")
.to_return(status: 200, body: "")
events = OpenStruct.new(
open: nil,
message: nil,
close: nil,
)
allow_any_instance_of(Binance::WebSocket).to receive(:on) do |_ws, kind, &block|
event = case kind
when :open
when :message
OpenStruct.new(data: json_string)
when :close
end
events.send("#{kind}=", -> { block.call(event) })
end
allow_any_instance_of(Binance::WebSocket).to receive(:send) do
events.open&.call
events.message&.call
events.close&.call
end
end

context "error" do
let(:json_string) { '{ "error": {"code": 0, "msg": "Unknown property","id": 123} }' }

subject { websocket.candlesticks!(symbols, interval) }

it { is_expected_block.to raise_error Binance::WebSocket::Error }
end

context "kline" do
let(:json_string) do
{
stream: stream_name,
data: JSON.parse(File.read("spec/fixtures/kline.json"), symbolize_names: true),
}.to_json
end

it "calls on_receive" do
inc = 0
websocket.candlesticks!(symbols, interval) { inc = 1 }
expect(inc).to eq 1
end
end
end
end
24 changes: 24 additions & 0 deletions spec/fixtures/kline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"e": "kline",
"E": 123456789,
"s": "BNBBTC",
"k": {
"t": 123400000,
"T": 123460000,
"s": "BNBBTC",
"i": "1m",
"f": 100,
"L": 200,
"o": "0.0010",
"c": "0.0020",
"h": "0.0025",
"l": "0.0015",
"v": "1000",
"n": 100,
"x": false,
"q": "1.0000",
"V": "500",
"Q": "0.500",
"B": "123456"
}
}
16 changes: 8 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Needs to be called before app code is required!
require 'simplecov'
require "simplecov"
SimpleCov.start

require 'dotenv/load'
require "dotenv/load"
require "bundler/setup"
require 'webmock/rspec'
require 'codecov'
require 'timecop'
require 'pry'
require "webmock/rspec"
require "codecov"
require "timecop"
require "byebug"

SimpleCov.formatter = SimpleCov::Formatter::Codecov unless ENV['CODECOV_TOKEN'].nil?
SimpleCov.formatter = SimpleCov::Formatter::Codecov unless ENV["CODECOV_TOKEN"].nil?

# Require support files
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each { |file| require file }
Dir[File.expand_path(File.join(File.dirname(__FILE__), "support", "**", "*.rb"))].each { |file| require file }

WebMock.disable_net_connect!

Expand Down

0 comments on commit 3110514

Please sign in to comment.