btce 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/btce.rb CHANGED
@@ -40,269 +40,8 @@ require 'openssl'
40
40
  require 'uri'
41
41
  require 'yaml'
42
42
 
43
- module Btce
44
- class API
45
- BTCE_DOMAIN = "btc-e.com"
46
- CURRENCY_PAIRS = %w(btc_usd
47
- btc_eur
48
- btc_rur
49
- eur_usd
50
- ftc_btc
51
- ltc_btc
52
- ltc_eur
53
- ltc_rur
54
- ltc_usd
55
- nmc_btc
56
- nmc_usd
57
- nvc_btc
58
- nvc_usd
59
- ppc_btc
60
- trc_btc
61
- usd_rur
62
- xpm_btc)
63
- CURRENCIES = CURRENCY_PAIRS.map {|pair| pair.split("_")}.flatten.uniq.sort
64
- MAX_DIGITS = {
65
- "btc_usd" => 3,
66
- "btc_eur" => 3,
67
- "btc_rur" => 4,
68
- "eur_usd" => 4,
69
- "ftc_btc" => 4,
70
- "ltc_btc" => 5,
71
- "ltc_eur" => 6,
72
- "ltc_rur" => 4,
73
- "ltc_usd" => 6,
74
- "nmc_btc" => 4,
75
- "nmc_usd" => 6,
76
- "nvc_btc" => 4,
77
- "nvc_usd" => 4,
78
- "ppc_btc" => 4,
79
- "trc_btc" => 6,
80
- "usd_rur" => 4,
81
- "xpm_btc" => 6
82
- }
83
-
84
- class << self
85
- def get_https(opts={})
86
- raise ArgumentError if not opts[:url].is_a? String
87
- uri = URI.parse opts[:url]
88
- http = Net::HTTP.new uri.host, uri.port
89
- http.use_ssl = true
90
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
91
- if opts[:params].nil?
92
- request = Net::HTTP::Get.new uri.request_uri
93
- else
94
- # If sending params, then we want a post request for authentication.
95
- request = Net::HTTP::Post.new uri.request_uri
96
- request.add_field "Key", opts[:key]
97
- request.add_field "Sign", opts[:signed]
98
- request.set_form_data opts[:params]
99
- end
100
- response = http.request request
101
- response.body
102
- end
103
-
104
- def get_json(opts={})
105
- result = get_https(opts)
106
- if not result.is_a? String or not result.valid_json?
107
- raise RuntimeError, "Server returned invalid data."
108
- end
109
- JSON.load result
110
- end
111
- end
112
- end
113
-
114
- class PublicAPI < API
115
- OPERATIONS = %w(fee ticker trades depth)
116
-
117
- class << self
118
- def get_pair_operation_json(pair, operation)
119
- raise ArgumentError if not API::CURRENCY_PAIRS.include? pair
120
- raise ArgumentError if not OPERATIONS.include? operation
121
- get_json({ :url => "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}" })
122
- end
123
-
124
- OPERATIONS.each do |operation|
125
- class_eval %{
126
- def get_pair_#{operation}_json(pair)
127
- get_pair_operation_json pair, "#{operation}"
128
- end
129
- }
130
-
131
- API::CURRENCY_PAIRS.each do |pair|
132
- class_eval %{
133
- def get_#{pair}_#{operation}_json
134
- get_pair_#{operation}_json "#{pair}"
135
- end
136
- }
137
- end
138
- end
139
- end
140
- end
141
-
142
- class PublicOperation
143
- attr_reader :json, :operation, :pair
144
-
145
- def initialize(operation, pair)
146
- @operation = operation
147
- @pair = pair
148
- load_json
149
- end
150
-
151
- def load_json
152
- @json = PublicAPI.get_pair_operation_json pair, operation
153
- end
154
- end
155
-
156
- class Fee < PublicOperation
157
- def initialize(pair)
158
- super 'fee', pair
159
- end
160
-
161
- def trade
162
- json["trade"]
163
- end
164
- end
165
-
166
- class Ticker < PublicOperation
167
- def initialize(pair)
168
- super 'ticker', pair
169
- end
170
-
171
- JSON_METHODS = %w(high low avg vol vol_cur last buy sell server_time)
172
-
173
- JSON_METHODS.each do |method|
174
- class_eval %{
175
- def #{method}
176
- json["ticker"]["#{method}"] if json["ticker"] and json["ticker"].is_a? Hash
177
- end
178
- }
179
- end
180
-
181
- alias_method :bid, :buy
182
- alias_method :offer, :sell
183
- alias_method :ask, :sell
184
- alias_method :average, :avg
185
- alias_method :volume, :vol
186
- alias_method :volume_current, :vol_cur
187
-
188
- def spread
189
- (offer - bid) / offer
190
- end
191
-
192
- def spread_percent
193
- spread * 100.0
194
- end
195
- end
43
+ require 'btce/api'
44
+ require 'btce/trade'
196
45
 
