Skip to content

Commit

Permalink
Fix for changed page layout (HTML) (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanglie committed Jul 3, 2023
1 parent 4cdfeda commit 5fcc12c
Show file tree
Hide file tree
Showing 7 changed files with 742 additions and 455 deletions.
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Test](https://github.com/ivanglie/go-br-client/actions/workflows/test.yml/badge.svg)](https://github.com/ivanglie/go-br-client/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/ivanglie/go-br-client/branch/master/graph/badge.svg?token=8lRyze5RSQ)](https://codecov.io/gh/ivanglie/go-br-client)

Golang client that provides latest exchange rate of cash currency in largest cities of Russia.
Golang client that provides latest exchange rate of USDRUB cash in largest cities of Russia.

## Example

Expand All @@ -14,7 +14,7 @@ First, ensure the library is installed and up to date by running
go get -u github.com/ivanglie/go-br-client
```

This is a very simple app that just displays exhange rate of Chinese Yuan Renminbi in Novosibirsk.
This is a very simple app that just displays USDRUB exhange rate in Novosibirsk.

```golang
package main
Expand All @@ -27,7 +27,7 @@ import (

func main() {
client := br.NewClient()
rates, err := client.Rates(br.CNY, br.Novosibirsk)
rates, err := client.Rates(br.Novosibirsk)
if err != nil {
panic(err)
}
Expand All @@ -39,17 +39,32 @@ Console output:

```json
{
"currency": "CNY",
"currency": "USD",
"city": "novosibirsk",
"branches": [
{
"bank": "Банк «Открытие»",
"address": "630102, г. Новосибирск, ул. Кирова, дом. 44",
"subway": "м. Октябрьская",
"currency": "CNY",
"buy": 9.61,
"sell": 11.64,
"updated": "2023-01-24T16:54:00+03:00"
"bank": "ОП № 029/0000 Филиала \"Газпромбанк\" АО",
"subway": "Заельцовская, Гагаринская, Сибирская",
"currency": "USD",
"buy": 87.1,
"sell": 91.3,
"updated": "2023-07-03T13:00:00+03:00"
},
{
"bank": "ДО № 029/1007 Филиала \"Газпромбанк\" АО",
"subway": "Заельцовская, Берёзовая роща, Гагаринская",
"currency": "USD",
"buy": 87.1,
"sell": 91.3,
"updated": "2023-07-03T13:00:00+03:00"
},
{
"bank": "ДО № 029/1003 Филиала \"Газпромбанк\" АО",
"subway": "Берёзовая роща, Маршала Покрышкина, Золотая Нива",
"currency": "USD",
"buy": 87.1,
"sell": 91.3,
"updated": "2023-07-03T13:00:00+03:00"
}
]
}
Expand All @@ -60,4 +75,4 @@ See [main.go](./_example/main.go).

For more information check out the following links:

* Cash currency exchange rates by [Banki.ru](https://www.banki.ru/products/currency/cash/moskva/) (RU)
* Cash currency exchange rates by [Banki.ru](https://www.banki.ru/products/currency/map/moskva/) (RU)
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
client := br.NewClient()
rates, err := client.Rates(br.CNY, br.Novosibirsk)
rates, err := client.Rates(br.Novosibirsk)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions br.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// Bank or branch.
type Branch struct {
Bank string `json:"bank"`
Address string `json:"address"`
Subway string `json:"subway"`
Currency string `json:"currency"`
Buy float64 `json:"buy"`
Expand All @@ -31,8 +30,8 @@ type Rates struct {
}

// NewBranch creates a new Branch instance.
func newBranch(bank, address, subway, currency string, buy, sell float64, updated time.Time) Branch {
return Branch{bank, address, subway, currency, buy, sell, updated}
func newBranch(bank, subway, currency string, buy, sell float64, updated time.Time) Branch {
return Branch{bank, subway, currency, buy, sell, updated}
}

// ByBuySorter implements sort.Interface based on the Buy field.
Expand Down
67 changes: 36 additions & 31 deletions br_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
)

func Test_newBranch(t *testing.T) {
got := newBranch("b", "a", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
want := newBranch("b", "a", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
got := newBranch("b", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
want := newBranch("b", "s", "c", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }())
if !reflect.DeepEqual(got, want) {
t.Errorf("newBranch() = %v, want %v", got, want)
}
}

func TestBranch_ByBuySorter(t *testing.T) {
got := []Branch{
newBranch("bank", "address", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
sort.Sort(ByBuySorter(got))

want := []Branch{
newBranch("bank", "address", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 56.78, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 90.12, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 100.23, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 101.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}

if !reflect.DeepEqual(got, want) {
Expand All @@ -41,20 +41,20 @@ func TestBranch_ByBuySorter(t *testing.T) {

func TestBranch_BySellSorter(t *testing.T) {
got := []Branch{
newBranch("bank", "address", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}
sort.Sort(BySellSorter(got))

want := []Branch{
newBranch("bank", "address", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "address", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 52.64, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.75, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 56.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 58.78, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
newBranch("bank", "subway", "currency", 12.34, 78.56, func() time.Time { t, _ := time.Parse("02.01.2006 15:04", "01.02.2018 12:35"); return t }()),
}

if !reflect.DeepEqual(got, want) {
Expand All @@ -66,27 +66,32 @@ func TestRates_String(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Moscow")
b := newBranch(
"Банк «Открытие»",
"630102, г. Новосибирск, ул. Кирова, дом. 44",
"м. Октябрьская", "CNY",
9.61,
11.64,
"м. Октябрьская",
"USD",
79.61,
81.64,
time.Date(2023, time.January, 24, 16, 54, 0, 0, loc))

r := &Rates{}
r.Currency = CNY
r.Currency = currency
r.City = Novosibirsk
r.Branches = []Branch{b}

got := r.String()
want := `{"currency":"CNY","city":"novosibirsk","branches":[{` +
`"bank":"Банк «Открытие»","address":"630102, г. Новосибирск, ул. Кирова, дом. 44","subway":"м. Октябрьская",` +
`"currency":"CNY","buy":9.61,"sell":11.64,"updated":"2023-01-24T16:54:00+03:00"}]}`
want := `{"currency":"USD","city":"novosibirsk","branches":[{` +
`"bank":"Банк «Открытие»","subway":"м. Октябрьская",` +
`"currency":"USD","buy":79.61,"sell":81.64,"updated":"2023-01-24T16:54:00+03:00"}]}`

if got != want {
t.Errorf("got= %v, want= %v", got, want)
}

// Negative case
// Errors
r.Branches[0].Buy = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}

r.Branches[0].Sell = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
Expand Down
Loading

0 comments on commit 5fcc12c

Please sign in to comment.