btce 0.1.11 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/btce.rb +57 -43
  2. metadata +2 -2
data/lib/btce.rb CHANGED
@@ -75,30 +75,29 @@ module Btce
75
75
  "ppc_btc" => 4,
76
76
  "ftc_btc" => 4
77
77
  }
78
- KEY = YAML::load File.open 'btce-api-key.yml'
79
78
 
80
79
  class << self
81
- def get_https(url, params = nil, sign = nil)
82
- raise ArgumentError if not url.is_a? String
83
- uri = URI.parse url
80
+ def get_https(opts={})
81
+ raise ArgumentError if not opts[:url].is_a? String
82
+ uri = URI.parse opts[:url]
84
83
  http = Net::HTTP.new uri.host, uri.port
85
84
  http.use_ssl = true
86
85
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
- if params.nil?
86
+ if opts[:params].nil?
88
87
  request = Net::HTTP::Get.new uri.request_uri
89
88
  else
90
89
  # If sending params, then we want a post request for authentication.
91
90
  request = Net::HTTP::Post.new uri.request_uri
92
- request.add_field "Key", API::KEY['key']
93
- request.add_field "Sign", sign
94
- request.set_form_data params
91
+ request.add_field "Key", opts[:key]
92
+ request.add_field "Sign", opts[:signed]
93
+ request.set_form_data opts[:params]
95
94
  end
96
95
  response = http.request request
97
96
  response.body
98
97
  end
99
98
 
100
- def get_json(url, params = nil, sign = nil)
101
- result = get_https(url, params, sign)
99
+ def get_json(opts={})
100
+ result = get_https(opts)
102
101
  if not result.is_a? String or not result.valid_json?
103
102
  raise RuntimeError, "Server returned invalid data."
104
103
  end
@@ -111,11 +110,10 @@ module Btce
111
110
  OPERATIONS = %w(fee ticker trades depth)
112
111
 
113
112
  class << self
114
-
115
113
  def get_pair_operation_json(pair, operation)
116
114
  raise ArgumentError if not API::CURRENCY_PAIRS.include? pair
117
115
  raise ArgumentError if not OPERATIONS.include? operation
118
- get_json "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}"
116
+ get_json({ :url => "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}" })
119
117
  end
120
118
 
121
119
  OPERATIONS.each do |operation|
@@ -239,6 +237,16 @@ module Btce
239
237
  end
240
238
 
241
239
  class TradeAPI < API
240
+ if File.exists? 'btce-api-key.yml'
241
+ KEY = YAML::load File.open 'btce-api-key.yml'
242
+
243
+ class << self
244
+ def new_from_keyfile
245
+ new key: KEY["key"], secret: KEY["secret"]
246
+ end
247
+ end
248
+ end
249
+
242
250
  OPERATIONS = %w(getInfo
243
251
  TransHistory
244
252
  TradeHistory
@@ -246,43 +254,49 @@ module Btce
246
254
  Trade
247
255
  CancelOrder)
248
256
 
249
- class << self
250
- def sign(params)
251
- # The digest needs to be created.
252
- hmac = OpenSSL::HMAC.new(API::KEY['secret'],
253
- OpenSSL::Digest::SHA512.new)
254
- params = params
255
- .collect {|k,v| "#{k}=#{v}"}
256
- .join('&')
257
- signed = hmac.update params
258
- end
259
257
 
260
- def trade_api_call(method, extra)
261
- params = {"method" => method, "nonce" => nonce}
262
- if ! extra.empty?
263
- extra.each do |k,v|
264
- params[k.to_s] = v
265
- end
266
- end
267
- signed = sign params
268
- get_json "https://#{API::BTCE_DOMAIN}/tapi", params, signed
269
- end
258
+ def initialize(opts={})
259
+ raise ArgumentError unless opts.has_key?(:key) and opts.has_key?(:secret)
260
+ @key = opts[:key]
261
+ @secret = opts[:secret]
262
+ end
270
263
 
271
- def nonce
272
- while result = Time.now.to_i and @last_nonce and @last_nonce >= result
273
- sleep 1
264
+ def sign(params)
265
+ # The digest needs to be created.
266
+ hmac = OpenSSL::HMAC.new(@secret,
267
+ OpenSSL::Digest::SHA512.new)
268
+ params = params.collect {|k,v| "#{k}=#{v}"}.join('&')
269
+ signed = hmac.update params
270
+ end
271
+
272
+ def trade_api_call(method, extra)
273
+ params = {"method" => method, "nonce" => nonce}
274
+ if ! extra.empty?
275
+ extra.each do |k,v|
276
+ params[k.to_s] = v
274
277
  end
275
- return @last_nonce = result
276
278
  end
277
- private :nonce
279
+ signed = sign params
280
+ API::get_json({ :url => "https://#{API::BTCE_DOMAIN}/tapi",
281
+ :key => @key,
282
+ :params => params,
283
+ :signed => signed })
284
+ end
278
285
 
279
- OPERATIONS.each do |operation|
280
- class_eval %{
281
- def #{operation.camelcase_to_snakecase} extra={}
282
- trade_api_call "#{operation}", extra
283
- end
284
- }
286
+ def nonce
287
+ while result = Time.now.to_i and @last_nonce and @last_nonce >= result
288
+ sleep 1
285
289
  end
290
+ return @last_nonce = result
291
+ end
292
+ private :nonce
293
+
294
+ OPERATIONS.each do |operation|
295
+ class_eval %{
296
+ def #{operation.camelcase_to_snakecase} extra={}
297
+ trade_api_call "#{operation}", extra
298
+ end
299
+ }
286
300
  end
287
301
  end
288
302
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: btce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-11-21 00:00:00.000000000 Z
15
+ date: 2013-11-27 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: monkey-patch