andrewroth-btce 0.1.9.4 → 0.3.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4a8b33e2d392af5b7e0419e4d5bd90ccf480952
4
+ data.tar.gz: e1bbf7015cdc23db6584848194483a6e6425fae3
5
+ SHA512:
6
+ metadata.gz: 58851b0e0100302f0d3c8497bf3a75a9f8de52958f86af33c32297ab0ba18798796ab5c2a56db9080f922e0426800fd5064c805a48688a2ec9d8afcac049c3e9
7
+ data.tar.gz: 9be81a11e6c7d032667ac1f4d44a3fc906ac28c29c5041e4af7c3f7606858377d0308a2b55a6f93d93d590a3aae7c7f1b757844a7acde826364e56c886ae5fb6
data/lib/btce.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # Soli Deo Gloria,
3
3
  # All rights reserved.
4
4
  #
5
- # 8729 Lower Marine Road, Saint Jacob, Illinois 62281 USA.
5
+ # 2317 South River Road, Saint Charles, Missouri 63303 USA.
6
6
  # Web: http://cgore.com
7
7
  # Email: cgore@cgore.com
8
8
  #
@@ -39,268 +39,9 @@ require 'net/https'
39
39
  require 'openssl'
40
40
  require 'uri'
41
41
  require 'yaml'
42
- require 'cgi'
43
42
 
43
+ require 'btce/api'
44
+ require 'btce/trade'
44
45
 
45
46
  module Btce
46
- class API
47
- BTCE_DOMAIN = "btc-e.com"
48
- CURRENCIES = %w(btc
49
- usd
50
- rur
51
- ltc
52
- nmc
53
- eur
54
- nvc
55
- trc
56
- ppc
57
- fnc)
58
- CURRENCY_PAIRS = %w(btc_usd
59
- btc_eur
60
- btc_rur
61
- ltc_btc
62
- ltc_usd
63
- ltc_rur
64
- nmc_btc
65
- usd_rur
66
- eur_usd
67
- nvc_btc
68
- trc_btc
69
- ppc_btc
70
- ftc_btc)
71
- MAX_DIGITS = {
72
- "btc_usd" => 3,
73
- "btc_eur" => 3,
74
- "btc_rur" => 4,
75
- "ltc_btc" => 5,
76
- "ltc_usd" => 6,
77
- "ltc_rur" => 4,
78
- "nmc_btc" => 4,
79
- "usd_rur" => 4,
80
- "eur_usd" => 4,
81
- "nvc_btc" => 4,
82
- "trc_btc" => 4,
83
- "ppc_btc" => 4,
84
- "ftc_btc" => 4
85
- }
86
- KEY = YAML::load File.open 'btce-api-key.yml'
87
-
88
- class << self
89
- def get_https(url, params = nil, sign = nil)
90
- raise ArgumentError if not url.is_a? String
91
- uri = URI.parse url
92
- http = Net::HTTP.new uri.host, uri.port
93
- http.use_ssl = true
94
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
95
- if params.nil?
96
- request = Net::HTTP::Get.new uri.request_uri
97
- else
98
- # If sending params, then we want a post request for authentication.
99
- request = Net::HTTP::Post.new uri.request_uri
100
- request.add_field "Key", API::KEY['key']
101
- request.add_field "Sign", sign
102
- request.set_form_data params
103
- end
104
- response = http.request request
105
- response.body
106
- end
107
-
108
- def get_json(url, params = nil, sign = nil)
109
- result = get_https(url, params, sign)
110
- if not result.is_a? String or not result.valid_json?
111
- raise RuntimeError, "Server returned invalid data."
112
- end
113
- JSON.load result
114
- end
115
- end
116
- end
117
-
118
- class PublicAPI < API
119
- OPERATIONS = %w(fee ticker trades depth)
120
-
121
- class << self
122
-
123
- def get_pair_operation_json(pair, operation)
124
- raise ArgumentError if not API::CURRENCY_PAIRS.include? pair
125
- raise ArgumentError if not OPERATIONS.include? operation
126
- get_json "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}"
127
- end
128
-
129
- OPERATIONS.each do |operation|
130
- class_eval %{
131
- def get_pair_#{operation}_json(pair)
132
- get_pair_operation_json pair, "#{operation}"
133
- end
134
- }
135
-
136
- API::CURRENCY_PAIRS.each do |pair|
137
- class_eval %{
138
- def get_#{pair}_#{operation}_json
139
- get_pair_#{operation}_json "#{pair}"
140
- end
141
- }
142
- end
143
- end
144
- end
145
- end
146
-
147
- class PublicOperation
148
- attr_reader :json, :operation, :pair
149
-
150
- def initialize(operation, pair)
151
- @operation = operation
152
- @pair = pair
153
- load_json
154
- end
155
-
156
- def load_json
157
- @json = PublicAPI.get_pair_operation_json pair, operation
158
- end
159
- end
160
-
161
- class Fee < PublicOperation
162
- def initialize(pair)
163
- super 'fee', pair
164
- end
165
-
166
- def trade
167
- json["trade"]
168
- end
169
- end
170
-
171
- class Ticker < PublicOperation
172
- def initialize(pair)
173
- super 'ticker', pair
174
- end
175
-
176
- JSON_METHODS = %w(high low avg vol vol_cur last buy sell server_time)
177
-
178
- JSON_METHODS.each do |method|
179
- class_eval %{
180
- def #{method}
181
- json["ticker"]["#{method}"] if json["ticker"] and json["ticker"].is_a? Hash
182
- end
183
- }
184
- end
185
-
186
- alias_method :bid, :buy
187
- alias_method :offer, :sell
188
- alias_method :ask, :sell
189
- alias_method :average, :avg
190
- alias_method :volume, :vol
191
- alias_method :volume_current, :vol_cur
192
-
193
- def spread
194
- (offer - bid) / offer
195
- end
196
-
197
- def spread_percent
198
- spread * 100.0
199
- end
200
- end
201
-
202
- class Trade
203
- attr_accessor :json
204
-
205
- JSON_METHODS = %w(date price amount tid price_currency item trade_type)
206
-
207
- attr_accessor *JSON_METHODS.map(&:to_sym)
208
-
209
- class << self
210
- def new_from_json(json)
211
- result = Trade.new
212
- result.json = json
213
- if json.is_a? Hash
214
- JSON_METHODS.each do |method|
215
- instance_eval %{
216
- result.#{method} = json["#{method}"]
217
- }
218
- end
219
- end
220
- result
221
- end
222
- end
223
- end
224
-
225
- class Trades < PublicOperation
226
- attr_reader :all
227
-
228
- def initialize(pair)
229
- super 'trades', pair
230
- load_trades
231
- end
232
-
233
- def load_trades
234
- @all = json.map {|trade| Trade.new_from_json trade} if json.is_a? Array
235
- end
236
- private :load_trades
237
-
238
- def [] *rest
239
- all[*rest]
240
- end
241
- end
242
-
243
- class Depth < PublicOperation
244
- def initialize(pair)
245
- super 'depth', pair
246
- end
247
- end
248
-
249
- class TradeAPI < API
250
- OPERATIONS = %w(getInfo
251
- TransHistory
252
- TradeHistory
253
- OrderList
254
- Trade
255
- CancelOrder)
256
-
257
- class << self
258
- def sign(params)
259
- data = params.collect do |key, value|
260
- "#{key}=#{CGI::escape(value.to_s)}"
261
- end.join('&')
262
-
263
- # Sign data with the HMAC SHA-512 algorhythm
264
- # Read https://btc-e.com/defines/documentation
265
- OpenSSL::HMAC.hexdigest('sha512', API::KEY['secret'], data)
266
- end
267
-
268
- def trade_api_call(method, extra, retries = 4)
269
- params = {"method" => method, "nonce" => nonce}
270
- if ! extra.empty?
271
- extra.each do |k,v|
272
- params[k.to_s] = v
273
- end
274
- end
275
- signed = sign params
276
- response = get_json "https://#{API::BTCE_DOMAIN}/tapi", params, signed
277
- if response.is_a?(Hash) && response["success"] == 0 && response["error"][/invalid nonce parameter/]
278
- sleep 1
279
- return retries > 0 ? trade_api_call(method, extra, retries - 1) : response
280
- else
281
- return response
282
- end
283
- end
284
-
285
- def nonce
286
- File.open("btce-nonce", "a+") do |f|
287
- f.flock(File::LOCK_EX)
288
- @nonce = f.read.to_i
289
- @nonce += 1
290
- f.truncate(0)
291
- f.puts(@nonce)
292
- end
293
- return Time::now.to_i + @nonce
294
- end
295
- private :nonce
296
-
297
- OPERATIONS.each do |operation|
298
- class_eval %{
299
- def #{operation.camelcase_to_snakecase} extra={}
300
- trade_api_call "#{operation}", extra
301
- end
302
- }
303
- end
304
- end
305
- end
306
47
  end
