crypto_ticker 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +11 -0
- data/README.md +1 -1
- data/lib/crypto_ticker.rb +189 -99
- data/lib/crypto_ticker/version.rb +1 -1
- data/test/test_crypto_ticker.rb +39 -0
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dad7e958ea367a57323e38355211c4d97a339a5a
|
4
|
+
data.tar.gz: 325d56318673557d4d897f1be24f38caaf883cb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2881c6068641101bdd60c3a62811250686c464b8674e33749d3ab6d1bbe3071d0a3c7949bc1c5fc670712eae0673d80cbe0007a925e65303b43037051a2c72dd
|
7
|
+
data.tar.gz: 11c1ecc13d93e0418919630c5597c00cd55b54488aecf537294a4b61a4193a4777c0c41491991f57df0cc65f64bc8bc26e9684dba9576c56720c3a5e9bf7e89a
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
data/lib/crypto_ticker.rb
CHANGED
@@ -21,17 +21,6 @@ module CryptoTicker
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
def self.makepair(base, quote='')
|
25
|
-
if base =~ /([a-z]{3})\/([a-z]{3})/i
|
26
|
-
base, quote = $1, $2
|
27
|
-
end
|
28
|
-
# assume user entered either a fxpair array or a fxpair string
|
29
|
-
# TODO: error-checking on input
|
30
|
-
pair = FXPair.new("#{base.upcase}/#{quote.upcase}")
|
31
|
-
pair
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
24
|
##
|
36
25
|
# MtGox BTC/USD ticker and retrieval method
|
37
26
|
#
|
@@ -42,53 +31,59 @@ module CryptoTicker
|
|
42
31
|
class MtGox
|
43
32
|
@@valid_pairs = %w[ BTC/USD LTC/USD NMC/USD ]
|
44
33
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
34
|
+
class << self
|
35
|
+
|
36
|
+
# return a ticker URL for a given crypto FX pair
|
37
|
+
def ticker(base, quote='')
|
38
|
+
pair = CryptoTicker::makepair(base, quote)
|
39
|
+
if @@valid_pairs.include?( pair )
|
40
|
+
"http://data.mtgox.com/api/2/" + pair.split('/').join('') +
|
41
|
+
"/money/ticker"
|
42
|
+
end
|
51
43
|
end
|
52
|
-
end
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
45
|
+
def trades(base, quote='')
|
46
|
+
pair = CryptoTicker::makepair(base, quote)
|
47
|
+
if @@valid_pairs.include?( pair )
|
48
|
+
"http://data.mtgox.com/api/2/" + pair.split('/').join('') +
|
49
|
+
"/money/trades/fetch"
|
50
|
+
end
|
59
51
|
end
|
60
|
-
end
|
61
52
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
53
|
+
def depth(base, quote='')
|
54
|
+
pair = CryptoTicker::makepair(base, quote)
|
55
|
+
if @@valid_pairs.include?( pair )
|
56
|
+
"http://data.mtgox.com/api/2/" + pair.split('/').join('') +
|
57
|
+
"/money/depth/fetch"
|
58
|
+
end
|
67
59
|
end
|
68
|
-
end
|
69
60
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
61
|
+
# Accepts JSON retrieved from the MtGox ticker URL, returns last trade
|
62
|
+
# amount (denominated in counter currency) as a BigDecimal.
|
63
|
+
# eg: BTC/USD ticker will return amount in USD
|
64
|
+
def last(json)
|
65
|
+
hash = JSON.parse(json)
|
66
|
+
if hash['result'] === 'success'
|
67
|
+
hash['data']['last']['value'].to_d
|
68
|
+
end
|
77
69
|
end
|
78
|
-
end
|
79
70
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
71
|
+
def info(json)
|
72
|
+
hash = JSON.parse(json)
|
73
|
+
info = {}
|
74
|
+
if hash['result'] === 'success'
|
75
|
+
hash['data'].each do |k,v|
|
76
|
+
if v.class.to_s.eql?( 'Hash' )
|
77
|
+
info[k.to_sym] = v['value'].to_d
|
78
|
+
end
|
87
79
|
end
|
88
80
|
end
|
81
|
+
|
82
|
+
# return either nil or the hash w/data
|
83
|
+
info.empty? ? nil : info
|
89
84
|
end
|
90
|
-
info
|
91
85
|
end
|
86
|
+
|
92
87
|
end
|
93
88
|
|
94
89
|
|
@@ -99,35 +94,78 @@ module CryptoTicker
|
|
99
94
|
@@valid_pairs = %w[ BTC/USD BTC/RUR BTC/EUR LTC/BTC LTC/USD LTC/RUR NMC/BTC
|
100
95
|
USD/RUR EUR/USD NVC/BTC TRC/BTC PPC/BTC RUC/BTC ]
|
101
96
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
97
|
+
@@ret_types = {
|
98
|
+
'date' => :integer,
|
99
|
+
'price' => :bigdecimal,
|
100
|
+
'amount' => :bigdecimal,
|
101
|
+
'tid' => :integer,
|
102
|
+
'item' => :string,
|
103
|
+
'trade_type' => :string,
|
104
|
+
'price_currency' => :string,
|
105
|
+
'high' => :bigdecimal,
|
106
|
+
'low' => :bigdecimal,
|
107
|
+
'sell' => :bigdecimal,
|
108
|
+
'buy' => :bigdecimal,
|
109
|
+
'last' => :bigdecimal,
|
110
|
+
'vol_cur' => :bigdecimal,
|
111
|
+
'vol' => :bigdecimal,
|
112
|
+
'avg' => :bigdecimal,
|
113
|
+
'server_time' => :integer,
|
114
|
+
}
|
115
|
+
|
116
|
+
@@ret_fcns = {
|
117
|
+
:integer => 'to_i',
|
118
|
+
:bigdecimal => 'to_d',
|
119
|
+
:string => 'to_s',
|
120
|
+
}
|
121
|
+
|
122
|
+
class << self
|
123
|
+
|
124
|
+
def ticker(base, quote='')
|
125
|
+
pair = CryptoTicker::makepair(base, quote)
|
126
|
+
if @@valid_pairs.include?( pair )
|
127
|
+
"https://btc-e.com/api/2/#{pair.to_sym}/ticker"
|
128
|
+
end
|
106
129
|
end
|
107
|
-
end
|
108
130
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
131
|
+
def trades(base, quote='')
|
132
|
+
pair = CryptoTicker::makepair(base, quote)
|
133
|
+
if @@valid_pairs.include?( pair )
|
134
|
+
"https://btc-e.com/api/2/#{pair.to_sym}/trades"
|
135
|
+
end
|
113
136
|
end
|
114
|
-
end
|
115
137
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
138
|
+
def depth(base, quote='')
|
139
|
+
pair = CryptoTicker::makepair(base, quote)
|
140
|
+
if @@valid_pairs.include?( pair )
|
141
|
+
"https://btc-e.com/api/2/#{pair.to_sym}/depth"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Accepts JSON retrieved from the appropriate BTC-e ticker URL, returns
|
146
|
+
# last trade amount (denominated in counter currency) as a float.
|
147
|
+
# eg: BTC/USD ticker will return amount in USD
|
148
|
+
# NMC/BTC ticker will return amount in BTC
|
149
|
+
def last(json)
|
150
|
+
hash = JSON.parse(json)
|
151
|
+
hash['ticker']['last'].to_d
|
120
152
|
end
|
121
|
-
end
|
122
153
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
154
|
+
|
155
|
+
def info(json)
|
156
|
+
hash = JSON.parse(json)
|
157
|
+
info = {}
|
158
|
+
# TODO: recursively process Arrays, Hashes, Strings, Fixnums...
|
159
|
+
if hash.has_key?('ticker')
|
160
|
+
hash['ticker'].keys.each do |key|
|
161
|
+
val = hash['ticker'][key]
|
162
|
+
info[key.to_sym] = val.send(@@ret_fcns[@@ret_types[key]])
|
163
|
+
end
|
164
|
+
end
|
165
|
+
info
|
166
|
+
end
|
130
167
|
end
|
168
|
+
|
131
169
|
end
|
132
170
|
|
133
171
|
|
@@ -135,55 +173,107 @@ module CryptoTicker
|
|
135
173
|
# Vircurex ticker API and retrieval method
|
136
174
|
class Vircurex
|
137
175
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
176
|
+
class << self
|
177
|
+
# accept [base, quote] args for consistency with other classes, but ignore
|
178
|
+
# them
|
179
|
+
def ticker(base='', quote='')
|
180
|
+
# this one gets everything...
|
181
|
+
'https://vircurex.com/api/get_info_for_currency.json'
|
182
|
+
end
|
144
183
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
184
|
+
# Accepts JSON retrieved from the Vircurex ticker URL, as well as a
|
185
|
+
# currency pair (specified as 2 arguments, a base currency and a quote
|
186
|
+
# currency). Returns last trade amount in quote currency.
|
187
|
+
def getpair(json, base, quote)
|
188
|
+
hash = JSON.parse(json)
|
150
189
|
|
151
|
-
|
152
|
-
|
153
|
-
|
190
|
+
# upcase currency pair inputs
|
191
|
+
base.upcase!
|
192
|
+
quote.upcase!
|
193
|
+
|
194
|
+
# default value (may change this to just throw an exception)
|
195
|
+
last = 0.0
|
196
|
+
|
197
|
+
# if currency pair exists, return value of last trade
|
198
|
+
if hash.has_key?(base) && hash[base].has_key?( quote ) &&
|
199
|
+
hash[base][quote].has_key?('last_trade')
|
200
|
+
last = hash[base][quote]['last_trade'].to_d
|
201
|
+
end
|
154
202
|
|
155
|
-
|
156
|
-
|
203
|
+
last
|
204
|
+
end
|
205
|
+
|
206
|
+
# Accepts JSON retrieved from the Vircurex ticker URL, as well as a
|
207
|
+
# currency pair (specified as 2 arguments, a base currency and a quote
|
208
|
+
# currency). Returns last trade amount in quote currency.
|
209
|
+
def pair_info(json, base, quote)
|
210
|
+
hash = JSON.parse(json)
|
211
|
+
|
212
|
+
# upcase currency pair inputs
|
213
|
+
base.upcase!
|
214
|
+
quote.upcase!
|
215
|
+
|
216
|
+
# default value (may change this to just throw an exception)
|
217
|
+
info = {}
|
218
|
+
|
219
|
+
# if currency pair exists, return value of last trade
|
220
|
+
if hash.has_key?(base) && hash[base].has_key?( quote )
|
221
|
+
info = hash[base][quote]
|
222
|
+
end
|
157
223
|
|
158
|
-
|
159
|
-
if hash.has_key?(base) && hash[base].has_key?( quote ) &&
|
160
|
-
hash[base][quote].has_key?('last_trade')
|
161
|
-
last = hash[base][quote]['last_trade'].to_d
|
224
|
+
info
|
162
225
|
end
|
163
226
|
|
164
|
-
last
|
165
227
|
end
|
228
|
+
|
166
229
|
end
|
167
230
|
|
168
231
|
|
169
232
|
class Bitstamp
|
170
233
|
# this exchange only has BTC/USD
|
171
|
-
|
172
|
-
|
173
|
-
|
234
|
+
class << self
|
235
|
+
def ticker(base='BTC', quote='USD')
|
236
|
+
'https://www.bitstamp.net/api/ticker/'
|
237
|
+
end
|
174
238
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
239
|
+
def info(json)
|
240
|
+
info = {}
|
241
|
+
JSON.parse(json).each do |k,v|
|
242
|
+
info[k.to_sym] = v.to_d
|
243
|
+
end
|
244
|
+
info
|
179
245
|
end
|
180
|
-
|
246
|
+
|
247
|
+
def last(json)
|
248
|
+
self.info(json)[:last]
|
249
|
+
end
|
250
|
+
|
181
251
|
end
|
182
252
|
|
183
|
-
|
184
|
-
|
253
|
+
end
|
254
|
+
|
255
|
+
class << self
|
256
|
+
@@h = {
|
257
|
+
'btce' => CryptoTicker::BTCe,
|
258
|
+
'mtgox' => CryptoTicker::MtGox,
|
259
|
+
'vircurex' => CryptoTicker::Vircurex,
|
260
|
+
'bitstamp' => CryptoTicker::Bitstamp,
|
261
|
+
}
|
262
|
+
def get_class_for(exchange_name)
|
263
|
+
@@h[ exchange_name.gsub('-', '').downcase ]
|
264
|
+
end
|
265
|
+
|
266
|
+
def makepair(base, quote='')
|
267
|
+
if base =~ /([a-z]{3})\/([a-z]{3})/i
|
268
|
+
base, quote = $1, $2
|
269
|
+
end
|
270
|
+
# assume user entered either a fxpair array or a fxpair string
|
271
|
+
# TODO: error-checking on input
|
272
|
+
pair = FXPair.new("#{base.upcase}/#{quote.upcase}")
|
273
|
+
pair
|
185
274
|
end
|
186
275
|
|
187
276
|
end
|
277
|
+
|
188
278
|
end
|
189
279
|
|
data/test/test_crypto_ticker.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'minitest_helper'
|
2
2
|
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'pp'
|
3
5
|
|
4
6
|
class TestCryptoTicker < MiniTest::Unit::TestCase
|
5
7
|
def test_that_it_has_a_version_number
|
@@ -54,5 +56,42 @@ class TestCryptoTicker < MiniTest::Unit::TestCase
|
|
54
56
|
assert CryptoTicker::Bitstamp.ticker =~ URI::regexp
|
55
57
|
end
|
56
58
|
|
59
|
+
def test_btce_parser
|
60
|
+
data = '{"ticker":{"high":99,"low":81,"avg":90,"vol":1857509.30091,"vol_cur":20229.18878,"last":84.5,"buy":84.873,"sell":84.01,"server_time":1366060728}}'
|
61
|
+
info = CryptoTicker::BTCe.info(data)
|
62
|
+
|
63
|
+
assert info.has_key?( :high )
|
64
|
+
assert info.has_key?( :low )
|
65
|
+
assert info.has_key?( :last )
|
66
|
+
|
67
|
+
assert_equal BigDecimal, info.fetch(:high).class
|
68
|
+
assert_equal BigDecimal, info.fetch(:low ).class
|
69
|
+
assert_equal BigDecimal, info.fetch(:last).class
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_get_class_for
|
73
|
+
klass = CryptoTicker::get_class_for('BTC-e')
|
74
|
+
assert_equal CryptoTicker::BTCe, klass
|
75
|
+
|
76
|
+
klass = CryptoTicker::get_class_for('BTCe')
|
77
|
+
assert_equal CryptoTicker::BTCe, klass
|
78
|
+
|
79
|
+
klass = CryptoTicker::get_class_for('btc-e')
|
80
|
+
assert_equal CryptoTicker::BTCe, klass
|
81
|
+
|
82
|
+
klass = CryptoTicker::get_class_for('MtGox')
|
83
|
+
assert_equal CryptoTicker::MtGox, klass
|
84
|
+
|
85
|
+
klass = CryptoTicker::get_class_for('mtgox')
|
86
|
+
assert_equal CryptoTicker::MtGox, klass
|
87
|
+
|
88
|
+
klass = CryptoTicker::get_class_for('Vircurex')
|
89
|
+
assert_equal CryptoTicker::Vircurex, klass
|
90
|
+
|
91
|
+
klass = CryptoTicker::get_class_for('vircurex')
|
92
|
+
assert_equal CryptoTicker::Vircurex, klass
|
93
|
+
end
|
94
|
+
|
57
95
|
end
|
58
96
|
|
97
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto_ticker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Marley
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
p2W7hfexY6Z0vvNPnKZi47PDm0TlB0AbqGorUQFkwwrll4XglFRJGiLooFXN08sC
|
31
31
|
6eRTq9IlmYPfHWtlTQo3d1rgg2Bx0ukp9+SbN81z+I7IbqE+
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2013-04-
|
33
|
+
date: 2013-04-21 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: bundler
|
@@ -84,6 +84,7 @@ extensions: []
|
|
84
84
|
extra_rdoc_files: []
|
85
85
|
files:
|
86
86
|
- .gitignore
|
87
|
+
- CHANGELOG.md
|
87
88
|
- Gemfile
|
88
89
|
- LICENSE.md
|
89
90
|
- README.md
|
metadata.gz.sig
CHANGED
Binary file
|