| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 ic noai expandtab showmode showmatch: |
| 3 |
|
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301, USA |
| 18 |
|
| 19 |
# Copyright (C) 2026, Bruce Schuck <[email protected]> |
| 20 |
|
| 21 |
# Changes: |
| 22 |
# 2026-05-17 - Initial version. Base code copied from |
| 23 |
# CurrencyRates/TwelveData.pm |
| 24 |
|
| 25 |
package Finance::Quote::CurrencyRates::Frankfurter; |
| 26 |
|
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
|
| 30 |
use constant DEBUG => $ENV{DEBUG}; |
| 31 |
use if DEBUG, 'Smart::Comments'; |
| 32 |
|
| 33 |
use JSON; |
| 34 |
|
| 35 |
our $VERSION = '1.71'; # VERSION |
| 36 |
|
| 37 |
my $FRANKFURTER_URL_HEAD = 'https://api.frankfurter.dev/v2/rate/'; |
| 38 |
|
| 39 |
sub new |
| 40 |
{ |
| 41 |
my $self = shift; |
| 42 |
my $class = ref($self) || $self; |
| 43 |
|
| 44 |
my $this = {}; |
| 45 |
bless $this, $class; |
| 46 |
|
| 47 |
my $args = shift; |
| 48 |
|
| 49 |
### Frankfurter->new args : $args |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
sub multipliers |
| 55 |
{ |
| 56 |
my ($this, $ua, $from, $to) = @_; |
| 57 |
|
| 58 |
my $json_data; |
| 59 |
my $rate; |
| 60 |
|
| 61 |
# Set headers. |
| 62 |
my @ua_headers = ( |
| 63 |
'Accept' => 'application/json', |
| 64 |
'User-Agent' => 'finance-quote-frankfurter/1.0', |
| 65 |
); |
| 66 |
|
| 67 |
my $reply = $ua->get($FRANKFURTER_URL_HEAD |
| 68 |
. ${from} |
| 69 |
. '/' |
| 70 |
. ${to} |
| 71 |
, @ua_headers); |
| 72 |
|
| 73 |
### URL: $FRANKFURTER_URL_HEAD . ${from} . '/' . ${to} |
| 74 |
|
| 75 |
### HTTP Status: $reply->code |
| 76 |
return unless ($reply->code == 200); |
| 77 |
|
| 78 |
my $body = $reply->content; |
| 79 |
|
| 80 |
$json_data = JSON::decode_json $body; |
| 81 |
|
| 82 |
### JSON: $json_data |
| 83 |
|
| 84 |
if ( !$json_data || !$json_data->{'rate'} ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$rate = |
| 89 |
$json_data->{'rate'}; |
| 90 |
|
| 91 |
### Rate from JSON: $rate |
| 92 |
|
| 93 |
return unless $rate + 0; |
| 94 |
|
| 95 |
# For small rates, request the inverse |
| 96 |
if ($rate < 0.001) { |
| 97 |
### Rate is too small, requesting inverse : $rate |
| 98 |
my ($a, $b) = $this->multipliers($ua, $to, $from); |
| 99 |
return ($b, $a); |
| 100 |
} |
| 101 |
|
| 102 |
return (1.0, $rate); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
1; |
| 107 |
|
| 108 |
=head1 NAME |
| 109 |
|
| 110 |
Finance::Quote::CurrencyRates::Frankfurter - Obtain currency rates from |
| 111 |
https://api.frankfurter.dev/v2/rate/ |
| 112 |
|
| 113 |
=head1 SYNOPSIS |
| 114 |
|
| 115 |
use Finance::Quote; |
| 116 |
|
| 117 |
$q = Finance::Quote->new(currency_rates => {order => ['Frankfurter']}); |
| 118 |
|
| 119 |
$value = $q->currency('18.99 EUR', 'USD'); |
| 120 |
|
| 121 |
=head1 DESCRIPTION |
| 122 |
|
| 123 |
This module fetches currency rates from |
| 124 |
https://api.frankfurter.dev/v2/rate/... provides data to Finance::Quote |
| 125 |
to convert the first argument to the equivalent value in the currency |
| 126 |
indicated by the second argument. |
| 127 |
|
| 128 |
This module is not the default currency conversion module for a Finance::Quote |
| 129 |
object. |
| 130 |
It can be utilized by setting the environment variable FQ_CURRENCY=Frankfurter. |
| 131 |
|
| 132 |
=head1 API_KEY |
| 133 |
|
| 134 |
Currently, https://api.frankfurter.dev/v2/rate/... does not require an |
| 135 |
API Key. |
| 136 |
|
| 137 |
=head1 Terms & Conditions |
| 138 |
|
| 139 |
Use of https://api.frankfurter.dev/v2/rate is |
| 140 |
governed by any terms & conditions of that site. |
| 141 |
|
| 142 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 143 |
which explicitly carries a "No Warranty" clause. |
| 144 |
|
| 145 |
=cut |