Added functionality and future handling for currency.go

This commit is contained in:
Ryan O'Hara-Reid
2017-04-05 13:03:30 +10:00
committed by Adrian Gallagher
parent c579611706
commit 94fc25ce25

View File

@@ -63,21 +63,33 @@ func IsDefaultCryptocurrency(currency string) bool {
} }
func IsFiatCurrency(currency string) bool { func IsFiatCurrency(currency string) bool {
if BaseCurrencies == "" {
log.Println("IsFiatCurrency: BaseCurrencies string variable not populated")
}
return common.StringContains(BaseCurrencies, common.StringToUpper(currency)) return common.StringContains(BaseCurrencies, common.StringToUpper(currency))
} }
func IsCryptocurrency(currency string) bool { func IsCryptocurrency(currency string) bool {
if CryptoCurrencies == "" {
log.Println("IsCryptocurrency: CryptoCurrencies string variable not populated")
}
return common.StringContains(CryptoCurrencies, common.StringToUpper(currency)) return common.StringContains(CryptoCurrencies, common.StringToUpper(currency))
} }
func ContainsSeparator(input string) (bool, string) { func ContainsSeparator(input string) (bool, string) {
separators := []string{"-", "_"} separators := []string{"-", "_"}
var separatorsContainer []string
for _, x := range separators { for _, x := range separators {
if common.StringContains(input, x) { if common.StringContains(input, x) {
return true, x separatorsContainer = append(separatorsContainer, x)
} }
} }
return false, "" if len(separatorsContainer) == 0 {
return false, ""
} else {
return true, strings.Join(separatorsContainer, ",")
}
} }
func ContainsBaseCurrencyIndex(baseCurrencies []string, currency string) (bool, string) { func ContainsBaseCurrencyIndex(baseCurrencies []string, currency string) (bool, string) {
@@ -100,13 +112,28 @@ func ContainsBaseCurrency(baseCurrencies []string, currency string) bool {
func CheckAndAddCurrency(input []string, check string) []string { func CheckAndAddCurrency(input []string, check string) []string {
for _, x := range input { for _, x := range input {
if check == x { if IsDefaultCurrency(x) {
if IsDefaultCurrency(check) {
if check == x {
return input
}
continue
} else {
return input
}
} else if IsDefaultCryptocurrency(x) {
if IsDefaultCryptocurrency(check) {
if check == x {
return input
}
continue
} else {
return input
}
} else {
return input return input
} }
} }
if IsCryptocurrency(check) && IsFiatCurrency(check) {
return input
}
input = append(input, check) input = append(input, check)
return input return input
@@ -118,7 +145,6 @@ func SeedCurrencyData(fiatCurrencies string) error {
} }
err := QueryYahooCurrencyValues(fiatCurrencies) err := QueryYahooCurrencyValues(fiatCurrencies)
if err != nil { if err != nil {
return ErrQueryingYahoo return ErrQueryingYahoo
} }
@@ -142,16 +168,15 @@ func MakecurrencyPairs(supportedCurrencies string) string {
} }
func ConvertCurrency(amount float64, from, to string) (float64, error) { func ConvertCurrency(amount float64, from, to string) (float64, error) {
if len(CurrencyStore) == 0 { currency := common.StringToUpper(from + to)
err := SeedCurrencyData(common.StringToUpper(from) + "," + common.StringToUpper(to))
if CurrencyStore[currency].Name != currency {
err := SeedCurrencyData(currency[:len(from)] + "," + currency[len(to):])
if err != nil { if err != nil {
return 0, err return 0, err
} }
} }
currency := common.StringToUpper(from + to)
if common.StringContains(currency, "RUB") {
currency = strings.Replace(currency, "RUB", "RUR", -1)
}
for x, y := range CurrencyStore { for x, y := range CurrencyStore {
if x == currency { if x == currency {
return amount * y.Rate, nil return amount * y.Rate, nil