From 15337d46723f34217d22f02e365aebc8ee637806 Mon Sep 17 00:00:00 2001 From: Lokraan Date: Mon, 22 Oct 2018 23:38:35 -0500 Subject: [PATCH] Move duplicate functionality into `source.ex` --- .../lib/explorer/exchange_rates/source.ex | 14 +++++++++++++ .../exchange_rates/source/coin_gecko.ex | 20 +++++-------------- .../exchange_rates/source/coin_market_cap.ex | 16 +++------------ 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/apps/explorer/lib/explorer/exchange_rates/source.ex b/apps/explorer/lib/explorer/exchange_rates/source.ex index 51cfb8336a..6445fedf7a 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source.ex @@ -9,4 +9,18 @@ defmodule Explorer.ExchangeRates.Source do Callback for fetching an exchange rates for currencies/tokens. """ @callback fetch_exchange_rates :: {:ok, [Token.t()]} | {:error, any} + + def headers do + [{"Content-Type", "application/json"}] + end + + def decode_json(data) do + Jason.decode!(data) + end + + def to_decimal(nil), do: nil + + def to_decimal(value) do + Decimal.new(value) + end end diff --git a/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex b/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex index cf6e2d90a9..c8ef67f2fa 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex @@ -6,13 +6,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do alias Explorer.ExchangeRates.{Source, Token} alias HTTPoison.{Error, Response} + import Source, only: [decode_json: 1, to_decimal: 1, headers: 0] + @behaviour Source + @headers @impl Source def fetch_exchange_rates do - headers = [{"Content-Type", "application/json"}] - - case HTTPoison.get(source_url(), headers) do + case HTTPoison.get(source_url(), headers()) do {:ok, %Response{body: body, status_code: 200}} -> {:ok, format_data(body)} @@ -57,25 +58,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do configured_url || "https://api.coingecko.com/api/v3" end - defp decode_json(data) do - Jason.decode!(data) - end - - defp to_decimal(nil), do: nil - - defp to_decimal(value) do - Decimal.new(value) - end - defp source_url(currency \\ "usd") do "#{base_url()}/coins/markets?vs_currency=#{currency}" end defp get_btc_price(currency \\ "usd") do - headers = [{"Content-Type", "application/json"}] url = "#{base_url()}/exchange_rates" - case HTTPoison.get(url, headers) do + case HTTPoison.get(url, headers()) do {:ok, %Response{body: body, status_code: 200}} -> data = decode_json(body) current_price = data["rates"][currency]["value"] diff --git a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex b/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex index 1031858337..f8137fee79 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex @@ -6,13 +6,13 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do alias Explorer.ExchangeRates.{Source, Token} alias HTTPoison.{Error, Response} + import Source, only: [decode_json: 1, to_decimal: 1, headers: 0] + @behaviour Source @impl Source def fetch_exchange_rates do - headers = [{"Content-Type", "application/json"}] - - case HTTPoison.get(source_url(), headers) do + case HTTPoison.get(source_url(), headers()) do {:ok, %Response{body: body, status_code: 200}} -> {:ok, format_data(body)} @@ -49,16 +49,6 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do configured_url || "https://api.coinmarketcap.com" end - defp decode_json(data) do - Jason.decode!(data) - end - - defp to_decimal(nil), do: nil - - defp to_decimal(value) do - Decimal.new(value) - end - defp source_url do "#{base_url()}/v1/ticker/?limit=0" end