NAME

Finance::Currency::FiatX - Currency exchange rate library

VERSION

This document describes version 0.011 of Finance::Currency::FiatX (from Perl distribution Finance-Currency-FiatX), released on 2019-01-10.

SYNOPSIS

The easiest way to use this module is via CLI fiatx from App::fiatx. To use this module directly, first create a MySQL database, then:

use Finance::Currency::FiatX qw(get_spot_rate get_all_spot_rates);

# connect to database
my $dbh = DBI->connect(...);

# create a database# get a single spot rate from any source
my $res = get_spot_rate(dbh => $dbh, from => "USD", to => "IDR");

DESCRIPTION

FiatX is a library/application to convert one currency to another using several backend modules ("sources", Finance::Currency::FiatX::Source::*, which in turns usually utilize Finance::Currency::Convert::*) and store the rates in DBI database for caching.

FUNCTIONS

get_all_spot_rates

Usage:

get_all_spot_rates(%args) -> [status, msg, payload, meta]

Get all spot rates from a source.

This routine will retrieve all available spot rates from a source. You can either pick a specific source (e.g. bi for "Bank of Indonesia") or one of the special/"pseudo" sources: ':any', ':highest', ':lowest', 'newest', ':oldest', ':average'.

Some source provide rates for a large number of currency pairs, not all of which you might need. For retrieving rates for a specific currency pair only, use get_spot_rate.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • dbh => obj

  • max_age_cache => nonnegint (default: 14400)

    Above this age (in seconds), we retrieve rate from remote source again.

  • source* => str (default: ":any")

    Ask for a specific remote source.

    If you want a specific remote source, you can specify it here. The default is ':any' which is to pick the first source that returns a recent enough current rate.

    Other special values: :highest to return highest rate of all sources, :lowest to return lowest rate of all sources, ':newest' to return rate from source with the newest last update time, ':oldest' to return rate from source with the oldest last update time, ':average' to return arithmetic average of all sources.

  • table_prefix => str (default: "fiatx_")

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

Return value: (any)

get_spot_rate

Usage:

get_spot_rate(%args) -> [status, msg, payload, meta]

Get spot rate for a currency pair.

This routine will retrieve spot rate(s) for a specific currency pair from a source. You can either let the routine pick any one source that is new enough (':any') or pick a specific source (e.g. bi for "Bank of Indonesia"), or one of the other special/"pseudo" sources: ':highest', ':lowest', 'newest', ':oldest', ':average'.

Note that there might be multiple spot rates for a single currency pair, because there might be different types of rates, for example: "buy", "sell", or more specific types or buy and sell rates.

See also get_all_spot_rates to retrieve rates for all currency pairs available from a specific source.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • dbh => obj

  • from* => currency::code

  • max_age_cache => nonnegint (default: 14400)

    Above this age (in seconds), we retrieve rate from remote source again.

  • source => str (default: ":any")

    Ask for a specific remote source.

    If you want a specific remote source, you can specify it here. The default is ':any' which is to pick the first source that returns a recent enough current rate.

    Other special values: :highest to return highest rate of all sources, :lowest to return lowest rate of all sources, ':newest' to return rate from source with the newest last update time, ':oldest' to return rate from source with the oldest last update time, ':average' to return arithmetic average of all sources.

  • table_prefix => str (default: "fiatx_")

  • to* => currency::code

  • type => str

    Which rate is wanted? e.g. sell, buy.

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

Return value: (any)

list_rate_sources

Usage:

list_rate_sources() -> [status, msg, payload, meta]

List exchange rate sources installed on the system.

Sources are modules under the namespace Finance::Currency::FiatX::Source::*, for example the source bi ("Bank of Indonesia") is implemented in Finance::Currency::FiatX::Source::bi. This routine lists the installed sources without the namespace prefix.

This function is not exported by default, but exportable.

No arguments.

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

Return value: (any)

BUGS

Please report all bug reports or feature requests to mailto:stevenharyanto@gmail.com.

SEE ALSO

Finance::Currency::Convert::* modules.

App::fiatx and fiatx which provides CLI for this module.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019, 2018 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.