bitfex 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bitfex/api.rb +6 -23
- data/lib/bitfex/version.rb +1 -1
- data/test/test_bitfex.rb +24 -52
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac2f06a3a9254389e0bb5d7c8999270f7e8f36bc017876864483d2afe210e34b
|
4
|
+
data.tar.gz: 86bbdded35e3c2f878f92b202bd8d59a9578e3043c10b446e5314a8fe4ab4d41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab8d0a9e9dfdebe36f07cea6e25b2b5f9571d3765a6acaa8af059b530e45f9ecf1fbef8f80ce30b0db8ac13c6e5cfcf6ac0f7194b9f20327be95ea5a19da5dd9
|
7
|
+
data.tar.gz: 41c9f32e4186011830bc88248cd8c41e9b1d5b5ebd9444a1a81620331a00975dfe67a62e00afa4698444471689d0991d819642a32cb2c3b8e0c251e59d0a46b2
|
data/lib/bitfex/api.rb
CHANGED
@@ -7,9 +7,9 @@ module Bitfex
|
|
7
7
|
|
8
8
|
# @param server_url [String] URL of the main server
|
9
9
|
# @param auth_server_url [String] URL of the auth server
|
10
|
-
def initialize(
|
10
|
+
def initialize(token: nil, server_url: 'https://bitfex.trade')
|
11
|
+
@token = token
|
11
12
|
@_server_url = server_url
|
12
|
-
@_auth_server_url = auth_server_url
|
13
13
|
end
|
14
14
|
|
15
15
|
# @return [String] server url
|
@@ -17,35 +17,17 @@ module Bitfex
|
|
17
17
|
@_server_url
|
18
18
|
end
|
19
19
|
|
20
|
-
# @return [String] auth server url
|
21
|
-
def auth_server_url
|
22
|
-
@_auth_server_url
|
23
|
-
end
|
24
|
-
|
25
20
|
# @param url [String] set URL of the server
|
26
21
|
def server_url=(url)
|
27
22
|
@_server_url = url
|
28
23
|
end
|
29
24
|
|
30
|
-
# @param url [String] set URL of the auth server
|
31
|
-
def auth_server_url=(url)
|
32
|
-
@_auth_server_url = url
|
33
|
-
end
|
34
|
-
|
35
|
-
# Get token for API operations
|
36
|
-
# @param email [String] email
|
37
|
-
# @param password [String] password
|
38
|
-
def auth(email, password)
|
39
|
-
body = request_post('/auth', auth: { email: email, password: password })
|
40
|
-
return @token = body['jwt'] if body['jwt']
|
41
|
-
raise AuthError.new
|
42
|
-
end
|
43
|
-
|
44
25
|
# Return account balances
|
45
26
|
# @return [Hash<String, Fixnum>] balances
|
46
27
|
def balances
|
47
28
|
response = request_get('/api/v1/user')
|
48
29
|
raise ApiError.new(response['errors'].to_json) unless response['success']
|
30
|
+
|
49
31
|
response['balances']
|
50
32
|
end
|
51
33
|
|
@@ -112,16 +94,17 @@ module Bitfex
|
|
112
94
|
end
|
113
95
|
|
114
96
|
def request(endpoint, klass, body = nil)
|
115
|
-
url =
|
97
|
+
url = @_server_url + endpoint
|
116
98
|
uri = URI.parse(url)
|
117
99
|
http = Net::HTTP.new(uri.host, uri.port)
|
118
100
|
http.use_ssl = uri.port == 443
|
119
101
|
|
120
102
|
request = klass.new(uri.request_uri, 'Content-Type' => 'application/json')
|
121
103
|
|
122
|
-
request.add_field('
|
104
|
+
request.add_field('X-API-Key', token) if token
|
123
105
|
request.body = JSON.dump(body) if body
|
124
106
|
response = http.request(request)
|
107
|
+
|
125
108
|
JSON.parse(response.body || '{}')
|
126
109
|
end
|
127
110
|
end
|
data/lib/bitfex/version.rb
CHANGED
data/test/test_bitfex.rb
CHANGED
@@ -19,110 +19,82 @@ class BitfexTest < Minitest::Test
|
|
19
19
|
assert_equal 'https://google.com/', client.server_url
|
20
20
|
end
|
21
21
|
|
22
|
-
def test_auth_success
|
23
|
-
stub_request(:post, 'https://auth.bitfex.trade/auth')
|
24
|
-
.with(body: '{"auth":{"email":"user@example.com","password":"123"}}')
|
25
|
-
.to_return(status: 200, body: '{"jwt":"token"}')
|
26
|
-
|
27
|
-
client = Bitfex::Api.new
|
28
|
-
assert_equal 'token', client.auth('user@example.com', '123')
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_auth_failure
|
32
|
-
stub_request(:post, 'https://auth.bitfex.trade/auth')
|
33
|
-
.with(body: '{"auth":{"email":"user@example.com","password":"123"}}')
|
34
|
-
.to_return(status: 404, body: '{}')
|
35
|
-
|
36
|
-
assert_raises Bitfex::AuthError do
|
37
|
-
client = Bitfex::Api.new
|
38
|
-
client.auth('user@example.com', '123')
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
22
|
def test_balances_success
|
43
23
|
stub_request(:get, "https://bitfex.trade/api/v1/user")
|
44
|
-
.with(headers: { '
|
24
|
+
.with(headers: { 'X-API-Key' => '123' })
|
45
25
|
.to_return(status: 200, body: '{"success":true,"balances":{"BTC":10.5}}')
|
46
26
|
|
47
|
-
client = Bitfex::Api.new
|
48
|
-
|
49
|
-
assert_equal({'BTC' => 10.5}, client.balances)
|
27
|
+
client = Bitfex::Api.new(token: '123')
|
28
|
+
assert_equal({ 'BTC' => 10.5 }, client.balances)
|
50
29
|
end
|
51
30
|
|
52
31
|
def test_balances_failure
|
53
32
|
stub_request(:get, "https://bitfex.trade/api/v1/user")
|
54
|
-
.with(headers: { '
|
33
|
+
.with(headers: { 'X-API-Key' => '123' })
|
55
34
|
.to_return(status: 403, body: '{"success":false,"errors":["TEST"]}')
|
56
35
|
|
57
36
|
assert_raises Bitfex::ApiError do
|
58
|
-
client = Bitfex::Api.new
|
59
|
-
client.token = '123'
|
37
|
+
client = Bitfex::Api.new(token: '123')
|
60
38
|
client.balances
|
61
39
|
end
|
62
40
|
end
|
63
41
|
|
64
42
|
def test_orders_list_success
|
65
43
|
stub_request(:get, "https://bitfex.trade/api/v1/orders?pair=BTC_RUR")
|
66
|
-
.with(headers: { '
|
44
|
+
.with(headers: { 'X-API-Key' => '123' })
|
67
45
|
.to_return(status: 200, body: '{"success":true,"orders":[{"id":1}]}')
|
68
46
|
|
69
|
-
client = Bitfex::Api.new
|
70
|
-
client.token = '123'
|
47
|
+
client = Bitfex::Api.new(token: '123')
|
71
48
|
assert_equal([{'id' => 1}], client.orders_list('BTC_RUR'))
|
72
49
|
end
|
73
50
|
|
74
51
|
def test_orders_list_faulure
|
75
52
|
stub_request(:get, "https://bitfex.trade/api/v1/orders?pair=BTC_RUR")
|
76
|
-
.with(headers: { '
|
53
|
+
.with(headers: { 'X-API-Key' => '123' })
|
77
54
|
.to_return(status: 403, body: '{"success":false,"errors":["TEST"]}')
|
78
55
|
|
79
56
|
assert_raises Bitfex::ApiError do
|
80
|
-
client = Bitfex::Api.new
|
81
|
-
client.token = '123'
|
57
|
+
client = Bitfex::Api.new(token: '123')
|
82
58
|
client.orders_list('BTC_RUR')
|
83
59
|
end
|
84
60
|
end
|
85
61
|
|
86
62
|
def test_my_orders_success
|
87
63
|
stub_request(:get, "https://bitfex.trade/api/v1/orders/my")
|
88
|
-
.with(headers: { '
|
64
|
+
.with(headers: { 'X-API-Key' => '123' })
|
89
65
|
.to_return(status: 200, body: '{"success":true,"orders":[{"id":1}]}')
|
90
66
|
|
91
|
-
client = Bitfex::Api.new
|
92
|
-
client.token = '123'
|
67
|
+
client = Bitfex::Api.new(token: '123')
|
93
68
|
assert_equal([{'id' => 1}], client.my_orders)
|
94
69
|
end
|
95
70
|
|
96
71
|
def test_my_orders_faulure
|
97
72
|
stub_request(:get, "https://bitfex.trade/api/v1/orders/my")
|
98
|
-
.with(headers: { '
|
73
|
+
.with(headers: { 'X-API-Key' => '123' })
|
99
74
|
.to_return(status: 403, body: '{"success":false,"errors":["TEST"]}')
|
100
75
|
|
101
76
|
assert_raises Bitfex::ApiError do
|
102
|
-
client = Bitfex::Api.new
|
103
|
-
client.token = '123'
|
77
|
+
client = Bitfex::Api.new(token: '123')
|
104
78
|
client.my_orders
|
105
79
|
end
|
106
80
|
end
|
107
81
|
|
108
82
|
def test_delete_order_success
|
109
83
|
stub_request(:delete, 'https://bitfex.trade/api/v1/orders/1')
|
110
|
-
.with(headers: { '
|
84
|
+
.with(headers: { 'X-API-Key' => '123' })
|
111
85
|
.to_return(status: 200, body: '{"success":true}')
|
112
86
|
|
113
|
-
client = Bitfex::Api.new
|
114
|
-
client.token = '123'
|
87
|
+
client = Bitfex::Api.new(token: '123')
|
115
88
|
assert_equal true, client.delete_order(1)
|
116
89
|
end
|
117
90
|
|
118
91
|
def test_delete_order_failure
|
119
92
|
stub_request(:delete, 'https://bitfex.trade/api/v1/orders/1')
|
120
|
-
.with(headers: { '
|
93
|
+
.with(headers: { 'X-API-Key' => '123' })
|
121
94
|
.to_return(status: 400, body: '{"success":false,"errors":["TEST"]}')
|
122
95
|
|
123
96
|
assert_raises Bitfex::ApiError do
|
124
|
-
client = Bitfex::Api.new
|
125
|
-
client.token = '123'
|
97
|
+
client = Bitfex::Api.new(token: '123')
|
126
98
|
client.delete_order(1)
|
127
99
|
end
|
128
100
|
end
|
@@ -130,23 +102,23 @@ class BitfexTest < Minitest::Test
|
|
130
102
|
def test_create_order_success
|
131
103
|
stub_request(:post, 'https://bitfex.trade/api/v1/orders')
|
132
104
|
.with(body: "{\"order\":{\"pair\":\"BTC_RUR\",\"operation\":\"buy\",\"amount\":1.0,\"price\":60000.0}}",
|
133
|
-
headers: { '
|
105
|
+
headers: { 'X-API-Key' => '123' })
|
134
106
|
.to_return(status: 200, body: '{"success":true}')
|
135
107
|
|
136
|
-
client = Bitfex::Api.new
|
137
|
-
client.token = '123'
|
108
|
+
client = Bitfex::Api.new(token: '123')
|
138
109
|
assert_equal true, client.create_order(:buy, 'BTC_RUR', 1.0, 600_00.0)
|
139
110
|
end
|
140
111
|
|
141
112
|
def test_create_order_failure
|
142
113
|
stub_request(:post, 'https://bitfex.trade/api/v1/orders')
|
143
|
-
.with(
|
144
|
-
|
114
|
+
.with(
|
115
|
+
body: '{"order":{"pair":"BTC_RUR","operation":"buy","amount":1.0,"price":60000.0}}',
|
116
|
+
headers: { 'X-API-Key' => '123' }
|
117
|
+
)
|
145
118
|
.to_return(status: 403, body: '{"success":false,"errors":["TEST"]}')
|
146
119
|
|
147
120
|
assert_raises Bitfex::ApiError do
|
148
|
-
client = Bitfex::Api.new
|
149
|
-
client.token = '123'
|
121
|
+
client = Bitfex::Api.new(token: '123')
|
150
122
|
client.create_order(:buy, 'BTC_RUR', 1.0, 600_00.0)
|
151
123
|
end
|
152
124
|
end
|