Skip to content

Commit

Permalink
Merge pull request #11 from uelei/feat/update-docs
Browse files Browse the repository at this point in the history
📜 docs: update docs and command to get last rate of a currency
  • Loading branch information
uelei authored Nov 5, 2023
2 parents 272c0b1 + 60e5f41 commit 869d80d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/uelei/brl-rate-get/pkg"
)

func handle_get(currency string) {

yesterday := brlrateget.GenerateYesterdayString()

rates := brlrateget.MakeRangeRequest(yesterday, yesterday, currency)
currentTime := time.Now()

currency = strings.ToUpper(currency)

rates := brlrateget.MakeRangeRequest(yesterday, currentTime, currency, false)

brlrateget.FormatGetResult(rates, currency)
}
Expand All @@ -32,7 +38,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)
Expand All @@ -55,7 +61,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")
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
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
}
16 changes: 11 additions & 5 deletions pkg/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(" ---------------------------------- ")

}
13 changes: 12 additions & 1 deletion pkg/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"
"time"

lib "github.com/uelei/brl-rate-get/pkg"
)

Expand Down Expand Up @@ -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")
}
}
Expand All @@ -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)
}

}

0 comments on commit 869d80d

Please sign in to comment.