crypto_ticker 0.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: 281ca2a5ea59ae06a17409cdbbd40858f9880093
4
+ data.tar.gz: 58e90d3eda746d158c436c7aff9950ba8e1732d0
5
+ SHA512:
6
+ metadata.gz: e45195fbcc743a5e1d4cea02e6b183b57d3e03d40c9de19235f63c1b5b6375be9585f19f77b28b04cf6d1a80538869ad4593273914c32be51ae5ff6f5573602e
7
+ data.tar.gz: 7019ffb86b702820610bdc534d821692ba03450d3efc77700ee716bd3bca200f3a65f9b7e5a89604330dade3f3a2ca72164f1f2abc87a7d2dbff6c69a545c999
checksums.yaml.gz.sig ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crypto_ticker.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nathan Marley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+
2
+ CryptoTicker
3
+ ============
4
+
5
+ Collection of public data API urls for various online crypto-currency
6
+ exchanges, e.g. MtGox, BTC-e, etc.
7
+
8
+ I tried to follow the UNIX philosophy of "write programs which do one thing and
9
+ do it well", so this module doesn't include a user-agent module. It only
10
+ provides public data API URL's in one convenient location, and optionally,
11
+ parses the data returned from those URLs. You'll have to use this in
12
+ conjunction with 'mechanize' or 'net/http' or some other such module to
13
+ actually do anything (see [example](#example)).
14
+
15
+ Installation
16
+ ------------
17
+
18
+ gem install crypto_ticker
19
+
20
+ Usage
21
+ -----
22
+
23
+ ### Example
24
+
25
+ require 'crypto_ticker'
26
+ require 'mechanize'
27
+ require 'pp'
28
+
29
+ agent = Mechanize.new
30
+
31
+ # get MtGox BTC/USD ticker URL:
32
+ url = CryptoTicker::MtGox.ticker('BTC/USD')
33
+ json = agent.get( url ).body
34
+
35
+ mtgox_data_hash = CryptoCurrency::MtGox.info( json )
36
+ pp mtgox_data_hash
37
+
38
+ Contributing
39
+ ------------
40
+
41
+ 1. Fork it (Github repo: [homepage][homepage])
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
46
+
47
+ [homepage]: https://github.com/nmarley/crypto_ticker
48
+
49
+
50
+ License
51
+ -------
52
+ Released under the MIT License. See the [LICENSE][] file for further details.
53
+
54
+ [license]: LICENSE.md
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "lib" << "test"
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $:.unshift(lib) unless $:.include?(lib)
4
+ require 'crypto_ticker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crypto_ticker"
8
+ spec.version = CryptoTicker::VERSION
9
+ spec.authors = ["Nathan Marley"]
10
+ spec.email = ["nmarley@blackcarrot.be"]
11
+ spec.description = %q{Collection of public data API urls for various online
12
+ crypto-currency exchanges, e.g. MtGox, BTC-e, etc.}
13
+ spec.summary = spec.description
14
+ spec.homepage = "https://github.com/nmarley/crypto_ticker"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,3 @@
1
+ module CryptoTicker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,189 @@
1
+
2
+ require 'crypto_ticker/version'
3
+ require 'json'
4
+ require 'bigdecimal'
5
+ require 'bigdecimal/util'
6
+
7
+ ##
8
+ # This module bundles crypto-currency exchange public data API sources. (e.g.
9
+ # Bitcoin exchange rate, Litecoin, etc.)
10
+
11
+ module CryptoTicker
12
+
13
+ class FXPair < String
14
+ def to_sym
15
+ self.downcase.split('/').join('_').to_sym
16
+ end
17
+ end
18
+ class Symbol
19
+ def to_fxpair
20
+ FXPair.new( self.to_s.split('_').join('/').upcase )
21
+ end
22
+ end
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
+ ##
36
+ # MtGox BTC/USD ticker and retrieval method
37
+ #
38
+ # note: The official MtGox API documentation is very sparse, and some methods
39
+ # are published but undocumented. The 'ticker' method should work, others may
40
+ # lag or return large amounts of data.
41
+
42
+ class MtGox
43
+ @@valid_pairs = %w[ BTC/USD LTC/USD NMC/USD ]
44
+
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"
51
+ end
52
+ end
53
+
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"
59
+ end
60
+ end
61
+
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"
67
+ end
68
+ end
69
+
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
77
+ end
78
+ end
79
+
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
87
+ end
88
+ end
89
+ end
90
+ info
91
+ end
92
+ end
93
+
94
+
95
+ ##
96
+ # BTC-e tickers for various crypto-currencies and retrieval methods
97
+
98
+ class BTCe
99
+ @@valid_pairs = %w[ BTC/USD BTC/RUR BTC/EUR LTC/BTC LTC/USD LTC/RUR NMC/BTC
100
+ USD/RUR EUR/USD NVC/BTC TRC/BTC PPC/BTC RUC/BTC ]
101
+
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"
106
+ end
107
+ end
108
+
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"
113
+ end
114
+ end
115
+
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"
120
+ end
121
+ end
122
+
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
130
+ end
131
+ end
132
+
133
+
134
+ ##
135
+ # Vircurex ticker API and retrieval method
136
+ class Vircurex
137
+
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
144
+
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)
150
+
151
+ # upcase currency pair inputs
152
+ base.upcase!
153
+ quote.upcase!
154
+
155
+ # default value (may change this to just throw an exception)
156
+ last = 0.0
157
+
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
162
+ end
163
+
164
+ last
165
+ end
166
+ end
167
+
168
+
169
+ class Bitstamp
170
+ # this exchange only has BTC/USD
171
+ def self.ticker(base='BTC', quote='USD')
172
+ 'https://www.bitstamp.net/api/ticker/'
173
+ end
174
+
175
+ def self.info(json)
176
+ info = {}
177
+ JSON.parse(json).each do |k,v|
178
+ info[k.to_sym] = v.to_d
179
+ end
180
+ info
181
+ end
182
+
183
+ def self.last(json)
184
+ self.info(json)[:last]
185
+ end
186
+
187
+ end
188
+ end
189
+
@@ -0,0 +1,4 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'crypto_ticker'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,58 @@
1
+ require 'minitest_helper'
2
+ require 'uri'
3
+
4
+ class TestCryptoTicker < MiniTest::Unit::TestCase
5
+ def test_that_it_has_a_version_number
6
+ refute_nil ::CryptoTicker::VERSION
7
+ end
8
+
9
+ def test_urls
10
+ # BTC-e
11
+ # BTC/USD
12
+ assert CryptoTicker::BTCe.ticker('BTC/USD') =~ URI::regexp
13
+ assert CryptoTicker::BTCe.ticker('BTC','USD') =~ URI::regexp
14
+ assert CryptoTicker::BTCe.ticker('btc','usd') =~ URI::regexp
15
+ assert CryptoTicker::BTCe.ticker('bTC','UsD') =~ URI::regexp
16
+
17
+ # LTC/USD
18
+ assert CryptoTicker::BTCe.ticker('ltc', 'usd') =~ URI::regexp
19
+
20
+ # LTC/BTC
21
+ assert CryptoTicker::BTCe.ticker('ltc', 'btc') =~ URI::regexp
22
+
23
+ # other
24
+ assert CryptoTicker::BTCe.ticker('NMC/BTC') =~ URI::regexp
25
+ assert CryptoTicker::BTCe.ticker('NVC/BTC') =~ URI::regexp
26
+ assert CryptoTicker::BTCe.ticker('PPC/BTC') =~ URI::regexp
27
+ assert CryptoTicker::BTCe.ticker('RUC/BTC') =~ URI::regexp
28
+
29
+ # invalid
30
+ assert_nil CryptoTicker::BTCe.ticker('USD/BTC')
31
+ assert_nil CryptoTicker::BTCe.ticker('usd', 'btc')
32
+
33
+ # invalid - order is important
34
+ assert_nil CryptoTicker::BTCe.ticker('btc', 'ltc')
35
+
36
+ # invalid
37
+ assert_nil CryptoTicker::BTCe.ticker('xyz', 'abc')
38
+
39
+ # trades
40
+ assert CryptoTicker::BTCe.trades('BTC/USD') =~ URI::regexp
41
+
42
+ # depth
43
+ assert CryptoTicker::BTCe.depth('BTC/USD') =~ URI::regexp
44
+
45
+ # MtGox
46
+ assert CryptoTicker::MtGox.ticker('BTC/USD') =~ URI::regexp
47
+ assert CryptoTicker::MtGox.trades('BTC/USD') =~ URI::regexp
48
+ assert CryptoTicker::MtGox.depth('BTC/USD') =~ URI::regexp
49
+
50
+ # Vircurex
51
+ assert CryptoTicker::Vircurex.ticker =~ URI::regexp
52
+
53
+ # Bitstamp
54
+ assert CryptoTicker::Bitstamp.ticker =~ URI::regexp
55
+ end
56
+
57
+ end
58
+
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crypto_ticker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Marley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDgDCCAmigAwIBAgIBATANBgkqhkiG9w0BAQUFADBDMRAwDgYDVQQDDAdubWFy
14
+ bGV5MRswGQYKCZImiZPyLGQBGRYLYmxhY2tjYXJyb3QxEjAQBgoJkiaJk/Is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qGorUQFkwwrll4XglFRJGiLooFXN08sC
31
+ 6eRTq9IlmYPfHWtlTQo3d1rgg2Bx0ukp9+SbN81z+I7IbqE+
32
+ -----END CERTIFICATE-----
33
+ date: 2013-04-12 00:00:00.000000000 Z
34
+ dependencies:
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '1.3'
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: minitest
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ description: |-
78
+ Collection of public data API urls for various online
79
+ crypto-currency exchanges, e.g. MtGox, BTC-e, etc.
80
+ email:
81
+ - nmarley@blackcarrot.be
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.md
89
+ - README.md
90
+ - Rakefile
91
+ - crypto_ticker.gemspec
92
+ - lib/crypto_ticker.rb
93
+ - lib/crypto_ticker/version.rb
94
+ - test/minitest_helper.rb
95
+ - test/test_crypto_ticker.rb
96
+ homepage: https://github.com/nmarley/crypto_ticker
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.0.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Collection of public data API urls for various online crypto-currency exchanges,
120
+ e.g. MtGox, BTC-e, etc.
121
+ test_files:
122
+ - test/minitest_helper.rb
123
+ - test/test_crypto_ticker.rb
124
+ has_rdoc:
metadata.gz.sig ADDED
Binary file