data/lib/btce/api.rb ADDED
@@ -0,0 +1,111 @@
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
+ ppc_usd
53
+ trc_btc
54
+ usd_rur
55
+ xpm_btc)
56
+ CURRENCIES = CURRENCY_PAIRS.map {|pair| pair.split("_")}.flatten.uniq.sort
57
+ MAX_DIGITS = {
58
+ "btc_usd" => 3,
59
+ "btc_eur" => 3,
60
+ "btc_rur" => 4,
61
+ "eur_usd" => 4,
62
+ "ftc_btc" => 4,
63
+ "ltc_btc" => 5,
64
+ "ltc_eur" => 6,
65
+ "ltc_rur" => 4,
66
+ "ltc_usd" => 6,
67
+ "nmc_btc" => 4,
68
+ "nmc_usd" => 6,
69
+ "nvc_btc" => 4,
70
+ "nvc_usd" => 4,
71
+ "ppc_btc" => 4,
72
+ "ppc_usd" => 4,
73
+ "trc_btc" => 6,
74
+ "usd_rur" => 4,
75
+ "xpm_btc" => 6
76
+ }
77
+
78
+ class << self
79
+ def get_https(opts={})
80
+ raise ArgumentError if not opts[:url].is_a? String
81
+ uri = URI.parse opts[:url]
82
+ http = Net::HTTP.new uri.host, uri.port
83
+ http.use_ssl = true
84
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
85
+ if opts[:params].nil?
86
+ request = Net::HTTP::Get.new uri.request_uri
87
+ else
88
+ # If sending params, then we want a post request for authentication.
89
+ request = Net::HTTP::Post.new uri.request_uri
90
+ request.add_field "Key", opts[:key]
91
+ request.add_field "Sign", opts[:signed]
92
+ request.set_form_data opts[:params]
93
+ end
94
+ response = http.request request
95
+ response.body
96
+ end
97
+
98
+ def get_json(opts={})
99
+ result = get_https(opts)
100
+ if not result.is_a? String or not result.valid_json?
101
+ raise RuntimeError, "Server returned invalid data."
102
+ end
103
+ JSON.load result
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ require 'btce/api/public_api'
110
+ require 'btce/api/trade_api'
111
+ 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[pair]
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,70 @@
1
+ # Copyright (c) 2013-2014, 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 updated)
42
+
43
+ JSON_METHODS.each do |method|
44
+ class_eval %{
45
+ def #{method}
46
+ json[@pair]["#{method}"] if json[@pair] and json[@pair].is_a? Hash
47
+ end
48
+ }
49
+ end
50
+
51
+ alias_method :bid, :buy
52
+ alias_method :offer, :sell
53
+
54
+ alias_method :ask, :sell
55
+ alias_method :average, :avg
56
+
57
+ alias_method :server_time, :updated
58
+
59
+ alias_method :volume, :vol
60
+ alias_method :volume_current, :vol_cur
61
+
62
+ def spread
63
+ (offer - bid) / offer
64
+ end
65
+
66
+ def spread_percent
67
+ spread * 100.0
68
+ end
69
+ end
70
+ 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,68 @@
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
+ list = pair.split('-')
42
+ i = 0
43
+ begin
44
+ raise ArgumentError if not API::CURRENCY_PAIRS.include? list[i]
45
+ i = i + 1
46
+ end while i < list.length
47
+ raise ArgumentError if not OPERATIONS.include? operation
48
+ get_json({ :url => "https://#{API::BTCE_DOMAIN}/api/3/#{operation}/#{pair}" })
49
+ end
50
+
51
+ OPERATIONS.each do |operation|
52
+ class_eval %{
53
+ def get_pair_#{operation}_json(pair)
54
+ get_pair_operation_json pair, "#{operation}"
55
+ end
56
+ }
57
+
58
+ API::CURRENCY_PAIRS.each do |pair|
59
+ class_eval %{
60
+ def get_#{pair}_#{operation}_json
61
+ get_pair_#{operation}_json "#{pair}"
62
+ end
63
+ }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,104 @@
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
+ File.open("btce-nonce", "a+") do |f|
86
+ f.flock(File::LOCK_EX)
87
+ @nonce = f.read.to_i
88
+ @nonce += 1
89
+ f.truncate(0)
90
+ f.puts(@nonce)
91
+ end
92
+ return Time::now.to_i + @nonce
93
+ end
94
+ private :nonce
95
+
96
+ OPERATIONS.each do |operation|
97
+ class_eval %{
98
+ def #{operation.camelcase_to_snakecase} extra={}
99
+ trade_api_call "#{operation}", extra
100
+ end
101
+ }
102
+ end
103
+ end
104
+ 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,63 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: andrewroth-btce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9.4
5
- prerelease:
4
+ version: 0.3.0.1
6
5
  platform: ruby
