Move duplicate functionality into `source.ex`

pull/969/head
Lokraan 6 years ago committed by Locke
parent b078ed7a76
commit 15337d4672
  1. 14
      apps/explorer/lib/explorer/exchange_rates/source.ex
  2. 20
      apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex
  3. 16
      apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex

@ -9,4 +9,18 @@ defmodule Explorer.ExchangeRates.Source do
Callback for fetching an exchange rates for currencies/tokens. Callback for fetching an exchange rates for currencies/tokens.
""" """
@callback fetch_exchange_rates :: {:ok, [Token.t()]} | {:error, any} @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 end

@ -6,13 +6,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do
alias Explorer.ExchangeRates.{Source, Token} alias Explorer.ExchangeRates.{Source, Token}
alias HTTPoison.{Error, Response} alias HTTPoison.{Error, Response}
import Source, only: [decode_json: 1, to_decimal: 1, headers: 0]
@behaviour Source @behaviour Source
@headers
@impl Source @impl Source
def fetch_exchange_rates do 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, %Response{body: body, status_code: 200}} ->
{:ok, format_data(body)} {:ok, format_data(body)}
@ -57,25 +58,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do
configured_url || "https://api.coingecko.com/api/v3" configured_url || "https://api.coingecko.com/api/v3"
end 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 defp source_url(currency \\ "usd") do
"#{base_url()}/coins/markets?vs_currency=#{currency}" "#{base_url()}/coins/markets?vs_currency=#{currency}"
end end
defp get_btc_price(currency \\ "usd") do defp get_btc_price(currency \\ "usd") do
headers = [{"Content-Type", "application/json"}]
url = "#{base_url()}/exchange_rates" url = "#{base_url()}/exchange_rates"
case HTTPoison.get(url, headers) do case HTTPoison.get(url, headers()) do
{:ok, %Response{body: body, status_code: 200}} -> {:ok, %Response{body: body, status_code: 200}} ->
data = decode_json(body) data = decode_json(body)
current_price = data["rates"][currency]["value"] current_price = data["rates"][currency]["value"]

@ -6,13 +6,13 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
alias Explorer.ExchangeRates.{Source, Token} alias Explorer.ExchangeRates.{Source, Token}
alias HTTPoison.{Error, Response} alias HTTPoison.{Error, Response}
import Source, only: [decode_json: 1, to_decimal: 1, headers: 0]
@behaviour Source @behaviour Source
@impl Source @impl Source
def fetch_exchange_rates do 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, %Response{body: body, status_code: 200}} ->
{:ok, format_data(body)} {:ok, format_data(body)}
@ -49,16 +49,6 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
configured_url || "https://api.coinmarketcap.com" configured_url || "https://api.coinmarketcap.com"
end 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 defp source_url do
"#{base_url()}/v1/ticker/?limit=0" "#{base_url()}/v1/ticker/?limit=0"
end end

Loading…
Cancel
Save