Skip to content

Commit

Permalink
Rates are now always returned as a map
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed Sep 30, 2019
1 parent 84f6f9d commit b06f062
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions exchange_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
"time"
)

Expand All @@ -17,29 +18,41 @@ var RatesURL = DefaultRatesURL
var DefaultTimeout = 5 * time.Second

// ExchangeRates returs the list exchange rates
func ExchangeRates() (ExchangeRate, error) {
func ExchangeRates() (map[string]float64, error) {

var rates ExchangeRate

ratesMap := make(map[string]float64, 0)

client := &http.Client{}
client.Timeout = DefaultTimeout

resp, err := client.Get(RatesURL)
if err != nil {
return rates, err
return ratesMap, err
}
defer resp.Body.Close()

rawData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return rates, err
return ratesMap, err
}

err = xml.Unmarshal(rawData, &rates)
if err != nil {
return rates, err
return ratesMap, err
}

ratesMap["EUR"] = 1

for _, cube := range rates.Cubes {
for _, timedCube := range cube.TimedCubes {
for _, rate := range timedCube.Rates {
ratesMap[strings.ToUpper(rate.Currency)] = rate.Rate
}
}
}

return rates, nil
return ratesMap, nil

}

0 comments on commit b06f062

Please sign in to comment.