diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e471262 Binary files /dev/null and b/.DS_Store differ diff --git a/coinmarketcap.go b/coinmarketcap.go index f48cdc1..30dc292 100644 --- a/coinmarketcap.go +++ b/coinmarketcap.go @@ -35,6 +35,7 @@ func GetMarketData() (GlobalMarketData, error) { // GetCoinData get information about a crypto currency func GetCoinData(coin string) (Coin, error) { + coin = strings.ToLower(coin) url = fmt.Sprintf("%s/ticker/%s", baseURL, coin) resp, err := makeReq(url) if err != nil { @@ -89,6 +90,15 @@ func GetCoinGraphData(coin string, start int64, end int64) (CoinGraph, error) { return data, nil } +// GetCoinPriceUsd get USD price of crypto currency +func GetCoinPriceUsd(coin string) (float64, error) { + data, err := GetCoinData(coin) + if err != nil { + return float64(0), nil + } + return data.PriceUsd, nil +} + // CoinMarkets get market data for a coin name. func CoinMarkets(coin string) ([]Market, error) { url := fmt.Sprintf("https://coinmarketcap.com/currencies/%s/#markets", coin) diff --git a/coinmarketcap_test.go b/coinmarketcap_test.go index df31fd9..595d1e3 100644 --- a/coinmarketcap_test.go +++ b/coinmarketcap_test.go @@ -1,6 +1,8 @@ package coinmarketcap -import "testing" +import ( + "testing" +) func TestGetMarketData(t *testing.T) { @@ -18,6 +20,16 @@ func TestGetCoinGraphData(t *testing.T) { } +func TestGetCoinPriceUsd(t *testing.T) { + price, err := GetCoinPriceUsd("ethereum") + if err != nil { + t.FailNow() + } + if price <= 0 { + t.FailNow() + } +} + func TestCoinMarkets(t *testing.T) { }