197
- class Trade
198
- attr_accessor :json
199
-
200
- JSON_METHODS = %w(date price amount tid price_currency item trade_type)
201
-
202
- attr_accessor *JSON_METHODS.map(&:to_sym)
203
-
204
- class << self
205
- def new_from_json(json)
206
- result = Trade.new
207
- result.json = json
208
- if json.is_a? Hash
209
- JSON_METHODS.each do |method|
210
- instance_eval %{
211
- result.#{method} = json["#{method}"]
212
- }
213
- end
214
- end
215
- result
216
- end
217
- end
218
- end
219
-
220
- class Trades < PublicOperation
221
- attr_reader :all
222
-
223
- def initialize(pair)
224
- super 'trades', pair
225
- load_trades
226
- end
227
-
228
- def load_trades
229
- @all = json.map {|trade| Trade.new_from_json trade} if json.is_a? Array
230
- end
231
- private :load_trades
232
-
233
- def [] *rest
234
- all[*rest]
235
- end
236
- end
237
-
238
- class Depth < PublicOperation
239
- def initialize(pair)
240
- super 'depth', pair
241
- end
242
- end
243
-
244
- class TradeAPI < API
245
- if File.exists? 'btce-api-key.yml'
246
- KEY = YAML::load File.open 'btce-api-key.yml'
247
-
248
- class << self
249
- def new_from_keyfile
250
- new key: KEY["key"], secret: KEY["secret"]
251
- end
252
- end
253
- end
254
-
255
- OPERATIONS = %w(getInfo
256
- TransHistory
257
- TradeHistory
258
- OrderList
259
- ActiveOrders
260
- Trade
261
- CancelOrder)
262
-
263
-
264
- def initialize(opts={})
265
- raise ArgumentError unless opts.has_key?(:key) and opts.has_key?(:secret)
266
- @key = opts[:key]
267
- @secret = opts[:secret]
268
- end
269
-
270
- def sign(params)
271
- # The digest needs to be created.
272
- hmac = OpenSSL::HMAC.new(@secret,
273
- OpenSSL::Digest::SHA512.new)
274
- params = params.collect {|k,v| "#{k}=#{v}"}.join('&')
275
- signed = hmac.update params
276
- end
277
-
278
- def trade_api_call(method, extra)
279
- params = {"method" => method, "nonce" => nonce}
280
- if ! extra.empty?
281
- extra.each do |k,v|
282
- params[k.to_s] = v
283
- end
284
- end
285
- signed = sign params
286
- API::get_json({ :url => "https://#{API::BTCE_DOMAIN}/tapi",
287
- :key => @key,
288
- :params => params,
289
- :signed => signed })
290
- end
291
-
292
- def nonce
293
- while result = Time.now.to_i and @last_nonce and @last_nonce >= result
294
- sleep 1
295
- end
296
- return @last_nonce = result
297
- end
298
- private :nonce
299
-
300
- OPERATIONS.each do |operation|
301
- class_eval %{
302
- def #{operation.camelcase_to_snakecase} extra={}
303
- trade_api_call "#{operation}", extra
304
- end
305
- }
306
- end
307
- end
46
+ module Btce
308
47
  end
