btce 0.0.1 → 0.0.2

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 +125 -22
  2. metadata +2 -2
@@ -47,6 +47,13 @@ class String
47
47
  .tr("-", "_")
48
48
  .downcase
49
49
  end
50
+
51
+ def valid_json?
52
+ JSON.parse self
53
+ return true
54
+ rescue JSON::ParserError
55
+ return false
56
+ end
50
57
  end
51
58
 
52
59
  module Btce
@@ -79,32 +86,34 @@ module Btce
79
86
  "eur_usd" => 4,
80
87
  "nvc_btc" => 4
81
88
  }
82
- API_KEY = YAML::load(File.open('btce-api-key.yml'))
83
-
89
+ KEY = YAML::load File.open 'btce-api-key.yml'
84
90
 
85
91
  class << self
86
- def get_https(url,params=nil,sign=nil)
92
+ def get_https(url, params = nil, sign = nil)
87
93
  raise ArgumentError if not url.is_a? String
88
94
  uri = URI.parse url
89
95
  http = Net::HTTP.new uri.host, uri.port
90
96
  http.use_ssl = true
91
97
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
92
- #if sending params then we want a post request(for authentication)
93
- if(params==nil)
98
+ if params.nil?
94
99
  request = Net::HTTP::Get.new uri.request_uri
95
100
  else
101
+ # If sending params, then we want a post request for authentication.
96
102
  request = Net::HTTP::Post.new uri.request_uri
97
- request.add_field("Key",API::API_KEY['key'])
98
- request.add_field("Sign",sign)
99
- request.set_form_data(params)
100
-
103
+ request.add_field "Key", API::KEY['key']
104
+ request.add_field "Sign", sign
105
+ request.set_form_data params
101
106
  end
102
107
  response = http.request request
103
108
  response.body
104
109
  end
105
110
 
106
- def get_json(url,params=nil,sign=nil)
107
- JSON.load get_https url, params, sign
111
+ def get_json(url, params = nil, sign = nil)
112
+ result = get_https(url, params, sign)
113
+ if not result.is_a? String or not result.valid_json?
114
+ raise RuntimeError, "Server returned invalid data."
115
+ end
116
+ JSON.load result
108
117
  end
109
118
  end
110
119
  end
@@ -138,33 +147,127 @@ module Btce
138
147
  end
139
148
  end
140
149
 
150
+ class PublicOperation
151
+ attr_reader :json, :operation, :pair
152
+
153
+ def initialize(operation, pair)
154
+ @operation = operation
155
+ @pair = pair
156
+ load_json
157
+ end
158
+
159
+ def load_json
160
+ @json = PublicAPI.get_pair_operation_json pair, operation
161
+ end
162
+ end
163
+
164
+ class Fee < PublicOperation
165
+ def initialize(pair)
166
+ super 'fee', pair
167
+ end
168
+
169
+ def trade
170
+ json["trade"]
171
+ end
172
+ end
173
+
174
+ class Ticker < PublicOperation
175
+ def initialize(pair)
176
+ super 'ticker', pair
177
+ end
178
+
179
+ JSON_METHODS = %w(high low avg vol vol_cur last buy sell server_time)
180
+
181
+ JSON_METHODS.each do |method|
182
+ class_eval %{
183
+ def #{method}
184
+ json["ticker"]["#{method}"] if json["ticker"] and json["ticker"].is_a? Hash
185
+ end
186
+ }
187
+ end
188
+ end
189
+
190
+ class Trade
191
+ attr_accessor :json
192
+
193
+ JSON_METHODS = %w(date price amount tid price_currency item trade_type)
194
+
195
+ attr_accessor *JSON_METHODS.map(&:to_sym)
196
+
197
+ class << self
198
+ def new_from_json(json)
199
+ result = Trade.new
200
+ result.json = json
201
+ if json.is_a? Hash
202
+ JSON_METHODS.each do |method|
203
+ instance_eval %{
204
+ result.#{method} = json["#{method}"]
205
+ }
206
+ end
207
+ end
208
+ result
209
+ end
210
+ end
211
+ end
212
+
213
+ class Trades < PublicOperation
214
+ attr_reader :all
215
+
216
+ def initialize(pair)
217
+ super 'trades', pair
218
+ load_trades
219
+ end
220
+
221
+ def load_trades
222
+ @all = json.map {|trade| Trade.new_from_json trade} if json.is_a? Array
223
+ end
224
+ private :load_trades
225
+
226
+ def [] *rest
227
+ all[*rest]
228
+ end
229
+ end
230
+
231
+ class Depth < PublicOperation
232
+ def initialize(pair)
233
+ super 'depth', pair
234
+ end
235
+ end
236
+
141
237
  class TradeAPI < API
142
- OPERATIONS = %w(getInfo TransHistory TradeHistory OrderList Trade CancelOrder)
238
+ OPERATIONS = %w(getInfo
239
+ TransHistory
240
+ TradeHistory
241
+ OrderList
242
+ Trade
243
+ CancelOrder)
143
244
 
144
245
  class << self
145
246
  def sign(params)
146
- #digest needs to be created
147
- hmac = OpenSSL::HMAC.new(API::API_KEY['secret'], OpenSSL::Digest::SHA512.new)
148
- params = params.collect{|k,v| "#{k}=#{v}"}.join('&')
149
- signed = hmac.update(params)
247
+ # The digest needs to be created.
248
+ hmac = OpenSSL::HMAC.new(API::KEY['secret'],
249
+ OpenSSL::Digest::SHA512.new)
250
+ params = params
251
+ .collect {|k,v| "#{k}=#{v}"}
252
+ .join('&')
253
+ signed = hmac.update params
150
254
  end
151
255
 
152
256
  def trade_api_call(method, extra)
153
- params = {"method"=>method, "nonce"=>nonce}
154
- if !extra.empty?
155
-
257
+ params = {"method" => method, "nonce" => nonce}
258
+ if ! extra.empty?
156
259
  extra.each do |a|
157
- params["#{a.to_s}"] = a
260
+ params[a.to_s] = a
158
261
  end
159
262
  end
160
- puts params
161
- signed = sign(params)
263
+ signed = sign params
162
264
  get_json "https://#{API::BTCE_DOMAIN}/tapi", params, signed
163
265
  end
164
266
 
165
267
  def nonce
166
268
  Time.now.to_i
167
269
  end
270
+ private :nonce
168
271
 
169
272
  OPERATIONS.each do |operation|
170
273
  class_eval %{
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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-06 00:00:00.000000000 Z
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple library to interface with the API for btc-e.com in Ruby.
15
15
  email: cgore@cgore.com