Skip to content

Commit

Permalink
Handle errors a bit better. Support for POST requests too if they wer…
Browse files Browse the repository at this point in the history
…e actually working. #1
  • Loading branch information
nolim1t committed Jan 22, 2016
1 parent 14296c9 commit 79864e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
31 changes: 20 additions & 11 deletions lib/nl-btcmarkets.rb
Expand Up @@ -10,8 +10,13 @@ class BTCMarkets
base_uri 'https://api.btcmarkets.net'

def initialize(options={})
@apisecret = Base64.decode64(ENV['btcm_access_secret'])
@apikey = ENV['btcm_access_key']
if ENV['btcm_access_secret'] and ENV['btcm_access_key'] then
@apisecret = Base64.decode64(ENV['btcm_access_secret'])
@apikey = ENV['btcm_access_key']
else
@apisecret = ''
@apikey = ''
end
@nonce = (Time.now.to_f * 1000).to_i.to_s
@parameters = ''
end
Expand All @@ -31,17 +36,21 @@ def method_missing(method_sym, *arguments, &block)
else
to_sign = '/' + convert_undercores_to_slashes + "\n" + @nonce + "\n"
end
ssl_sign = OpenSSL::HMAC.digest('sha512', @apisecret, to_sign)
ssl_sign_encoded = Base64.encode64(ssl_sign).to_s.gsub("\n",'')
if method_type == "get" then
self.class.get('/' + convert_undercores_to_slashes, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json','Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
else
if method_type == "post" and @parameters != '' then
self.class.post('/' + convert_undercores_to_slashes, :body => @parameters.to_s, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json','Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
if @apisecret != '' and @apikey != '' then
ssl_sign = OpenSSL::HMAC.digest('sha512', @apisecret, to_sign)
ssl_sign_encoded = Base64.encode64(ssl_sign).to_s.gsub("\n",'')
if method_type == "get" then
self.class.get('/' + convert_undercores_to_slashes, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json','Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
else
# No parameters
self.class.post('/' + convert_undercores_to_slashes, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
if method_type == "post" and @parameters != '' then
self.class.post('/' + convert_undercores_to_slashes, :body => @parameters.to_s, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json','Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
else
# No parameters
self.class.post('/' + convert_undercores_to_slashes, :headers => {'Accept-Charset' => 'UTF-8', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'apikey' => @apikey , 'signature' => ssl_sign_encoded, 'timestamp' => @nonce}).to_json
end
end
else
"APISECRET and APIKEY not set"
end
end
end
4 changes: 2 additions & 2 deletions nl-btcmarkets.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'nl-btcmarkets'
s.version = '0.0.2'
s.date = '2016-01-21'
s.version = '0.1.0'
s.date = '2016-01-22'
s.summary = "nolim1t's BTC Markets Ruby Gem"
s.description = "A ruby class to talk to the BTC Markets API (Work in progress!)"
s.authors = ["Barry Teoh"]
Expand Down

0 comments on commit 79864e1

Please sign in to comment.