From 764d54d50397e6514158c10c4f943153eab0ec09 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Mon, 1 Jul 2019 22:37:16 -0700 Subject: [PATCH] v2 coin symbol to slug --- v2/v2.go | 24 +++++++++++++++++++++++- v2/v2_test.go | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/v2/v2.go b/v2/v2.go index 5ec6892..d5f03de 100644 --- a/v2/v2.go +++ b/v2/v2.go @@ -37,6 +37,7 @@ type Interface interface { Price(options *PriceOptions) (float64, error) CoinID(symbol string) (int, error) CoinSlug(symbol string) (string, error) + CoinSymbol(slug string) (string, error) } // listingsMedia listings response media @@ -315,8 +316,12 @@ func CoinID(symbol string) (int, error) { if l.Symbol == symbol { return l.ID, nil } + + if l.Slug == strings.ToLower(symbol) { + return l.ID, nil + } } - //returns error as default + return 0, errors.New("coin not found") } @@ -336,6 +341,23 @@ func CoinSlug(symbol string) (string, error) { return coin.Slug, nil } +// CoinSymbol gets the symbol for the cryptocurrency +func CoinSymbol(slug string) (string, error) { + slug = strings.ToLower(strings.TrimSpace(slug)) + coin, err := Ticker(&TickerOptions{ + Symbol: slug, + }) + if err != nil { + return "", err + } + + if coin == nil { + return "", errors.New("coin not found") + } + + return coin.Symbol, nil +} + // toInt helper for parsing strings to int func toInt(rawInt string) int { parsed, _ := strconv.Atoi(strings.Replace(strings.Replace(rawInt, "$", "", -1), ",", "", -1)) diff --git a/v2/v2_test.go b/v2/v2_test.go index ec55408..86dbff7 100644 --- a/v2/v2_test.go +++ b/v2/v2_test.go @@ -213,6 +213,28 @@ func TestMarkets(t *testing.T) { } } +func TestCoinSlug(t *testing.T) { + slug, err := CoinSlug("btc") + if err != nil { + t.FailNow() + } + + if slug != "bitcoin" { + t.FailNow() + } +} + +func TestCoinSymbol(t *testing.T) { + symbol, err := CoinSymbol("bitcoin") + if err != nil { + t.FailNow() + } + + if symbol != "BTC" { + t.FailNow() + } +} + func TestPrice(t *testing.T) { price, err := Price(&PriceOptions{ Symbol: "ETH",