7
6
  authors:
7
+ - Christoph Bünte
8
+ - Davide Di Cillo
9
+ - Edward Funger
8
10
  - Christopher Mark Gore
9
- - Andrew Roth
11
+ - Stephan Kaag
12
+ - Sami Laine
13
+ - Selvam Palanimalai
14
+ - Jaime Quint
15
+ - Michaël Witrant
10
16
  autorequire:
11
17
  bindir: bin
12
18
  cert_chain: []
13
- date: 2013-07-26 00:00:00.000000000 Z
19
+ date: 2014-01-04 00:00:00.000000000 Z
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: monkey-patch
17
23
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
24
  requirements:
20
- - - ! '>='
25
+ - - ">="
21
26
  - !ruby/object:Gem::Version
22
27
  version: '0'
23
28
  type: :runtime
24
29
  prerelease: false
25
30
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
31
  requirements:
28
- - - ! '>='
32
+ - - ">="
29
33
  - !ruby/object:Gem::Version
30
34
  version: '0'
31
35
  description: A simple library to interface with the API for btc-e.com in Ruby.
32
- email: andrewroth@gmail.com
36
+ email: cgore@cgore.com
33
37
  executables: []
34
38
  extensions: []
35
39
  extra_rdoc_files: []
36
40
  files:
37
41
  - lib/btce.rb
42
+ - lib/btce/api.rb
43
+ - lib/btce/api/operations.rb
44
+ - lib/btce/api/operations/depth.rb
45
+ - lib/btce/api/operations/fee.rb
46
+ - lib/btce/api/operations/public_operation.rb
47
+ - lib/btce/api/operations/ticker.rb
48
+ - lib/btce/api/operations/trades.rb
49
+ - lib/btce/api/public_api.rb
50
+ - lib/btce/api/trade_api.rb
51
+ - lib/btce/trade.rb
38
52
  homepage: https://github.com/cgore/ruby-btce
39
53
  licenses: []
54
+ metadata: {}
40
55
  post_install_message:
41
56
  rdoc_options: []
42
57
  require_paths:
43
58
  - lib
44
59
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
60
  requirements:
47
- - - ! '>='
61
+ - - ">="
48
62
  - !ruby/object:Gem::Version
49
63
  version: '0'
50
64
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
65
  requirements:
53
- - - ! '>='
66
+ - - ">="
54
67
  - !ruby/object:Gem::Version
55
68
  version: '0'
56
69
  requirements: []
57
70
  rubyforge_project:
58
- rubygems_version: 1.8.25
71
+ rubygems_version: 2.2.0
59
72
  signing_key:
60
- specification_version: 3
73
+ specification_version: 4
61
74
  summary: A simple library to interface with the API for btc-e.com in Ruby.
62
75
  test_files: []
63
- has_rdoc: