class CryptoArbitrer::Base

Public Class Methods

btc_usd_strategy() click to toggle source
# File lib/crypto_arbitrer.rb, line 8
def self.btc_usd_strategy
  @btc_usd_strategy || :mtgox
end
btc_usd_strategy=(strategy) click to toggle source
# File lib/crypto_arbitrer.rb, line 12
def self.btc_usd_strategy=(strategy)
  @btc_usd_strategy = strategy
end
cache_backend() click to toggle source
# File lib/crypto_arbitrer.rb, line 47
def cache_backend
  @cache_backend
end
cache_backend=(backend) click to toggle source

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)

# File lib/crypto_arbitrer.rb, line 43
def cache_backend=(backend)
  @cache_backend = backend
end
derive_reversal(from, to) click to toggle source

For existing known exchange rates, we want to derive the reverse lookup, for example: We derive ars_usd from usd_ars

# 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) click to toggle source

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' @param from [String] a three letter currency code to convert from. @param to [String] a three letter currency code to convert to. @param to [String] a three letter currency code to convert to. @param force [Boolean] Ignore the cache (does not read from it, and does not write to it). Defaults to false. @return [{'buy' => Float, 'sell' => Float}] The buy and sell prices

# File lib/crypto_arbitrer.rb, line 177
def self.fetch(from,to, force = false)
  new.fetch(from.downcase, to.downcase, force)
end
supported_conversions() click to toggle source

Quasi constant, a list of iso code pairs representing all supported exchange rates.

['ars’,‘usd’],,['ltc','cnc'],…
# File lib/crypto_arbitrer.rb, line 33
def self.supported_conversions
  supported_currencies.product(supported_currencies)
end
supported_cryptos() click to toggle source

Quasi constant, all supported crypto currencies.

# File lib/crypto_arbitrer.rb, line 22
def self.supported_cryptos
  %w(btc ltc nmc nvc trc ppc ftc cnc)
end
supported_currencies() click to toggle source

Quasi constant, all supported currencies.

# File lib/crypto_arbitrer.rb, line 27
def self.supported_currencies
  supported_fiat + supported_cryptos
end
supported_fiat() click to toggle source

Quasi constant, all supported fiat currencies.

# File lib/crypto_arbitrer.rb, line 17
def self.supported_fiat
  %w(usd ars uyu brl clp sgd eur vef)
end

Public Instance Methods

fetch(from, to, force = false) click to toggle source
# 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() click to toggle source

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.

# 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() click to toggle source

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.

# 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() click to toggle source

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.

# 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) click to toggle source
# 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

Protected Instance Methods

cached(from, to, &block) click to toggle source
# File lib/crypto_arbitrer.rb, line 182
def cached(from, to, &block)
  if self.class.cache_backend
    self.class.cache_backend.call(from, to, block)
  else
    block.call
  end
end