crypto_ticker 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 281ca2a5ea59ae06a17409cdbbd40858f9880093
4
- data.tar.gz: 58e90d3eda746d158c436c7aff9950ba8e1732d0
3
+ metadata.gz: dad7e958ea367a57323e38355211c4d97a339a5a
4
+ data.tar.gz: 325d56318673557d4d897f1be24f38caaf883cb3
5
5
  SHA512:
6
- metadata.gz: e45195fbcc743a5e1d4cea02e6b183b57d3e03d40c9de19235f63c1b5b6375be9585f19f77b28b04cf6d1a80538869ad4593273914c32be51ae5ff6f5573602e
7
- data.tar.gz: 7019ffb86b702820610bdc534d821692ba03450d3efc77700ee716bd3bca200f3a65f9b7e5a89604330dade3f3a2ca72164f1f2abc87a7d2dbff6c69a545c999
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
@@ -0,0 +1,11 @@
1
+ ## 0.0.3 (2013-04-21)
2
+
3
+ Bugfixes:
4
+
5
+ - fix MtGox ticker url
6
+
7
+ Features:
8
+
9
+ - return data values in appropriate format for BTCe.info (BigDecimal, Fixnum, String, etc)
10
+ - added CryptoTicker::get_class_for() method
11
+
data/README.md CHANGED
@@ -32,7 +32,7 @@ Usage
32
32
  url = CryptoTicker::MtGox.ticker('BTC/USD')
33
33
  json = agent.get( url ).body
34
34
 
35
- mtgox_data_hash = CryptoCurrency::MtGox.info( json )
35
+ mtgox_data_hash = CryptoTicker::MtGox.info( json )
36
36
  pp mtgox_data_hash
37
37
 
38
38
  Contributing
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
- # return a ticker URL for a given crypto FX pair
46
- def self.ticker(base, quote='')
47
- pair = CryptoTicker::makepair(base, quote)
48
- if @@valid_pairs.include?( pair )
49
- "http://data.mtgox.com/api/2/" + pair.split('/').join('') +
50
- "/money/ticker_fast"
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
- def self.trades(base, quote='')
55
- pair = CryptoTicker::makepair(base, quote)
56
- if @@valid_pairs.include?( pair )
57
- "http://data.mtgox.com/api/2/" + pair.split('/').join('') +
58
- "/money/trades/fetch"
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
- def self.depth(base, quote='')
63
- pair = CryptoTicker::makepair(base, quote)
64
- if @@valid_pairs.include?( pair )
65
- "http://data.mtgox.com/api/2/" + pair.split('/').join('') +
66
- "/money/depth/fetch"
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
- # Accepts JSON retrieved from the MtGox ticker URL, returns last trade
71
- # amount (denominated in counter currency) as a BigDecimal.
72
- # eg: BTC/USD ticker will return amount in USD
73
- def self.last(json)
74
- hash = JSON.parse(json)
75
- if hash['result'] === 'success'
76
- hash['data']['last']['value'].to_d
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
- def self.info(json)
81
- hash = JSON.parse(json)
82
- info = {}
83
- if hash['result'] === 'success'
84
- hash['data'].each do |k,v|
85
- if v.class.to_s.eql?( 'Hash' )
86
- info[k.to_sym] = v['value'].to_d
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
- def self.ticker(base, quote='')
103
- pair = CryptoTicker::makepair(base, quote)
104
- if @@valid_pairs.include?( pair )
105
- "https://btc-e.com/api/2/#{pair.to_sym}/ticker"
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
- def self.trades(base, quote='')
110
- pair = CryptoTicker::makepair(base, quote)
111
- if @@valid_pairs.include?( pair )
112
- "https://btc-e.com/api/2/#{pair.to_sym}/trades"
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
- def self.depth(base, quote='')
117
- pair = CryptoTicker::makepair(base, quote)
118
- if @@valid_pairs.include?( pair )
119
- "https://btc-e.com/api/2/#{pair.to_sym}/depth"
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
- # Accepts JSON retrieved from the appropriate BTC-e ticker URL, returns
124
- # last trade amount (denominated in counter currency) as a float.
125
- # eg: BTC/USD ticker will return amount in USD
126
- # NMC/BTC ticker will return amount in BTC
127
- def self.last(json)
128
- hash = JSON.parse(json)
129
- hash['ticker']['last'].to_d
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
- # accept [base, quote] args for consistency with other classes, but ignore
139
- # them
140
- def self.ticker(base='', quote='')
141
- # this one gets everything...
142
- 'https://vircurex.com/api/get_info_for_currency.json'
143
- end
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
- # Accepts JSON retrieved from the Vircurex ticker URL, as well as a
146
- # currency pair (specified as 2 arguments, a base currency and a quote
147
- # currency). Returns last trade amount in quote currency.
148
- def self.getpair(json, base, quote)
149
- hash = JSON.parse(json)
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
- # upcase currency pair inputs
152
- base.upcase!
153
- quote.upcase!
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
- # default value (may change this to just throw an exception)
156
- last = 0.0
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
- # if currency pair exists, return value of last trade
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
- def self.ticker(base='BTC', quote='USD')
172
- 'https://www.bitstamp.net/api/ticker/'
173
- end
234
+ class << self
235
+ def ticker(base='BTC', quote='USD')
236
+ 'https://www.bitstamp.net/api/ticker/'
237
+ end
174
238
 
175
- def self.info(json)
176
- info = {}
177
- JSON.parse(json).each do |k,v|
178
- info[k.to_sym] = v.to_d
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
- info
246
+
247
+ def last(json)
248
+ self.info(json)[:last]
249
+ end
250
+
181
251
  end
182
252
 
183
- def self.last(json)
184
- self.info(json)[:last]
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
 
@@ -1,3 +1,3 @@
1
1
  module CryptoTicker
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.1
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-12 00:00:00.000000000 Z
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