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

Commit

Permalink
Tickers: also evaluate metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCount committed May 21, 2018
1 parent 8576053 commit 5f76173
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ type TickersOptions struct {

// tickerMedia tickers response media
type tickersMedia struct {
Data map[string]*types.Ticker `json:"data"`
Data map[string]*types.Ticker `json:"data,omitempty"`
Metadata struct {
Timestamp int64
NumCryptoCurrencies int `json:"num_cryptocurrencies,omitempty"`
Error string `json:",omitempty"`
}
}

// Tickers gets ticker information on coins
func Tickers(options *TickersOptions) (map[string]*types.Ticker, error) {
func Tickers(options *TickersOptions) (map[string]*types.Ticker, int, error) {
var params []string
if options.Start >= 0 {
params = append(params, fmt.Sprintf("start=%v", options.Start))
Expand All @@ -83,14 +88,17 @@ func Tickers(options *TickersOptions) (map[string]*types.Ticker, error) {
var body tickersMedia
err = json.Unmarshal(resp, &body)
if err != nil {
return nil, err
return nil, 0, err
}
tickers := make(map[string]*types.Ticker)
data := body.Data
for _, v := range data {
tickers[strings.ToUpper(string(v.Symbol))] = v
}
return tickers, nil
if body.Metadata.Error != "" {
err = errors.New(body.Metadata.Error)
}
return tickers, body.Metadata.NumCryptoCurrencies, err
}

// TickerOptions options for ticker method
Expand Down Expand Up @@ -269,7 +277,7 @@ type PriceOptions struct {

// Price gets price of a cryptocurrency
func Price(options *PriceOptions) (float64, error) {
coins, err := Tickers(&TickersOptions{
coins, _, err := Tickers(&TickersOptions{
Convert: options.Convert,
})
if err != nil {
Expand All @@ -285,7 +293,7 @@ func Price(options *PriceOptions) (float64, error) {
// CoinID gets the ID for the cryptocurrency
func CoinID(symbol string) (int, error) {
symbol = strings.ToUpper(strings.TrimSpace(symbol))
coins, err := Tickers(&TickersOptions{})
coins, _, err := Tickers(&TickersOptions{})
if err != nil {
return 0, err
}
Expand All @@ -299,7 +307,7 @@ func CoinID(symbol string) (int, error) {
// CoinSlug gets the slug for the cryptocurrency
func CoinSlug(symbol string) (string, error) {
symbol = strings.ToUpper(strings.TrimSpace(symbol))
coins, err := Tickers(&TickersOptions{})
coins, _, err := Tickers(&TickersOptions{})
if err != nil {
return "", err
}
Expand Down

0 comments on commit 5f76173

Please sign in to comment.