Skip to content

Commit

Permalink
leishman#3 - Implemented better EAPI:InvalidNonce solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkybro committed Mar 13, 2014
1 parent 83159e3 commit d3ba4eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/kraken_ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ def balance#(opts={})
post_private 'Balance', {} #opts
end

def trade_balance(opts={})
def trade_balance(assets=nil, opts={})
if assets
raise ArgumentError if !assets.is_a?(String)
opts[:asset] = assets
end
post_private 'TradeBalance', opts
end

Expand Down Expand Up @@ -144,6 +148,8 @@ def add_order(opts={})
private

def post_private(method, opts={})
sleep 0.3

opts['nonce'] = nonce
post_data = encode_options(opts)

Expand Down
42 changes: 36 additions & 6 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
describe Kraken::Client do

before :each do
# FIXME: perhaps it would be wiser or make more sense to implement
# this sleep within the actual library -- just not sure if that is
# a desired default behavior or if it's something that maybe should
# be left up to the developer using the library
# I moved this sleep into the post_private() method of the Client
# class, because in an actual production scenario, using the library
# would, in some cases, suffer the same problem that I guess you
# were suffering here (EAPI:InvalidNonce). So, the fix should be
# applied over the entire library, not only during spec runs.
# Additionally, it makes the spec suite run MUCH faster, since
# a nonce is not necessary for public requests.

sleep 0.3 # to prevent rapidly pinging the Kraken server
#sleep 0.3 # to prevent rapidly pinging the Kraken server
end

let(:kraken){ Kraken::Client.new(API_KEY, API_SECRET) }
Expand Down Expand Up @@ -278,7 +281,7 @@
context "fetching private data" do # More tests to come
context "using balance()" do
context "given no input" do
it "gets the user's balance(s)" do
it "gets the user's balance(s) in ZUSD" do
expect(kraken.balance).to be_instance_of(Hash)
end
end
Expand All @@ -292,6 +295,33 @@
expect { kraken.balance({:asset => 'XLTCXXDG'}) }.to raise_error(ArgumentError)
end
end
end

context "using trade_balance()" do
context "given no input" do
it "gets the user's trade balance(s) in ZUSD" do
expect(kraken.trade_balance).to be_instance_of(Hash)
end
end

context "given valid input" do
it "gets the user's trade balance(s) in the specified asset" do
zusd = kraken.trade_balance
expect(zusd).to be_instance_of(Hash)
xxdg = kraken.trade_balance('XXDG')
expect(xxdg).to be_instance_of(Hash)
expect(zusd).not_to eq xxdg
end
end

context "given invalid input" do
it "throws an ArgumentError exception" do
expect { kraken.trade_balance(1234) }.to raise_error(ArgumentError)
expect { kraken.trade_balance(1234.56) }.to raise_error(ArgumentError)
expect { kraken.trade_balance({}) }.to raise_error(ArgumentError)
expect { kraken.trade_balance([]) }.to raise_error(ArgumentError)
end
end
end
end

Expand Down

0 comments on commit d3ba4eb

Please sign in to comment.