Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaskulski committed Nov 28, 2020
1 parent 07766eb commit ae7f138
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 447 deletions.
47 changes: 24 additions & 23 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package nbpapi
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -83,7 +82,7 @@ func (c *NBPCurrency) CurrencyRaw(dFlag string, lFlag int, cFlag string, repForm
address := getCurrencyAddress(c.tableType, dFlag, lFlag, cFlag)
c.result, err = getData(address, repFormat)
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -97,7 +96,7 @@ func (c *NBPCurrency) CurrencyByDate(dFlag string, cFlag string) error {
address := getCurrencyAddress(c.tableType, dFlag, 0, cFlag)
c.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return err
}

if c.tableType != "C" {
Expand All @@ -106,7 +105,7 @@ func (c *NBPCurrency) CurrencyByDate(dFlag string, cFlag string) error {
err = json.Unmarshal(c.result, &c.ExchangeC)
}
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -120,7 +119,7 @@ func (c *NBPCurrency) CurrencyLast(cFlag string, lFlag int) error {
address := getCurrencyAddress(c.tableType, "", lFlag, cFlag)
c.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return err
}

if c.tableType != "C" {
Expand All @@ -129,7 +128,7 @@ func (c *NBPCurrency) CurrencyLast(cFlag string, lFlag int) error {
err = json.Unmarshal(c.result, &c.ExchangeC)
}
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -143,7 +142,7 @@ func (c *NBPCurrency) CurrencyToday(cFlag string) error {
address := getCurrencyAddress(c.tableType, "today", 0, cFlag)
c.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return err
}

if c.tableType != "C" {
Expand All @@ -152,7 +151,7 @@ func (c *NBPCurrency) CurrencyToday(cFlag string) error {
err = json.Unmarshal(c.result, &c.ExchangeC)
}
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -162,11 +161,12 @@ func (c *NBPCurrency) CurrencyToday(cFlag string) error {
// and return Rate struct (or error)
func (c *NBPCurrency) GetRateCurrent(cFlag string) (Rate, error) {
var err error
var rate Rate

address := getCurrencyAddress(c.tableType, "current", 0, cFlag)
c.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return rate, err
}

if c.tableType != "C" {
Expand All @@ -175,10 +175,9 @@ func (c *NBPCurrency) GetRateCurrent(cFlag string) (Rate, error) {
err = json.Unmarshal(c.result, &c.ExchangeC)
}
if err != nil {
log.Fatal(err)
return rate, err
}

var rate Rate
if c.tableType != "C" {
rate.No = c.Exchange.Rates[0].No
rate.EffectiveDate = c.Exchange.Rates[0].EffectiveDate
Expand All @@ -200,11 +199,12 @@ func (c *NBPCurrency) GetRateCurrent(cFlag string) (Rate, error) {
// and returns Rate struct (or error)
func (c *NBPCurrency) GetRateToday(cFlag string) (Rate, error) {
var err error
var rate Rate

address := getCurrencyAddress(c.tableType, "today", 0, cFlag)
c.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return rate, err
}

if c.tableType != "C" {
Expand All @@ -213,10 +213,9 @@ func (c *NBPCurrency) GetRateToday(cFlag string) (Rate, error) {
err = json.Unmarshal(c.result, &c.ExchangeC)
}
if err != nil {
log.Fatal(err)
return rate, err
}

var rate Rate
if c.tableType != "C" {
rate.No = c.Exchange.Rates[0].No
rate.EffectiveDate = c.Exchange.Rates[0].EffectiveDate
Expand All @@ -238,11 +237,8 @@ func (c *NBPCurrency) GetRateToday(cFlag string) (Rate, error) {
// and returns Rate struct (or error)
func (c *NBPCurrency) GetRateByDate(code string, date string) ([]Rate, error) {
var err error

err = CheckArg("currency", c.tableType, date, 0, "table", code)
if err != nil {
return nil, err
}
var rates []Rate
var rate Rate

address := getCurrencyAddress(c.tableType, date, 0, code)
c.result, err = getData(address, "json")
Expand All @@ -259,8 +255,6 @@ func (c *NBPCurrency) GetRateByDate(code string, date string) ([]Rate, error) {
return nil, err
}

var rates []Rate
var rate Rate
if c.tableType != "C" {
for _, item := range c.Exchange.Rates {
rate.No = item.No
Expand Down Expand Up @@ -288,10 +282,14 @@ func (c *NBPCurrency) GetRateByDate(code string, date string) ([]Rate, error) {
// depending on the tableType field:
// for type A and B tables a column with an average rate is printed,
// for type C two columns: buy price and sell price
func (c *NBPCurrency) GetPrettyOutput() string {
func (c *NBPCurrency) GetPrettyOutput(lang string) string {
const padding = 3
var builder strings.Builder
var output string

// output language
setLang(lang)

w := tabwriter.NewWriter(&builder, 0, 0, padding, ' ', tabwriter.Debug)

if c.tableType != "C" {
Expand Down Expand Up @@ -331,9 +329,12 @@ func (c *NBPCurrency) GetPrettyOutput() string {
// in the form of CSV (data separated by a comma), depending on the
// tableType field: for type A and B tables a column with an average
// rate is printed, for type C two columns: buy price and sell price
func (c *NBPCurrency) GetCSVOutput() string {
func (c *NBPCurrency) GetCSVOutput(lang string) string {
var output string = ""

// output language
setLang(lang)

if c.tableType != "C" {
output += fmt.Sprintln(l.Get("TABLE,DATE,AVERAGE (PLN)"))
for _, currencyItem := range c.Exchange.Rates {
Expand Down
35 changes: 16 additions & 19 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package main

import (
"fmt"
"log"

"github.com/pjaskulski/nbpapi"
)
Expand All @@ -21,13 +20,13 @@ func main() {
nbpTable := nbpapi.NewTable("A")
err = nbpTable.TableByDate("2020-11-12")
if err != nil {
log.Fatal(err)
}

for _, item := range nbpTable.Exchange {
tableNo = item.No
for _, currencyItem := range item.Rates {
fmt.Println(tableNo, currencyItem.Code, currencyItem.Currency, currencyItem.Mid)
fmt.Println(err)
} else {
for _, item := range nbpTable.Exchange {
tableNo = item.No
for _, currencyItem := range item.Rates {
fmt.Println(tableNo, currencyItem.Code, currencyItem.Currency, currencyItem.Mid)
}
}
}
fmt.Println()
Expand All @@ -37,11 +36,11 @@ func main() {
tableA, err = nbpTable.GetTableCurrent()
if err != nil {
fmt.Println(err)
}

for _, tItem := range tableA {
for _, item := range tItem.Rates {
fmt.Println(tItem.No, item.Code, item.Currency, item.Mid)
} else {
for _, tItem := range tableA {
for _, item := range tItem.Rates {
fmt.Println(tItem.No, item.Code, item.Currency, item.Mid)
}
}
}
fmt.Println()
Expand All @@ -51,19 +50,17 @@ func main() {
nbpMid := nbpapi.NewCurrency("A")
err = nbpMid.CurrencyLast("CHF", 5)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}

output := nbpMid.GetCSVOutput()
fmt.Println(output)
// english version
fmt.Println(nbpMid.GetCSVOutput("en"))
fmt.Println()

// polish version
nbpapi.SetLang("pl")
fmt.Println(nbpMid.GetCSVOutput())
fmt.Println(nbpMid.GetCSVOutput("pl"))
fmt.Println()

nbpapi.SetLang("en")
// how to get today's rate of CHF, table C (bid, ask - buy and sell
// exchange rate), function GetCurencyToday, returns error or nil and
// populates slice ExchangeC, the currency rate is taken from the first
Expand Down
40 changes: 20 additions & 20 deletions gold.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package nbpapi
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -49,7 +48,7 @@ func (g *NBPGold) GoldRaw(dFlag string, lFlag int, repFormat string) error {
address := getGoldAddress(dFlag, lFlag)
g.result, err = getData(address, repFormat)
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -63,12 +62,12 @@ func (g *NBPGold) GoldByDate(dFlag string) error {
address := getGoldAddress(dFlag, 0)
g.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return err
}

err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -82,12 +81,12 @@ func (g *NBPGold) GoldLast(lFlag int) error {
address := getGoldAddress("", lFlag)
g.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return err
}

err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
log.Fatal(err)
return err
}

return err
Expand All @@ -100,12 +99,12 @@ func (g *NBPGold) GetPriceToday() (GoldRate, error) {
address := getGoldAddress("today", 0)
g.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return GoldRate{}, err
}

err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
log.Fatal(err)
return GoldRate{}, err
}

return g.GoldRates[0], err
Expand All @@ -118,12 +117,12 @@ func (g *NBPGold) GetPriceCurrent() (GoldRate, error) {
address := getGoldAddress("current", 0)
g.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return GoldRate{}, err
}

err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
log.Fatal(err)
return GoldRate{}, err
}

return g.GoldRates[0], err
Expand All @@ -134,30 +133,28 @@ func (g *NBPGold) GetPriceCurrent() (GoldRate, error) {
func (g *NBPGold) GetPriceByDate(date string) ([]GoldRate, error) {
var err error

err = CheckArg("gold", "", date, 0, "table", "")
if err != nil {
return nil, err
}

address := getGoldAddress(date, 0)
g.result, err = getData(address, "json")
if err != nil {
log.Fatal(err)
return nil, err
}

err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
log.Fatal(err)
return nil, err
}

return g.GoldRates, err
}

// GetPrettyOutput - function returns a formatted table of gold prices
func (g *NBPGold) GetPrettyOutput() string {

func (g *NBPGold) GetPrettyOutput(lang string) string {
const padding = 3
var builder strings.Builder

// output language
setLang(lang)

w := tabwriter.NewWriter(&builder, 0, 0, padding, ' ', tabwriter.Debug)

fmt.Fprintln(w)
Expand All @@ -177,9 +174,12 @@ func (g *NBPGold) GetPrettyOutput() string {

// GetCSVOutput - function returns prices of gold in CSV format
// (comma separated data)
func (g *NBPGold) GetCSVOutput() string {
func (g *NBPGold) GetCSVOutput(lang string) string {
var output string = ""

// output language
setLang(lang)

output += fmt.Sprintln(l.Get("DATE,PRICE (PLN)"))
for _, goldItem := range g.GoldRates {
goldValue := fmt.Sprintf("%.4f", goldItem.Price)
Expand Down
Loading

0 comments on commit ae7f138

Please sign in to comment.