diff --git a/internal/common/common.go b/internal/common/common.go index 3f753f8..5ee18b5 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -1,7 +1,6 @@ package common import ( - . "github.com/achannarasappa/ticker/internal/currency" "github.com/go-resty/resty/v2" "github.com/spf13/afero" ) @@ -38,3 +37,11 @@ type Lot struct { UnitCost float64 `yaml:"unit_cost"` Quantity float64 `yaml:"quantity"` } + +type CurrencyRates map[string]CurrencyRate + +type CurrencyRate struct { + FromCurrency string + ToCurrency string + Rate float64 +} diff --git a/internal/currency/currency.go b/internal/currency/currency.go index cb04d7d..9c6410c 100644 --- a/internal/currency/currency.go +++ b/internal/currency/currency.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + c "github.com/achannarasappa/ticker/internal/common" "github.com/go-resty/resty/v2" . "github.com/novalagung/gubrak/v2" ) @@ -14,14 +15,6 @@ type ResponseQuote struct { RegularMarketPrice float64 `json:"regularMarketPrice"` } -type CurrencyRates map[string]CurrencyRate - -type CurrencyRate struct { - FromCurrency string - ToCurrency string - Rate float64 -} - type Response struct { QuoteResponse struct { Quotes []ResponseQuote `json:"result"` @@ -33,11 +26,11 @@ func getCurrencyPair(pair string) (string, string) { return pair[:3], pair[3:6] } -func transformResponseCurrency(responseQuote ResponseQuote) CurrencyRate { +func transformResponseCurrency(responseQuote ResponseQuote) c.CurrencyRate { fromCurrency, toCurrency := getCurrencyPair(responseQuote.Symbol) - return CurrencyRate{ + return c.CurrencyRate{ FromCurrency: fromCurrency, ToCurrency: toCurrency, Rate: responseQuote.RegularMarketPrice, @@ -45,19 +38,19 @@ func transformResponseCurrency(responseQuote ResponseQuote) CurrencyRate { } -func transformResponseCurrencies(responseQuotes []ResponseQuote) CurrencyRates { +func transformResponseCurrencies(responseQuotes []ResponseQuote) c.CurrencyRates { - currencyRates := From(responseQuotes).Reduce(func(acc CurrencyRates, responseQuote ResponseQuote) CurrencyRates { + currencyRates := From(responseQuotes).Reduce(func(acc c.CurrencyRates, responseQuote ResponseQuote) c.CurrencyRates { currencyRate := transformResponseCurrency(responseQuote) acc[currencyRate.FromCurrency] = currencyRate return acc - }, CurrencyRates{}).Result() + }, c.CurrencyRates{}).Result() - return (currencyRates).(CurrencyRates) + return (currencyRates).(c.CurrencyRates) } -func getCurrencyRatesFromCurrencyPairSymbols(client resty.Client, currencyPairSymbols []string) CurrencyRates { +func getCurrencyRatesFromCurrencyPairSymbols(client resty.Client, currencyPairSymbols []string) c.CurrencyRates { symbolsString := strings.Join(currencyPairSymbols, ",") url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US®ion=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=%s", symbolsString) @@ -96,7 +89,7 @@ func getCurrencyPairSymbols(client resty.Client, symbols []string, targetCurrenc return transformResponseCurrencyPairs((res.Result().(*Response)).QuoteResponse.Quotes, targetCurrency) } -func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency string) CurrencyRates { +func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency string) c.CurrencyRates { if targetCurrency == "" { targetCurrency = "USD" @@ -105,7 +98,7 @@ func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency stri currencyPairSymbols := getCurrencyPairSymbols(client, symbols, targetCurrency) if len(currencyPairSymbols) <= 0 { - return CurrencyRates{} + return c.CurrencyRates{} } return getCurrencyRatesFromCurrencyPairSymbols(client, currencyPairSymbols) diff --git a/internal/currency/currency_test.go b/internal/currency/currency_test.go index b1a127d..9c55da5 100644 --- a/internal/currency/currency_test.go +++ b/internal/currency/currency_test.go @@ -4,6 +4,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + c "github.com/achannarasappa/ticker/internal/common" . "github.com/achannarasappa/ticker/internal/currency" . "github.com/achannarasappa/ticker/test/http" ) @@ -15,8 +16,8 @@ var _ = Describe("Currency", func() { MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0}) MockResponse(ResponseParameters{Symbol: "EURUSD=X", Currency: "USD", Price: 1.2}) output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "USD") - Expect(output).To(Equal(CurrencyRates{ - "EUR": CurrencyRate{ + Expect(output).To(Equal(c.CurrencyRates{ + "EUR": c.CurrencyRate{ FromCurrency: "EUR", ToCurrency: "USD", Rate: 1.2, @@ -31,8 +32,8 @@ var _ = Describe("Currency", func() { MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0}) MockResponse(ResponseParameters{Symbol: "EURUSD=X", Currency: "USD", Price: 1.2}) output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "") - Expect(output).To(Equal(CurrencyRates{ - "EUR": CurrencyRate{ + Expect(output).To(Equal(c.CurrencyRates{ + "EUR": c.CurrencyRate{ FromCurrency: "EUR", ToCurrency: "USD", Rate: 1.2, @@ -47,7 +48,7 @@ var _ = Describe("Currency", func() { MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0}) output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "EUR") - Expect(output).To(Equal(CurrencyRates{})) + Expect(output).To(Equal(c.CurrencyRates{})) }) }) diff --git a/internal/position/position_test.go b/internal/position/position_test.go index d07c2bd..ff9d62a 100644 --- a/internal/position/position_test.go +++ b/internal/position/position_test.go @@ -2,7 +2,6 @@ package position_test import ( c "github.com/achannarasappa/ticker/internal/common" - "github.com/achannarasappa/ticker/internal/currency" . "github.com/achannarasappa/ticker/internal/position" . "github.com/achannarasappa/ticker/internal/quote" @@ -131,8 +130,8 @@ var _ = Describe("Position", func() { } inputCtx := c.Context{ Reference: c.Reference{ - CurrencyRates: currency.CurrencyRates{ - "EUR": currency.CurrencyRate{ + CurrencyRates: c.CurrencyRates{ + "EUR": c.CurrencyRate{ FromCurrency: "EUR", ToCurrency: "USD", Rate: 1.5,