data/lib/btce/api.rb ADDED
@@ -0,0 +1,109 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class API
37
+ BTCE_DOMAIN = "btc-e.com"
38
+ CURRENCY_PAIRS = %w(btc_usd
39
+ btc_eur
40
+ btc_rur
41
+ eur_usd
42
+ ftc_btc
43
+ ltc_btc
44
+ ltc_eur
45
+ ltc_rur
46
+ ltc_usd
47
+ nmc_btc
48
+ nmc_usd
49
+ nvc_btc
50
+ nvc_usd
51
+ ppc_btc
52
+ trc_btc
53
+ usd_rur
54
+ xpm_btc)
55
+ CURRENCIES = CURRENCY_PAIRS.map {|pair| pair.split("_")}.flatten.uniq.sort
56
+ MAX_DIGITS = {
57
+ "btc_usd" => 3,
58
+ "btc_eur" => 3,
59
+ "btc_rur" => 4,
60
+ "eur_usd" => 4,
61
+ "ftc_btc" => 4,
62
+ "ltc_btc" => 5,
63
+ "ltc_eur" => 6,
64
+ "ltc_rur" => 4,
65
+ "ltc_usd" => 6,
66
+ "nmc_btc" => 4,
67
+ "nmc_usd" => 6,
68
+ "nvc_btc" => 4,
69
+ "nvc_usd" => 4,
70
+ "ppc_btc" => 4,
71
+ "trc_btc" => 6,
72
+ "usd_rur" => 4,
73
+ "xpm_btc" => 6
74
+ }
75
+
76
+ class << self
77
+ def get_https(opts={})
78
+ raise ArgumentError if not opts[:url].is_a? String
79
+ uri = URI.parse opts[:url]
80
+ http = Net::HTTP.new uri.host, uri.port
81
+ http.use_ssl = true
82
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
83
+ if opts[:params].nil?
84
+ request = Net::HTTP::Get.new uri.request_uri
85
+ else
86
+ # If sending params, then we want a post request for authentication.
87
+ request = Net::HTTP::Post.new uri.request_uri
88
+ request.add_field "Key", opts[:key]
89
+ request.add_field "Sign", opts[:signed]
90
+ request.set_form_data opts[:params]
91
+ end
92
+ response = http.request request
93
+ response.body
94
+ end
95
+
96
+ def get_json(opts={})
97
+ result = get_https(opts)
98
+ if not result.is_a? String or not result.valid_json?
99
+ raise RuntimeError, "Server returned invalid data."
100
+ end
101
+ JSON.load result
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ require 'btce/api/public_api'
108
+ require 'btce/api/trade_api'
109
+ require 'btce/api/operations'
@@ -0,0 +1,40 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ require 'btce/api/operations/public_operation'
36
+
37
+ require 'btce/api/operations/depth'
38
+ require 'btce/api/operations/fee'
39
+ require 'btce/api/operations/ticker'
40
+ require 'btce/api/operations/trades'
@@ -0,0 +1,41 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class Depth < PublicOperation
37
+ def initialize(pair)
38
+ super 'depth', pair
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class Fee < PublicOperation
37
+ def initialize(pair)
38
+ super 'fee', pair
39
+ end
40
+
41
+ def trade
42
+ json["trade"]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class PublicOperation
37
+ attr_reader :json, :operation, :pair
38
+
39
+ def initialize(operation, pair)
40
+ @operation = operation
41
+ @pair = pair
42
+ load_json
43
+ end
44
+
45
+ def load_json
46
+ @json = PublicAPI.get_pair_operation_json pair, operation
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,66 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class Ticker < PublicOperation
37
+ def initialize(pair)
38
+ super 'ticker', pair
39
+ end
40
+
41
+ JSON_METHODS = %w(high low avg vol vol_cur last buy sell server_time)
42
+
43
+ JSON_METHODS.each do |method|
44
+ class_eval %{
45
+ def #{method}
46
+ json["ticker"]["#{method}"] if json["ticker"] and json["ticker"].is_a? Hash
47
+ end
48
+ }
49
+ end
50
+
51
+ alias_method :bid, :buy
52
+ alias_method :offer, :sell
53
+ alias_method :ask, :sell
54
+ alias_method :average, :avg
55
+ alias_method :volume, :vol
56
+ alias_method :volume_current, :vol_cur
57
+
58
+ def spread
59
+ (offer - bid) / offer
60
+ end
61
+
62
+ def spread_percent
63
+ spread * 100.0
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,53 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class Trades < PublicOperation
37
+ attr_reader :all
38
+
39
+ def initialize(pair)
40
+ super 'trades', pair
41
+ load_trades
42
+ end
43
+
44
+ def load_trades
45
+ @all = json.map {|trade| Trade.new_from_json trade} if json.is_a? Array
46
+ end
47
+ private :load_trades
48
+
49
+ def [] *rest
50
+ all[*rest]
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class PublicAPI < API
37
+ OPERATIONS = %w(fee ticker trades depth)
38
+
39
+ class << self
40
+ def get_pair_operation_json(pair, operation)
41
+ raise ArgumentError if not API::CURRENCY_PAIRS.include? pair
42
+ raise ArgumentError if not OPERATIONS.include? operation
43
+ get_json({ :url => "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}" })
44
+ end
45
+
46
+ OPERATIONS.each do |operation|
47
+ class_eval %{
48
+ def get_pair_#{operation}_json(pair)
49
+ get_pair_operation_json pair, "#{operation}"
50
+ end
51
+ }
52
+
53
+ API::CURRENCY_PAIRS.each do |pair|
54
+ class_eval %{
55
+ def get_#{pair}_#{operation}_json
56
+ get_pair_#{operation}_json "#{pair}"
57
+ end
58
+ }
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,100 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class TradeAPI < API
37
+ if File.exists? 'btce-api-key.yml'
38
+ KEY = YAML::load File.open 'btce-api-key.yml'
39
+
40
+ class << self
41
+ def new_from_keyfile
42
+ new key: KEY["key"], secret: KEY["secret"]
43
+ end
44
+ end
45
+ end
46
+
47
+ OPERATIONS = %w(getInfo
48
+ TransHistory
49
+ TradeHistory
50
+ OrderList
51
+ ActiveOrders
52
+ Trade
53
+ CancelOrder)
54
+
55
+
56
+ def initialize(opts={})
57
+ raise ArgumentError unless opts.has_key?(:key) and opts.has_key?(:secret)
58
+ @key = opts[:key]
59
+ @secret = opts[:secret]
60
+ end
61
+
62
+ def sign(params)
63
+ # The digest needs to be created.
64
+ hmac = OpenSSL::HMAC.new(@secret,
65
+ OpenSSL::Digest::SHA512.new)
66
+ params = params.collect {|k,v| "#{k}=#{v}"}.join('&')
67
+ signed = hmac.update params
68
+ end
69
+
70
+ def trade_api_call(method, extra)
71
+ params = {"method" => method, "nonce" => nonce}
72
+ if ! extra.empty?
73
+ extra.each do |k,v|
74
+ params[k.to_s] = v
75
+ end
76
+ end
77
+ signed = sign params
78
+ API::get_json({ :url => "https://#{API::BTCE_DOMAIN}/tapi",
79
+ :key => @key,
80
+ :params => params,
81
+ :signed => signed })
82
+ end
83
+
84
+ def nonce
85
+ while result = Time.now.to_i and @last_nonce and @last_nonce >= result
86
+ sleep 1
87
+ end
88
+ return @last_nonce = result
89
+ end
90
+ private :nonce
91
+
92
+ OPERATIONS.each do |operation|
93
+ class_eval %{
94
+ def #{operation.camelcase_to_snakecase} extra={}
95
+ trade_api_call "#{operation}", extra
96
+ end
97
+ }
98
+ end
99
+ end
100
+ end
data/lib/btce/trade.rb ADDED
@@ -0,0 +1,58 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module Btce
36
+ class Trade
37
+ attr_accessor :json
38
+
39
+ JSON_METHODS = %w(date price amount tid price_currency item trade_type)
40
+
41
+ attr_accessor *JSON_METHODS.map(&:to_sym)
42
+
43
+ class << self
44
+ def new_from_json(json)
45
+ result = Trade.new
46
+ result.json = json
47
+ if json.is_a? Hash
48
+ JSON_METHODS.each do |method|
49
+ instance_eval %{
50
+ result.#{method} = json["#{method}"]
51
+ }
52
+ end
53
+ end
54
+ result
55
+ end
56
+ end
57
+ end
58
+ end
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: btce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christoph Bünte
9
9
  - Edward Funger
