From 1e4070caf3f342191cd53d73ac224512666cbb6e Mon Sep 17 00:00:00 2001 From: Wesley Werneck Date: Sun, 5 Nov 2023 09:24:08 -0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=93=9C=20docs:=20update=20docs=20and?= =?UTF-8?q?=20command=20to=20get=20last=20rate=20of=20a=20currency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Wesley Werneck --- README.md | 19 +++++++++---------- main.go | 10 +++++++--- pkg/client.go | 9 ++++++--- pkg/lib.go | 16 +++++++++++----- pkg/lib_test.go | 13 ++++++++++++- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 1317ef8..6df10bd 100644 --- a/README.md +++ b/README.md @@ -24,28 +24,27 @@ Usage of brl-rate-get: To get a range of closing currency rates ``` -## To use +## How to use the -get command -To get yesterday close of a currency +To get last rate of a currency ```bash brl-rate-get -get usd - -2023/11/04 21:27:27 Found 5 records of price.. -2023/11/04 21:27:27 Found 1 records of fechamento price.. +2023/11/05 09:17:38 Found 5 records of price.. ____________________________________ |#######====================#######| -|# BRL USD #| +|# BRL eur #| |# ==== #| -|# Buy: 4.8904 #| -|# 1,00 Sell: 4.8912 #| +|# Buy: 5.2474 #| +|# 1,00 Sell: 5.2500 #| |# #| |##==============================##| ------------------------------------- +| Rate Date 2023-11-03 | + ---------------------------------- ``` -## To use +## How to use the -range command To get a range of data from April first until April 10 use diff --git a/main.go b/main.go index 37eef80..675a6f9 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "strings" + "time" + "github.com/uelei/brl-rate-get/pkg" ) @@ -12,7 +14,9 @@ func handle_get(currency string) { yesterday := brlrateget.GenerateYesterdayString() - rates := brlrateget.MakeRangeRequest(yesterday, yesterday, currency) + currentTime := time.Now() + + rates := brlrateget.MakeRangeRequest(yesterday, currentTime, strings.ToUpper(currency), false) brlrateget.FormatGetResult(rates, currency) } @@ -32,7 +36,7 @@ func handle_range(args []string) { currency := args[0] - rates := brlrateget.MakeRangeRequest(start_date, end_date, strings.ToUpper(currency)) + rates := brlrateget.MakeRangeRequest(start_date, end_date, strings.ToUpper(currency), true) filename := fmt.Sprintf("%s_brl_%s-%s.csv", currency, start_date.Format("2006-01-02"), end_date.Format("2006-01-02")) brlrateget.WriteResultsToCSV(rates, filename) @@ -55,7 +59,7 @@ func main() { case *rangeCmd: handle_range(flag.Args()) case *getCmd: - handle_get(strings.ToUpper(flag.Args()[0])) + handle_get(flag.Args()[0]) default: fmt.Println("You must pass a command") } diff --git a/pkg/client.go b/pkg/client.go index 14e9cee..9ef7dd6 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -18,7 +18,7 @@ func format_url(base_url string, currency string, start_data string, end_data st return formatedUrl } -func MakeRangeRequest(startDate time.Time, endDate time.Time, currency string) []DayValues { +func MakeRangeRequest(startDate time.Time, endDate time.Time, currency string, filter bool) []DayValues { formatedUrl := format_url("", currency, FormatDateToBC(startDate), FormatDateToBC(endDate)) resp, err := GetDataFromUrl(formatedUrl) @@ -31,16 +31,19 @@ func MakeRangeRequest(startDate time.Time, endDate time.Time, currency string) [ if err := json.Unmarshal(resp, &result); err != nil { panic(err) } + log.Printf("Found %d records of price..\n", len(result.Value)) filtered := []DayValues{} for i := range result.Value { - if result.Value[i].TipoBoletim == "Fechamento" { + if (result.Value[i].TipoBoletim == "Fechamento") || (filter == false) { filtered = append(filtered, DayValues{CotacaoCompra: result.Value[i].CotacaoCompra, CotacaoVenda: result.Value[i].CotacaoVenda, DataHoraCotacao: result.Value[i].DataHoraCotacao}) } } - log.Printf("Found %d records of fechamento price..\n", len(filtered)) + if filter { + log.Printf("Found %d records of fechamento price..\n", len(filtered)) + } return filtered } diff --git a/pkg/lib.go b/pkg/lib.go index 49ee414..bcb0c13 100644 --- a/pkg/lib.go +++ b/pkg/lib.go @@ -14,7 +14,7 @@ func GenerateYesterdayString() time.Time { currentTime := time.Now() // Calculate yesterday's date - yesterday := currentTime.AddDate(0, 0, -1) + yesterday := currentTime.AddDate(0, 0, -2) return yesterday } @@ -56,21 +56,27 @@ func ParseStringToDate(dateString string) time.Time { fmt.Println(error) panic(error) } - return date } +func PadNumberWithZero(value float64) string { + return fmt.Sprintf("%.4f", value) +} + func FormatGetResult(rates []DayValues, currency string) { + rate := rates[len(rates)-1] + fmt.Println("") fmt.Println("____________________________________") fmt.Println("|#######====================#######|") fmt.Println("|# BRL ", currency, " #|") fmt.Println("|# ==== #|") - fmt.Println("|# Buy: ", rates[0].CotacaoCompra, " #|") - fmt.Println("|# 1,00 Sell: ", rates[0].CotacaoVenda, " #|") + fmt.Println("|# Buy: ", PadNumberWithZero(rate.CotacaoCompra), " #|") + fmt.Println("|# 1,00 Sell: ", PadNumberWithZero(rate.CotacaoVenda), " #|") fmt.Println("|# #|") fmt.Println("|##==============================##|") - fmt.Println("------------------------------------") + fmt.Println("| Rate Date ", rate.DataHoraCotacao[:10] , " |") + fmt.Println(" ---------------------------------- ") } diff --git a/pkg/lib_test.go b/pkg/lib_test.go index 2c0ee77..f14f4f1 100644 --- a/pkg/lib_test.go +++ b/pkg/lib_test.go @@ -4,6 +4,7 @@ import ( "os" "testing" "time" + lib "github.com/uelei/brl-rate-get/pkg" ) @@ -41,7 +42,7 @@ func TestGenerateYesterdayString(t *testing.T) { currentTime := time.Now() - if result.Day() != currentTime.Day()-1 { + if result.Day() != currentTime.Day()-2 { t.Fatal("error creating yesterday date") } } @@ -63,3 +64,13 @@ func TestParseStringToDate(t *testing.T) { } } + +func TestPadNumberWithZero(t *testing.T) { + + value := lib.PadNumberWithZero(3.1415) + + if value != "3.1415" { + t.Fatal("Error padding string", value) + } + +} From 316f6d9b718a340b21d1f05e5337e20a42ad1c7b Mon Sep 17 00:00:00 2001 From: Wesley Werneck Date: Sun, 5 Nov 2023 09:27:06 -0300 Subject: [PATCH 2/3] fix currency to uppercase --- README.md | 2 +- main.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6df10bd..1378c3e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ brl-rate-get -get usd ____________________________________ |#######====================#######| -|# BRL eur #| +|# BRL EUR #| |# ==== #| |# Buy: 5.2474 #| |# 1,00 Sell: 5.2500 #| diff --git a/main.go b/main.go index 675a6f9..a26ec80 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,9 @@ func handle_get(currency string) { currentTime := time.Now() - rates := brlrateget.MakeRangeRequest(yesterday, currentTime, strings.ToUpper(currency), false) + currency = strings.ToUpper(currency) + + rates := brlrateget.MakeRangeRequest(yesterday, currentTime, currency, false) brlrateget.FormatGetResult(rates, currency) } From 60e5f41cc2a00ca7abed7242f0f26f7ed4974cdf Mon Sep 17 00:00:00 2001 From: Wesley Werneck Date: Sun, 5 Nov 2023 09:34:08 -0300 Subject: [PATCH 3/3] fix lint issue --- pkg/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/client.go b/pkg/client.go index 9ef7dd6..05397cb 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -37,7 +37,7 @@ func MakeRangeRequest(startDate time.Time, endDate time.Time, currency string, f filtered := []DayValues{} for i := range result.Value { - if (result.Value[i].TipoBoletim == "Fechamento") || (filter == false) { + if (result.Value[i].TipoBoletim == "Fechamento") || (!filter) { filtered = append(filtered, DayValues{CotacaoCompra: result.Value[i].CotacaoCompra, CotacaoVenda: result.Value[i].CotacaoVenda, DataHoraCotacao: result.Value[i].DataHoraCotacao}) } }