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

Commit

Permalink
v2 coin symbol to slug
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 2, 2019
1 parent 2390dc3 commit 764d54d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
24 changes: 23 additions & 1 deletion v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

Expand All @@ -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))
Expand Down
22 changes: 22 additions & 0 deletions v2/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 764d54d

Please sign in to comment.