Sha256: 18c93c67822198d7a030bbc62ff49030ba17b4a2ba129304b553a93edd18047d

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module CurrencyRate
  class HuobiAdapter < CryptoAdapter

    FETCH_URL = {
      'btc_cny' => 'http://api.huobi.com/staticmarket/ticker_btc_json.js',
      'ltc_cny' => 'http://api.huobi.com/staticmarket/ticker_ltc_json.js'
    }
    DEFAULT_CURRENCIES = ["CNY", "BTC"]
    SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]

    def rate_for(from,to)
      super
      rate = rate_to_f(currency_pair_rate(to,from))
      invert_rate(from,to,rate)
    end

    def currency_pair_rate(currency1, currency2)
      rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
      raise CurrencyNotSupported unless rate
      rate["ticker"]['last']
    end

    def invert_rate(from,to,rate)
      if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to)
        _invert_rate(rate)
      else
        rate
      end
    end

    def supported_currency_pairs
      cache_supported_currency_pairs do
        @rates.each do |k,v|
          @supported_currency_pairs << k.sub("_", "/").upcase
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
currency-rate-0.4.1 lib/crypto_adapters/huobi_adapter.rb
currency-rate-0.4.0 lib/crypto_adapters/huobi_adapter.rb
currency-rate-0.3.9 lib/crypto_adapters/huobi_adapter.rb