rates

package module
v0.0.0-...-e43a5fe Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2016 License: MIT Imports: 3 Imported by: 0

README

Exchange rates provider

A package used to manage exchange rates from any provider

GoDoc

Examples

Get all exchange rates for the ECB provider

package main

import (
	"fmt"

	"github.com/openprovider/rates"
	"github.com/openprovider/rates/providers"
)

func main() {
	service := rates.New(
		// any collection of providers which implement rates.Provider interface
		providers.NewECBProvider(new(rates.Options)),
	)
	rates, errors := service.FetchLast()
	if len(errors) != 0 {
		fmt.Println(errors)
	}
	fmt.Println(service.Name(), "exchange rates for today")
	for index, rate := range rates {
		fmt.Printf("%d. %s - %v\r\n", index+1, rate.Currency, rate.Value)
	}
}

Get exchange rates for EUR, USD, CHF, HKD

package main

import (
	"fmt"

	"github.com/openprovider/rates"
	"github.com/openprovider/rates/providers"
)

func main() {
	registry := rates.Registry{
		// any collection of providers which implement rates.Provider interface
		providers.NewECBProvider(
			&rates.Options{
				Currencies: []string{
					providers.EUR,
					providers.USD,
					providers.CHF,
					providers.HKD,
				},
			},
		),
	}
	rates, errors := registry.FetchLast()
	if len(errors) != 0 {
		fmt.Println(errors)
	}
	fmt.Println(registry.Name(), "exchange rates for today")
	for index, rate := range rates {
		fmt.Printf("%d. %s - %v\r\n", index+1, rate.Currency, rate.Value)
	}
}

Authors

Igor Dolzhikov

Contributors

All the contributors are welcome. If you would like to be the contributor please accept some rules.

  • The pull requests will be accepted only in "develop" branch
  • All modifications or additions should be tested

Thank you for your understanding!

License

MIT Public License

Documentation

Overview

Package rates 0.2.2 This package helps to manage exchange rates from any provider

Example 1: Get all exchange rates for the ECB Provider

package main

import (
    "fmt"

    "github.com/openprovider/rates"
    "github.com/openprovider/rates/providers"
)

func main() {
    service := rates.New(
        // any collection of providers which implement rates.Provider interface
        providers.NewECBProvider(new(rates.Options)),
    )
    rates, errors := service.FetchLast()
    if len(errors) != 0 {
        fmt.Println(errors)
    }
    fmt.Println("European Central Bank exchange rates for today")
    for index, rate := range rates {
        fmt.Printf("%d. %s - %v\r\n", index+1, rate.Currency, rate.Value)
    }
}

Example 2: Get exchange rates for EUR, USD, CHF, HKD

package main

import (
    "fmt"

    "github.com/openprovider/rates"
    "github.com/openprovider/rates/providers"
)

func main() {
    registry := rates.Registry{
        // any collection of providers which implement rates.Provider interface
        providers.NewECBProvider(
            &rates.Options{
                Currencies: []string{
                    providers.EUR,
                    providers.USD,
                    providers.CHF,
                    providers.HKD,
                },
            },
        ),
    }
    rates, errors := registry.FetchLast()
    if len(errors) != 0 {
        fmt.Println(errors)
    }
    fmt.Println("European Central Bank exchange rates for today")
    for index, rate := range rates {
        fmt.Printf("%d. %s - %v\r\n", index+1, rate.Currency, rate.Value)
    }
}

Exchange Rates Provider

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// API key/token
	Token string
	// List of the currencies which need to get from the provider
	// If it is empty, should get all of existing currencies from the provider
	Currencies []string
	// Flexible settings list
	Settings map[string]interface{}
}

Options is some specific things for the specific provider It should configure the provider to manage currencies

type Provider

type Provider interface {
	Name() string
	FetchLast() (rates []Rate, errors []error)
	FetchHistory() (rates []Rate, errors []error)
}

Provider holds methods for providers which implement this interface

func New

func New(providers ...Provider) Provider

New service which contains registered providers

type Rate

type Rate struct {
	ID       uint64        `json:"id,omitempty"`
	Date     string        `json:"date"`
	Currency string        `json:"currency"`
	Time     time.Time     `json:"-"`
	Base     currency.Unit `json:"-"`
	Unit     currency.Unit `json:"-"`
	Value    interface{}   `json:"value"`
}

Rate represent date and currency exchange rates

type Registry

type Registry []Provider

Registry contains registered providers

func (Registry) FetchHistory

func (registry Registry) FetchHistory() (rates []Rate, errors []error)

FetchHistory returns exchange rates from all registered providers from history

func (Registry) FetchLast

func (registry Registry) FetchLast() (rates []Rate, errors []error)

FetchLast returns exchange rates from all registered providers on last day

func (Registry) Name

func (registry Registry) Name() string

Name returns name of the provider

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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