rates

package
v0.0.0-...-554626f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2017 License: AGPL-3.0 Imports: 19 Imported by: 0

README

rates

GoDoc

The package contains methods to get currencies exchange rates from Russian Central Bank https://www.cbr.ru.

cfg, err := New(getConfig(), logger)
if err != nil {
        log.Fatal(err)
}
// currencies aliases
requiredCodes := map[string][]string{
        "usd": {"$", "dollar"},
        "eur": {"€", "euro"},
}
err = cfg.SetRequiredCodes(requiredCodes)
if err != nil {
        log.Fatal(err)
}
info, err := cfg.GetRates(d, "15.5 euro, 100$")
if err != nil {
        log.Fatal(err)
}
// info.Rates
// [{15.5 euro map[eur:15.5 usd:16.19]}, {100$ map[usd:100 eur:95.77]}]

Documentation

Overview

Package rates contains methods to get currencies exchange rates from Russian Central Bank https://www.cbr.ru

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cfg

type Cfg struct {
	Host      string `json:"host"`
	Port      uint   `json:"port"`
	CacheSize int    `json:"cache"`
	Timeout   int64  `json:"timeout"`
	Debug     bool   `json:"debug"`
	// contains filtered or unexported fields
}

Cfg is rates' configuration settings.

func New

func New(filename string, logger *log.Logger, userAgent string) (*Cfg, error)

New returns new rates configuration.

func (*Cfg) Addr

func (c *Cfg) Addr() string

Addr returns service's net address.

func (*Cfg) GetCodes

func (c *Cfg) GetCodes() ([]CodeItem, error)

GetCodes returns available currencies codes.

func (*Cfg) GetRates

func (c *Cfg) GetRates(date time.Time, msg string) (*Info, error)

GetRates returns currencies rates info.

func (*Cfg) HandleTimeout

func (c *Cfg) HandleTimeout() time.Duration

HandleTimeout is service timeout.

func (*Cfg) SetRequiredCodes

func (c *Cfg) SetRequiredCodes(codeNames map[string][]string) error

SetRequiredCodes sets required currencies char codes and their aliases. For example, {"USD": ["$", "dollar"], "RUB": ["руб", "rubles"]}

type CodeItem

type CodeItem struct {
	ID         string `xml:"ID,attr"`
	Name       string `xml:"Name"`
	EngName    string `xml:"EngName"`
	Nominal    uint   `xml:"Nominal"`
	ParentCode string `xml:"ParentCode"`
}

CodeItem is currency code XML item.

type CurrencyItem

type CurrencyItem struct {
	ID       string `xml:"ID,attr"`
	NumCode  string `xml:"NumCode"`
	CharCode string `xml:"CharCode"`
	Nominal  uint   `xml:"Nominal"`
	Name     string `xml:"Name"`
	Value    string `xml:"Value"`
}

CurrencyItem is currency rate info.

type Info

type Info struct {
	Date  string     `json:"date"`
	Rates []RateItem `json:"rates"`
}

Info is rates' JSON struct response

func (*Info) String

func (i *Info) String() string

String returns string representation Info value.

type RateError

type RateError struct {
	HTTPCode int
	Msg      string
}

RateError is error type during rates getting.

func (*RateError) Error

func (r *RateError) Error() string

Error returns error message of RateError struct.

type RateItem

type RateItem struct {
	Msg  string             `json:"msg"`
	Rate map[string]float64 `json:"rate"`
}

RateItem is exchange rate item.

type ResponseCodes

type ResponseCodes struct {
	XMLName xml.Name   `xml:"Valuta"`
	Items   []CodeItem `xml:"Item"`
}

ResponseCodes is XML codes response.

type ResponseRates

type ResponseRates struct {
	XMLName xml.Name       `xml:"ValCurs"`
	Items   []CurrencyItem `xml:"Valute"`
}

ResponseRates is XML rates response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL