Skip to content

Commit

Permalink
refactor: moved currency rate structs to common
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 14, 2021
1 parent 0d079d7 commit 9332f0c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
9 changes: 8 additions & 1 deletion internal/common/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
. "github.com/achannarasappa/ticker/internal/currency"
"github.com/go-resty/resty/v2"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -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
}
27 changes: 10 additions & 17 deletions internal/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"`
Expand All @@ -33,31 +26,31 @@ 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,
}

}

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&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=%s", symbolsString)
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions internal/currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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{}))

})
})
Expand Down
5 changes: 2 additions & 3 deletions internal/position/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9332f0c

Please sign in to comment.