Class: CryptoArbitrer::Base
- Inherits:
-
Object
- Object
- CryptoArbitrer::Base
- Defined in:
- lib/crypto_arbitrer.rb
Class Method Summary collapse
- .btc_usd_strategy ⇒ Object
- .btc_usd_strategy=(strategy) ⇒ Object
- .cache_backend ⇒ Object
-
.cache_backend=(backend) ⇒ Object
If you want to cache calls done to third party api’s you should use this.
-
.derive_reversal(from, to) ⇒ Object
For existing known exchange rates, we want to derive the reverse lookup, for example: We derive ars_usd from usd_ars.
-
.fetch(from, to, force = false) ⇒ {'buy' => Float, 'sell' => Float}
Fetch a given conversion caching the result if a backend is set.
-
.supported_conversions ⇒ Object
Quasi constant, a list of iso code pairs representing all supported exchange rates.
-
.supported_cryptos ⇒ Object
Quasi constant, all supported crypto currencies.
-
.supported_currencies ⇒ Object
Quasi constant, all supported currencies.
-
.supported_fiat ⇒ Object
Quasi constant, all supported fiat currencies.
Instance Method Summary collapse
- #fetch(from, to, force = false) ⇒ Object
-
#fetch_btc_usd ⇒ Object
Uses mt.gox API for checking latest bitcoin sell and buy prices.
-
#fetch_usd_ars ⇒ Object
Uses eldolarblue.net API for checking Argentine unofficial US dolar prices.
-
#fetch_usd_vef ⇒ Object
Goes to dolarparalelo.org to grab the actual price for usd to vef The returned hash has ‘sell’ and ‘buy’ keys for the different prices, the prices mean at which price you could buy and sell from them, respectively.
- #parse_btce_prices(response) ⇒ Object
Class Method Details
.btc_usd_strategy ⇒ Object
8 9 10 |
# File 'lib/crypto_arbitrer.rb', line 8 def self.btc_usd_strategy @btc_usd_strategy || :mtgox end |
.btc_usd_strategy=(strategy) ⇒ Object
12 13 14 |
# File 'lib/crypto_arbitrer.rb', line 12 def self.btc_usd_strategy=(strategy) @btc_usd_strategy = strategy end |
.cache_backend ⇒ Object
47 48 49 |
# File 'lib/crypto_arbitrer.rb', line 47 def cache_backend @cache_backend end |
.cache_backend=(backend) ⇒ Object
If you want to cache calls done to third party api’s you should use this. Pass in a lambda that will receive the cache key name and the block for fetching it’s value. If you’re using rails you can configure CryptoArbitrer in an initializer to use rails caching just forwarding the cache key and the block to it. You can set the cache_backend to nil to prevent caching (not recommended, unless you’re testing)
43 44 45 |
# File 'lib/crypto_arbitrer.rb', line 43 def cache_backend=(backend) @cache_backend = backend end |
.derive_reversal(from, to) ⇒ Object
For existing known exchange rates, we want to derive the reverse lookup, for example: We derive ars_usd from usd_ars
54 55 56 57 58 59 |
# File 'lib/crypto_arbitrer.rb', line 54 def self.derive_reversal(from, to) define_method("fetch_#{to}_#{from}") do rate = send("fetch_#{from}_#{to}") {'sell' => 1/rate['sell'], 'buy' => 1/rate['buy']} end end |
.fetch(from, to, force = false) ⇒ {'buy' => Float, 'sell' => Float}
Fetch a given conversion caching the result if a backend is set. This should be the preferred way to fetch a conversion by users. Check #supported_currencies for a list of possible values for ‘from’ and ‘to’
177 178 179 |
# File 'lib/crypto_arbitrer.rb', line 177 def self.fetch(from,to, force = false) new.fetch(from.downcase, to.downcase, force) end |
.supported_conversions ⇒ Object
Quasi constant, a list of iso code pairs representing all supported exchange rates.
- [‘ars’,‘usd’],,[‘ltc’,‘cnc’],…
33 34 35 |
# File 'lib/crypto_arbitrer.rb', line 33 def self.supported_conversions supported_currencies.product(supported_currencies) end |
.supported_cryptos ⇒ Object
Quasi constant, all supported crypto currencies.
22 23 24 |
# File 'lib/crypto_arbitrer.rb', line 22 def self.supported_cryptos %w(btc ltc nmc nvc trc ppc ftc cnc) end |
.supported_currencies ⇒ Object
Quasi constant, all supported currencies.
27 28 29 |
# File 'lib/crypto_arbitrer.rb', line 27 def self.supported_currencies supported_fiat + supported_cryptos end |
.supported_fiat ⇒ Object
Quasi constant, all supported fiat currencies.
17 18 19 |
# File 'lib/crypto_arbitrer.rb', line 17 def self.supported_fiat %w(usd ars uyu brl clp sgd eur vef) end |
Instance Method Details
#fetch(from, to, force = false) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/crypto_arbitrer.rb', line 161 def fetch(from, to, force = false) if force send("fetch_#{from}_#{to}") else cached(from, to){ send("fetch_#{from}_#{to}") } end end |
#fetch_btc_usd ⇒ Object
Uses mt.gox API for checking latest bitcoin sell and buy prices. The returned hash has ‘sell’ and ‘buy’ keys for the different prices, the prices mean at which price you could buy and sell from them, respectively.
78 79 80 81 82 83 84 85 86 |
# File 'lib/crypto_arbitrer.rb', line 78 def fetch_btc_usd if self.class.btc_usd_strategy == :mtgox response = open('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast').read json = JSON.parse(response)['data'] {'sell' => json['sell']['value'].to_f, 'buy' => json['buy']['value'].to_f} else parse_btce_prices open("https://btc-e.com/exchange/btc_usd").read end end |
#fetch_usd_ars ⇒ Object
Uses eldolarblue.net API for checking Argentine unofficial US dolar prices. The returned hash has ‘sell’ and ‘buy’ keys for the different prices. ‘buy’ is the price at which you can buy USD from agents and ‘sell’ is the price at which you can sell USD to agents. Notice this is the opposite to argentina’s convention for ‘buy’ and ‘sell’ (where buy is the price in which agents would buy from you) The ugly regex is to fix the service’s response which is not valid json but a javascript object literal.
68 69 70 71 72 |
# File 'lib/crypto_arbitrer.rb', line 68 def fetch_usd_ars response = open('http://www.eldolarblue.net/getDolarBlue.php?as=json').read rate = JSON.parse(response.gsub(/([{,])([^:]*)/, '\1"\2"') )['exchangerate'] {'sell' => rate['buy'], 'buy' => rate['sell']} end |
#fetch_usd_vef ⇒ Object
Goes to dolarparalelo.org to grab the actual price for usd to vef The returned hash has ‘sell’ and ‘buy’ keys for the different prices, the prices mean at which price you could buy and sell from them, respectively.
92 93 94 95 96 97 |
# File 'lib/crypto_arbitrer.rb', line 92 def fetch_usd_vef response = open('http://www.dolarparalelo.org').read response =~ /<p><font.*?>Dolar:<\/font>.*?<font.*?>(.*?)</ rate = $1.to_f {'sell' => rate, 'buy' => rate} end |
#parse_btce_prices(response) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/crypto_arbitrer.rb', line 113 def parse_btce_prices(response) response =~ /<span id=.max_price..(.*?)..span./ sell = $1.to_f response =~ /<span id=.min_price..(.*?)..span./ buy = $1.to_f {'sell' => sell, 'buy' => buy} end |