Skip to content

Commit

Permalink
Implements new API authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelp committed Sep 26, 2013
1 parent 7988e42 commit b552dca
Show file tree
Hide file tree
Showing 14 changed files with 1,954 additions and 1,946 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ source "http://rubygems.org"
# gem "activesupport", ">= 2.3.5"
gem "activemodel", ">= 3.1"
gem "activesupport", ">= 3.1"

gem 'curb', '> 0.8.1'

gem "ruby-hmac", "0.4.0"

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GEM
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
ruby-hmac (0.4.0)
safe_yaml (0.9.7)
thread_safe (0.1.0)
atomic
Expand All @@ -58,5 +59,6 @@ DEPENDENCIES
jeweler (~> 1.8.4)
rdoc (~> 3.12)
rspec
ruby-hmac (= 0.4.0)
vcr (= 2.6.0)
webmock (= 1.13.0)
10 changes: 9 additions & 1 deletion lib/bitstamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'active_support/inflector'
require 'active_model'
require 'curb'
require 'hmac-sha2'

require 'bitstamp/net'
require 'bitstamp/helper'
Expand All @@ -20,6 +21,9 @@ module Bitstamp

# Bitstamp secret
mattr_accessor :secret

# Bitstamp client ID
mattr_accessor :client_id

# Currency
mattr_accessor :currency
Expand Down Expand Up @@ -54,9 +58,13 @@ def self.order_book
def self.setup
yield self
end

def self.configured?
self.key && self.secret && self.client_id
end

def self.sanity_check!
unless self.key || self.secret
unless configured?
raise MissingConfigExeception.new("Bitstamp Gem not properly configured")
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/bitstamp/net.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ def self.curl(verb, path, options={})

c = Curl::Easy.new(self.to_uri(path))

options[:user] = Bitstamp.key
options[:password] = Bitstamp.secret
if Bitstamp.configured?
options[:key] = Bitstamp.key
options[:nonce] = Time.now.to_i.to_s
options[:signature] = HMAC::SHA256.hexdigest(Bitstamp.secret, options[:nonce]+Bitstamp.client_id+options[:key]).upcase
end

c.post_body = options.to_query

Expand Down
13 changes: 7 additions & 6 deletions spec/bitstamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Bitstamp.setup do |config|
config.key = 'test'
config.secret = 'test'
config.client_id = 'test'
end
}
it { -> { Bitstamp.sanity_check! }.should_not raise_error }
Expand All @@ -25,19 +26,19 @@
subject { Bitstamp.ticker }
it { should be_kind_of Bitstamp::Ticker }
its(:high) { should == "124.90" }
its(:last) { should == "124.89" }
its(:timestamp) { should == "1380232743" }
its(:bid) { should == "124.70" }
its(:volume) { should == "7603.35699992" }
its(:last) { should == "124.55" }
its(:timestamp) { should == "1380237724" }
its(:bid) { should == "124.55" }
its(:volume) { should == "7766.46908740" }
its(:low) { should == "123.00" }
its(:ask) { should == "124.89" }
its(:ask) { should == "124.56" }
end

describe :balance, vcr: {cassette_name: 'bitstamp/balance'} do
context "configured" do
subject { Bitstamp.balance }
before { setup_bitstamp }
it { should == {"btc_reserved"=>"0", "fee"=>"0.4000", "btc_available"=>"0", "usd_reserved"=>"0", "btc_balance"=>"0", "usd_balance"=>"6953.07", "usd_available"=>"6953.07"} }
it { should == {"btc_reserved"=>"0", "fee"=>"0.4000", "btc_available"=>"0", "usd_reserved"=>"1.02", "btc_balance"=>"0", "usd_balance"=>"6953.07", "usd_available"=>"6952.05"} }
end
context "not configured" do
it { expect { Bitstamp.balance }.to raise_exception(Bitstamp::MissingConfigExeception, "Bitstamp Gem not properly configured") }
Expand Down
26 changes: 13 additions & 13 deletions spec/fixtures/vcr_cassettes/bitstamp/balance.yml

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

Loading

0 comments on commit b552dca

Please sign in to comment.