mercado_bitcoin 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mercado_bitcoin/errors.rb +1 -0
- data/lib/mercado_bitcoin/trade_api.rb +25 -3
- data/lib/mercado_bitcoin/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6a463c5141cfcd838dcb6f58639314220f99010
|
4
|
+
data.tar.gz: 843f340e2d0b8a43053222874bb457693d759447
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3bc9c8cc54b0e9ba6854859c203b12f6455de2255fb67eaf228d6aa674561719f46b150d8e144d6bfcdd8a532fa13db4f298e3d2863a7a95162841398fc3821
|
7
|
+
data.tar.gz: fb60a97d933425c725b77e7455aeb7072d2dbc1d945fb08a4a302e43f47536612e89d8b6a8266670b43434fe1586f58b9299f0193b192f595b4863ff1ab8441a
|
@@ -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'))
|