Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
get coin price usd helper
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 11, 2018
1 parent 7984dad commit fb15537
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion coinmarketcap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package coinmarketcap

import "testing"
import (
"testing"
)

func TestGetMarketData(t *testing.T) {

Expand All @@ -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) {

}
Expand Down

0 comments on commit fb15537

Please sign in to comment.