parent
3604187fd3
commit
372fb0d51d
@ -1,52 +0,0 @@ |
||||
defmodule Explorer.ExchangeRates.Source.CoinMarketCap do |
||||
@moduledoc """ |
||||
Adapter for fetching exchange rates from https://coinmarketcap.com. |
||||
""" |
||||
|
||||
alias Explorer.ExchangeRates.{Source, Token} |
||||
|
||||
import Source, only: [decode_json: 1, to_decimal: 1] |
||||
|
||||
@behaviour Source |
||||
|
||||
@impl Source |
||||
def format_data(data) do |
||||
for item <- decode_json(data), not is_nil(item["last_updated"]) do |
||||
{last_updated_as_unix, _} = Integer.parse(item["last_updated"]) |
||||
last_updated = DateTime.from_unix!(last_updated_as_unix) |
||||
|
||||
%Token{ |
||||
available_supply: to_decimal(item["available_supply"]), |
||||
total_supply: to_decimal(item["total_supply"]), |
||||
btc_value: to_decimal(item["price_btc"]), |
||||
id: item["id"], |
||||
last_updated: last_updated, |
||||
market_cap_usd: to_decimal(item["market_cap_usd"]), |
||||
name: item["name"], |
||||
symbol: item["symbol"], |
||||
usd_value: to_decimal(item["price_usd"]), |
||||
volume_24h_usd: to_decimal(item["24h_volume_usd"]) |
||||
} |
||||
end |
||||
end |
||||
|
||||
@impl Source |
||||
def source_url do |
||||
source_url(1) |
||||
end |
||||
|
||||
def source_url(page) do |
||||
"#{base_url()}/v1/ticker/?start=#{page - 1}00" |
||||
end |
||||
|
||||
def max_page_number, do: config(:pages) |
||||
|
||||
defp base_url do |
||||
config(:base_url) || "https://api.coinmarketcap.com" |
||||
end |
||||
|
||||
@spec config(atom()) :: term |
||||
defp config(key) do |
||||
Application.get_env(:explorer, __MODULE__, [])[key] |
||||
end |
||||
end |
@ -1,59 +0,0 @@ |
||||
defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do |
||||
use ExUnit.Case |
||||
|
||||
alias Explorer.ExchangeRates.Token |
||||
alias Explorer.ExchangeRates.Source.CoinMarketCap |
||||
|
||||
@json """ |
||||
[ |
||||
{ |
||||
"id": "poa-network", |
||||
"name": "POA Network", |
||||
"symbol": "POA", |
||||
"rank": "103", |
||||
"price_usd": "0.485053", |
||||
"price_btc": "0.00007032", |
||||
"24h_volume_usd": "20185000.0", |
||||
"market_cap_usd": "98941986.0", |
||||
"available_supply": "203981804.0", |
||||
"total_supply": "254473964.0", |
||||
"max_supply": null, |
||||
"percent_change_1h": "-0.66", |
||||
"percent_change_24h": "12.34", |
||||
"percent_change_7d": "49.15", |
||||
"last_updated": "1523473200" |
||||
} |
||||
] |
||||
""" |
||||
|
||||
describe "format_data/1" do |
||||
test "returns valid tokens with valid data" do |
||||
expected_date = ~N[2018-04-11 19:00:00] |> DateTime.from_naive!("Etc/UTC") |
||||
|
||||
expected = [ |
||||
%Token{ |
||||
available_supply: Decimal.new("203981804.0"), |
||||
total_supply: Decimal.new("254473964.0"), |
||||
btc_value: Decimal.new("0.00007032"), |
||||
id: "poa-network", |
||||
last_updated: expected_date, |
||||
market_cap_usd: Decimal.new("98941986.0"), |
||||
name: "POA Network", |
||||
symbol: "POA", |
||||
usd_value: Decimal.new("0.485053"), |
||||
volume_24h_usd: Decimal.new("20185000.0") |
||||
} |
||||
] |
||||
|
||||
assert expected == CoinMarketCap.format_data(@json) |
||||
end |
||||
|
||||
test "returns nothing when given bad data" do |
||||
bad_data = """ |
||||
[{"id": "poa-network"}] |
||||
""" |
||||
|
||||
assert [] = CoinMarketCap.format_data(bad_data) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue