mercado_bitcoin 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf8f7d12fa21c1ec6a442308e8cd63c156783d3e
4
- data.tar.gz: 7588adf292d1fbb72565f859d42fb256134c9364
3
+ metadata.gz: f6a463c5141cfcd838dcb6f58639314220f99010
4
+ data.tar.gz: 843f340e2d0b8a43053222874bb457693d759447
5
5
  SHA512:
6
- metadata.gz: a4392debb609e0589fe0968ba39649032d618b241c3144c8d54780f7090eae7f1164b29a1f0f204ca2aaad4ae14b95a6673ea29e0b61c54dab7923e4d354b5c3
7
- data.tar.gz: 02b6ea32d6b641f3dc3c795b11c0748cf775de6aa630e74cf1c7b685bb46ffd2530800d665b025cf179bafe87e53af68b2f04e0dc69cc6efc53c58b76227a91b
6
+ metadata.gz: b3bc9c8cc54b0e9ba6854859c203b12f6455de2255fb67eaf228d6aa674561719f46b150d8e144d6bfcdd8a532fa13db4f298e3d2863a7a95162841398fc3821
7
+ data.tar.gz: fb60a97d933425c725b77e7455aeb7072d2dbc1d945fb08a4a302e43f47536612e89d8b6a8266670b43434fe1586f58b9299f0193b192f595b4863ff1ab8441a
@@ -2,4 +2,5 @@ module MercadoBitcoin
2
2
  class Error < StandardError; end
3
3
  class ParserError < Error; end
4
4
  class CoinTypeError < Error; end
5
+ class TonceDesyncError < Error; end
5
6
  end
@@ -49,16 +49,31 @@ module MercadoBitcoin
49
49
  end
50
50
 
51
51
  def post(params)
52
+ params[:tonce] = Time.new.to_i + env_or_var_tonce
52
53
  signature = sign(params)
53
- JSON.parse(
54
+ result = JSON.parse(
54
55
  RestClient.post(
55
56
  base_url,
56
57
  params.to_query_string,
57
58
  header(signature)
58
59
  )
59
60
  )
61
+ raise TonceDesyncError.new('desync') if tonce_error?(result)
62
+ result
63
+ rescue TonceDesyncError
64
+ @tonce_correction = get_tonce_correction(result)
65
+ retry
60
66
  end
61
67
 
68
+ def tonce_error?(result)
69
+ if result['error'].to_s =~ /(\d+) e (\d+), tonce recebido (\d+)+/
70
+ $1.to_i - $3.to_i + 10
71
+ else
72
+ false
73
+ end
74
+ end
75
+ alias_method :get_tonce_correction, :tonce_error?
76
+
62
77
  def base_url
63
78
  @base_url ||= "https://www.mercadobitcoin.net/tapi/".freeze
64
79
  end
@@ -73,11 +88,18 @@ module MercadoBitcoin
73
88
 
74
89
  def base_params(method)
75
90
  {
76
- method: method,
77
- tonce: Time.new.to_i + (ENV['TONCE_CORRECTION'] || tonce_correction).to_i
91
+ method: method
78
92
  }
79
93
  end
80
94
 
95
+ def env_or_var_tonce
96
+ if ENV['TONCE_CORRECTION'].to_s =~ /\d+/
97
+ ENV['TONCE_CORRECTION'].to_i
98
+ else
99
+ tonce_correction
100
+ end
101
+ end
102
+
81
103
  def sign(string_or_hash)
82
104
  string_or_hash = string_or_hash.to_query_string if string_or_hash.is_a?(Hash)
83
105
  hmac = OpenSSL::HMAC.new(code, OpenSSL::Digest.new('sha512'))
@@ -1,3 +1,3 @@
1
1
  module MercadoBitcoin
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mercado_bitcoin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Carvalho