10
10
  - Christopher Mark Gore
11
+ - Stephan Kaag
11
12
  - Sami Laine
12
13
  - Jaime Quint
13
14
  - Michaël Witrant
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
- date: 2013-11-28 00:00:00.000000000 Z
18
+ date: 2013-12-01 00:00:00.000000000 Z
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency
20
21
  name: monkey-patch
@@ -39,6 +40,16 @@ extensions: []
39
40
  extra_rdoc_files: []
40
41
  files:
41
42
  - lib/btce.rb
43
+ - lib/btce/api.rb
44
+ - lib/btce/api/operations.rb
45
+ - lib/btce/api/operations/depth.rb
46
+ - lib/btce/api/operations/fee.rb
47
+ - lib/btce/api/operations/public_operation.rb
48
+ - lib/btce/api/operations/ticker.rb
49
+ - lib/btce/api/operations/trades.rb
50
+ - lib/btce/api/public_api.rb
51
+ - lib/btce/api/trade_api.rb
52
+ - lib/btce/trade.rb
42
53
  homepage: https://github.com/cgore/ruby-btce
43
54
  licenses: []
44
55
  post_install_message:
@@ -64,4 +75,3 @@ signing_key:
64
75
  specification_version: 3
65
76
  summary: A simple library to interface with the API for btc-e.com in Ruby.
66
77
  test_files: []
67
- has_rdoc: