Skip to content

Commit

Permalink
Added Fixer Currency Rates backend
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentl committed Nov 2, 2020
1 parent 21d71a6 commit 6e814cc
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/Finance/Quote/CurrencyRates/AlphaVantage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ https://www.alphavantage.co requires users to register and obtain an API key,
which is also called a token.
The API key may be set by either providing a alphavantage hash inside the
currency-rate hash to Finance::Quote->new as in the above example, or by
currency_rates hash to Finance::Quote->new as in the above example, or by
setting the environment variable ALPHAVANTAGE_API_KEY.
=head1 Terms & Conditions
Any use of https://www.alphavantage.co is governed by any terms & conditions
of that site.
Use of https://www.alphavantage.co is governed by any terms & conditions of
that site.
Finance::Quote is released under the GNU General Public License, version 2,
which explicitly carries a "No Warranty" clause.
Expand Down
4 changes: 2 additions & 2 deletions lib/Finance/Quote/CurrencyRates/ECB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ of the Finance::Quote object after the first currency conversion.
=head1 Terms & Conditions
Any use of https://www.ecb.europa.eu is governed by any terms & conditions
of that site.
Use of https://www.ecb.europa.eu is governed by any terms & conditions of that
site.
Finance::Quote is released under the GNU General Public License, version 2,
which explicitly carries a "No Warranty" clause.
Expand Down
121 changes: 121 additions & 0 deletions lib/Finance/Quote/CurrencyRates/Fixer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/perl -w

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA

package Finance::Quote::CurrencyRates::Fixer;

# VERSION

use constant DEBUG => $ENV{DEBUG};
use if DEBUG, Smart::Comments;

use strict;
use JSON;

sub new
{
my $self = shift;
my $class = ref($self) || $self;

my $this = {};
bless $this, $class;

my $args = shift;

### Fixer->new args : $args

return undef unless (ref $args eq 'HASH') and (exists $args->{API_KEY});

$this->{API_KEY} = $args->{API_KEY};
$this->{refresh} = 0;
$this->{refresh} = not $args->{cache} if exists $args->{cache};

return $this;
}

sub multipliers
{
my ($this, $ua, $from, $to) = @_;

if ($this->{refresh} or not exists $this->{cache}) {
my $url = "http://data.fixer.io/api/latest?access_key=$this->{API_KEY}";
my $reply = $ua->get($url);

return undef unless ($reply->code == 200);

my $body = $reply->content;

my $json_data = JSON::decode_json $body;
if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) {
### fixer error : $json_data->{error}->{info}
return undef;
}

$this->{cache} = $json_data->{rates};
### Fixer rates: $this->{cache}
}


if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
### from : $from, $this->{cache}->{$from}
### to : $to, $this->{cache}->{$to}
return ($this->{cache}->{$from}, $this->{cache}->{$to});
}

### At least one code not found: $from, $to

return undef;
}

1;

=head1 NAME
Finance::Quote::CurrencyRates::Fixer - Obtain currency rates from https://fixer.io
=head1 SYNOPSIS
use Finance::Quote;
$q = Finance::Quote->new(currency_rates => {order => ['Fixer'],
fixer => {API_KEY => ...}});
$value = $q->currency('18.99 EUR', 'USD');
=head1 DESCRIPTION
This module fetches currency rates from https://fixer.io and provides data to
Finance::Quote to convert the first argument to the equivalent value in the
currency indicated by the second argument.
Thie module caches the currency rates for the lifetime of the quoter object,
unless 'cache => 0' is included in the 'fixer' options hash.
=head1 API_KEY
https://fixer.io requires users to register and obtain an API key.
The API key must be set by providing a 'fixer' hash inside the 'currency_rates'
hash to Finance::Quote->new as in the above example.
=head1 Terms & Conditions
Use of https://fixer.io is governed by any terms & conditions of that site.
Finance::Quote is released under the GNU General Public License, version 2,
which explicitly carries a "No Warranty" clause.
=cut
2 changes: 1 addition & 1 deletion t/currency.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ subtest 'Fixer' => sub {
my @valid = (['100.00 USD', 'EUR'], ['1.00 GBP', 'IDR'], ['1.23 IDR', 'CAD'], ['10.00 AUD', 'AUD']);
my @invalid = (['20.12 ZZZ', 'GBP']);

module_check('Fixer', \@valid, \@invalid);
module_check('Fixer', \@valid, \@invalid, {cache => 1, API_KEY => $ENV{TEST_FIXER_API_KEY}});
};

0 comments on commit 6e814cc

Please sign in to comment.