Sha256: 2588918a5d8cc97f5194ef474384b89bdf02cb45e2f0a3db0f75050040e1dd11

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "json"
require "open-uri"

module Capwatch
  module Providers
    class CoinMarketCap

      attr_accessor :body

      NoCoinInProvider = Class.new(RuntimeError)

      TICKER_URL = "https://api.coinmarketcap.com/v1/ticker/"

      def fetched_json
        response = parse(fetch)
        update_rates(response)
        response
      end

      def fetch
        @body ||= open(TICKER_URL).read
      end

      def parse(response)
        JSON.parse(response)
      end

      def update_rates(response)
        response.each do |coin_json|
          Capwatch::Exchange.rate(
            coin_json['symbol'],
            coin_json["price_btc"].to_f
          )
        end
      end

      def update_coin(coin)
        provider_coin = fetched_json.find { |c| c["symbol"] == coin.symbol }
        fail NoCoinInProvider, "No #{coin.symbol} in provider response" if provider_coin.nil?
        coin.name               = provider_coin["name"]
        coin.price_usd          = provider_coin["price_usd"].to_f
        coin.price_btc          = provider_coin["price_btc"].to_f
        coin.percent_change_1h  = provider_coin["percent_change_1h"].to_f
        coin.percent_change_24h = provider_coin["percent_change_24h"].to_f
        coin.percent_change_7d  = provider_coin["percent_change_7d"].to_f
      end

    end # class CoinMarketCap

  end # module Providers

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capwatch-0.2.5 lib/capwatch/providers/coin_market_cap.rb
capwatch-0.2.4 lib/capwatch/providers/coin_market_cap.rb
capwatch-0.2.3 lib/capwatch/providers/coin_market_cap.rb
capwatch-0.2.0 lib/capwatch/providers/coin_market_cap.rb