Quote/CurrencyRates/AllRatesToday.pm

filter: Statementstmt 34.4% 30 / 87 Branchbran 7.1% 3 / 42 Conditioncond 5.6% 3 / 53 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 19.2% 43 / 224 CC 44 SCAR 60.1
CC
44
Cov
20%
_bank_multipliers
756.0
multipliers
110.0
_get_json
12.0
CRAP 407.9
« Quote/IndiaMutual.pm ↑ Summary Quote/YahooWeb.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 ic noai expandtab showmode showmatch:
3  
4 # This file is part of Finance::Quote.
5 #
6 # Finance::Quote is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation, either version 2 of
9 # the License, or (at your option) any later version.
10 #
11 # Finance::Quote is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Finance::Quote.
18 # If not, see <https://www.gnu.org/licenses/>.
19  
20 # Copyright (C) 2026, Madhushan Basnayaka (AllRatesToday)
21  
22 # Changes:
23 # 2026-10-29 - Initial version. Base code copied from
24 # CurrencyRates/Frankfurter.pm
25  
26 package Finance::Quote::CurrencyRates::AllRatesToday;
27  
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 our $VERSION = '1.72_01'; # TRIAL VERSION
37  
38 1 my $ALLRATESTODAY_URL_HEAD = 'https://allratestoday.com/api/';
39  
40 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
41 1 return ('BANK', 'API_KEY');
42 }
43  
44 sub new
45 1 ▶{
Subroutine new: called CC=7 SCAR=22.0
Pod undocumented
46 1 my $self = shift;
47 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
48  
49 1 my $this = {};
50 1 bless $this, $class;
51  
52 1 my $args = shift;
53  
54 ### AllRatesToday->new args : $args
55  
56 # Optional: the code of a central bank whose official table should be
57 # used instead of the ECB reference rates, e.g. 'sama' or 'rbnz'.
58 1 $this->{BANK} = $ENV{'ALLRATESTODAY_BANK'};
59 1 ▶ $this->{BANK} = $args->{BANK} if (ref $args eq 'HASH') and (exists $args->{BANK});
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" and exists $args->{"BANK"} 0 1 1
Condition33%1/3
ref $args eq "HASH" and exists $args->{"BANK"}
!ll&&!rl&&r
010
MC/DC0%0/2
ref $args eq "HASH" and exists $args->{"BANK"}
ref $args eq "HASH" exists $args->{"BANK"}
Truth table33%1/3
A and B
A= ref $args eq "HASH"B= exists $args->{"BANK"}
ABresult
0X0
100
111
60 1 ▶ $this->{BANK} = lc($this->{BANK}) if defined $this->{BANK};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"BANK"} 0 1 1
61  
62 # Optional: an AllRatesToday API key. With a key the central-bank table is
63 # read from the keyed endpoint, which the terms allow for any use; the
64 # keyless endpoints are for non-commercial use with a visible link.
65 1 $this->{API_KEY} = $ENV{'ALLRATESTODAY_API_KEY'};
66 1 ▶ $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" and exists $args->{"API_KEY"} 0 1 1
Condition33%1/3
ref $args eq "HASH" and exists $args->{"API_KEY"}
!ll&&!rl&&r
010
MC/DC0%0/2
ref $args eq "HASH" and exists $args->{"API_KEY"}
ref $args eq "HASH" exists $args->{"API_KEY"}
Truth table33%1/3
A and B
A= ref $args eq "HASH"B= exists $args->{"API_KEY"}
ABresult
0X0
100
111
67  
68 1 return $this;
69 }
70  
71 sub multipliers
72 0 ▶{
Subroutine multipliers: not called CC=10 SCAR=47.0
Pod undocumented
73 0 my ($this, $ua, $from, $to) = @_;
74  
75 0 ▶ return $this->_bank_multipliers($ua, $from, $to) if $this->{BANK};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"BANK"} 0 0 0
76  
77 # ECB reference table re-based on the requested currency; one request
78 # covers every quote currency.
79 0 my $json_data = $this->_get_json($ua, 'open/latest/' . $from);
80  
81 0 ▶ if ($json_data and ref $json_data->{'rates'} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if ($json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$to}) 0 0 0
Condition0%0/6
$json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$to}
!ll&&!rl&&r
000
$json_data and ref $json_data->{"rates"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$to}
$json_data ref $json_data->{"rates"} eq "HASH" $json_data->{"rates"}{$to}
Truth table0%0/4
A and B and C
A= $json_dataB= ref $json_data->{"rates"} eq "HASH"C= $json_data->{"rates"}{$to}
ABCresult
0XX0
10X0
1100
1111
82 and $json_data->{'rates'}->{$to}) {
83 0 my $rate = $json_data->{'rates'}->{$to};
84 ### Rate from JSON: $rate
85 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
86 0 return (1.0, $rate);
87 }
88  
89 # $from is not one of the table's base codes; the pair may still exist
90 # the other way around.
91 0 $json_data = $this->_get_json($ua, 'open/latest/' . $to);
92  
93 0 ▶ if ($json_data and ref $json_data->{'rates'} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if ($json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$from}) 0 0 0
Condition0%0/6
$json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$from}
!ll&&!rl&&r
000
$json_data and ref $json_data->{"rates"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$json_data and ref $json_data->{"rates"} eq "HASH" and $json_data->{"rates"}{$from}
$json_data ref $json_data->{"rates"} eq "HASH" $json_data->{"rates"}{$from}
Truth table0%0/4
A and B and C
A= $json_dataB= ref $json_data->{"rates"} eq "HASH"C= $json_data->{"rates"}{$from}
ABCresult
0XX0
10X0
1100
1111
94 and $json_data->{'rates'}->{$from}) {
95 0 my $inverse = $json_data->{'rates'}->{$from};
96 ### Inverse rate from JSON: $inverse
97 0 ▶ return unless $inverse + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $inverse + 0 0 0 0
98 0 return ($inverse, 1.0);
99 }
100  
101 0 return;
102 }
103  
104 # Official table of one central bank. Every row says "1 base = value quote"
105 # and carries a rate type. Banks publish either foreign-per-home (base varies,
106 # quote is the home currency) or home-per-foreign (base is the home currency,
107 # quote varies), so the home currency is the code common to all rows. One
108 # rate type is used per pair: reference, else middle/mid/official, else the
109 # mean of a buy and a sell row.
110 sub _bank_multipliers
111 0 ▶{
Subroutine _bank_multipliers: not called CC=27 SCAR=66.3
112 0 my ($this, $ua, $from, $to) = @_;
113  
114 my $path = $this->{API_KEY}
115 ? 'v1/central-bank/' . $this->{BANK} . '/latest'
116 0 ▶ : 'open/central-bank/' . $this->{BANK};
Branch: 1 branch
BranchTrueFalseTotal
$this->{'API_KEY'} ? : 0 0 0
117 0 my $json_data = $this->_get_json($ua, $path);
118  
119 0 ▶ return unless $json_data and ref $json_data->{'rates'} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $json_data and ref $json_data->{"rates"} eq "ARRAY" 0 0 0
Condition0%0/3
$json_data and ref $json_data->{"rates"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$json_data and ref $json_data->{"rates"} eq "ARRAY"
$json_data ref $json_data->{"rates"} eq "ARRAY"
Truth table0%0/3
A and B
A= $json_dataB= ref $json_data->{"rates"} eq "ARRAY"
ABresult
0X0
100
111
120  
121 0 my %by_pair;
122 0 foreach my $row (@{$json_data->{'rates'}}) {
123 next unless ref $row eq 'HASH' and $row->{'base'} and $row->{'quote'}
124 0 ▶ and $row->{'value'} and $row->{'value'} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" and $row->{"base"} and $row->{"quote"} and $row->{"value"} and $row->{"value"} > 0 0 0 0
Condition0%0/12
ref $row eq "HASH" and $row->{"base"} and $row->{"quote"} and $row->{"value"} and $row->{"value"} > 0
!ll&&!rl&&r
000
ref $row eq "HASH" and $row->{"base"} and $row->{"quote"} and $row->{"value"}
!ll&&!rl&&r
000
ref $row eq "HASH" and $row->{"base"} and $row->{"quote"}
!ll&&!rl&&r
000
ref $row eq "HASH" and $row->{"base"}
!ll&&!rl&&r
000
MC/DC0%0/5
ref $row eq "HASH" and $row->{"base"} and $row->{"quote"} and $row->{"value"} and $row->{"value"} > 0
ref $row eq "HASH" $row->{"base"} $row->{"quote"} $row->{"value"} $row->{"value"} > 0
Truth table0%0/6
A and B and C and D and E
A= ref $row eq "HASH"B= $row->{"base"}C= $row->{"quote"}D= $row->{"value"}E= $row->{"value"} > 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
125 0 my $pair = uc($row->{'base'}) . '/' . uc($row->{'quote'});
126 0 ▶ $by_pair{$pair}{lc($row->{'type'} || 'reference')} = $row->{'value'};
Condition0%0/2
$row->{'type'} || 'reference'
l!l
00
MC/DC0%0/1
$row->{'type'} || 'reference'
$row->{'type'}
Truth table0%0/2
A
A= $row->{'type'}
Aresult
11
00
127 }
128  
129 0 my (%seen, @rows);
130 0 foreach my $pair (keys %by_pair) {
131 0 my $types = $by_pair{$pair};
132 0 my $value;
133 0 foreach my $type ('reference', 'middle', 'mid', 'official') {
134 0 ▶ $value = $types->{$type}, last if exists $types->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $types->{$type} 0 0 0
135 }
136 0 ▶ if (not defined $value and exists $types->{'buy'} and exists $types->{'sell'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and exists $types->{"buy"} and exists $types->{"sell"}) 0 0 0
Condition0%0/6
not defined $value and exists $types->{"buy"} and exists $types->{"sell"}
!ll&&!rl&&r
000
not defined $value and exists $types->{"buy"}
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $value and exists $types->{"buy"} and exists $types->{"sell"}
not defined $value exists $types->{"buy"} exists $types->{"sell"}
Truth table0%0/4
not(A) and B and C
A= not defined $valueB= exists $types->{"buy"}C= exists $types->{"sell"}
ABCresult
0XX0
10X0
1100
1111
137 0 $value = ($types->{'buy'} + $types->{'sell'}) / 2;
138 }
139 0 ▶ $value = (values %$types)[0] if not defined $value and keys %$types == 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and keys %$types == 1 0 0 0
Condition0%0/3
not defined $value and keys %$types == 1
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $value and keys %$types == 1
not defined $value keys %$types == 1
Truth table0%0/3
not(A) and B
A= not defined $valueB= keys %$types == 1
ABresult
0X0
100
111
140 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
141  
142 0 my ($base, $quote) = split '/', $pair;
143 0 push @rows, [$base, $quote, $value];
144 0 $seen{$base}++;
145 0 $seen{$quote}++;
146 }
147 0 ▶ return unless @rows;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
148  
149 0 ▶ my ($home) = sort { $seen{$b} <=> $seen{$a} or $a cmp $b } keys %seen;
Condition0%0/3
$seen{$b} <=> $seen{$a} || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$seen{$b} <=> $seen{$a} || $a cmp $b
$seen{$b} <=> $seen{$a} $a cmp $b
Truth table0%0/3
A || B
A= $seen{$b} <=> $seen{$a}B= $a cmp $b
ABresult
1X1
011
000
150  
151 # %units: home-currency units per one unit of each currency
152 0 my %units = ($home => 1.0);
153 0 foreach my $row (@rows) {
154 0 my ($base, $quote, $value) = @$row;
155 0 ▶ if ($quote eq $home) {
Branch: 2 branches
BranchTrueFalseTotal
if ($quote eq $home) { } 0 0 0
elsif ($base eq $home) { } 0 0 0
156 0 $units{$base} = $value;
157 }
158 elsif ($base eq $home) {
159 0 $units{$quote} = 1.0 / $value;
160 }
161 }
162  
163 ### Bank table home : $home
164 ### Bank table units : \%units
165  
166 0 ▶ return unless $units{$from} and $units{$to};
Branch: 1 branch
BranchTrueFalseTotal
unless $units{$from} and $units{$to} 0 0 0
Condition0%0/3
$units{$from} and $units{$to}
!ll&&!rl&&r
000
MC/DC0%0/2
$units{$from} and $units{$to}
$units{$from} $units{$to}
Truth table0%0/3
A and B
A= $units{$from}B= $units{$to}
ABresult
0X0
100
111
167  
168 # amount * (units{$from} / units{$to}) == amount in $to
169 0 return ($units{$to}, $units{$from});
170 }
171  
172 sub _get_json
173 0 ▶{
Subroutine _get_json: not called CC=3 SCAR=24.8
174 0 my ($this, $ua, $path) = @_;
175  
176 0 my $url = $ALLRATESTODAY_URL_HEAD . $path;
177  
178 0 my @headers = (
179 'Accept' => 'application/json',
180 'User-Agent' => 'finance-quote-allratestoday/1.0');
181 0 ▶ push @headers, 'Authorization' => 'Bearer ' . $this->{API_KEY} if $this->{API_KEY};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"API_KEY"} 0 0 0
182  
183 0 my $reply = $ua->get($url, @headers);
184  
185 ### URL: $url
186 ### HTTP Status: $reply->code
187  
188 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
189  
190 0 my $json_data = eval { JSON::decode_json $reply->content };
191  
192 ### JSON: $json_data
193  
194 0 return $json_data;
195 }
196  
197 1 1;
198  
199 =head1 NAME
200  
201 Finance::Quote::CurrencyRates::AllRatesToday - Obtain currency rates from
202 https://allratestoday.com/api/open/
203  
204 =head1 SYNOPSIS
205  
206 use Finance::Quote;
207  
208 # European Central Bank reference rates (30 currencies)
209 $q = Finance::Quote->new(currency_rates => {order => ['AllRatesToday']});
210 $value = $q->currency('18.99 EUR', 'USD');
211  
212 # Official table of one central bank, e.g. the Saudi Central Bank
213 $q = Finance::Quote->new(currency_rates => {order => ['AllRatesToday'],
214 allratestoday => {BANK => 'sama'}});
215 $value = $q->currency('100.00 SAR', 'USD');
216  
217 =head1 DESCRIPTION
218  
219 This module fetches currency rates from the keyless AllRatesToday open
220 endpoints and provides data to Finance::Quote to convert the first argument
221 to the equivalent value in the currency indicated by the second argument.
222  
223 Without options it uses the European Central Bank reference table re-based
224 on the source currency (https://allratestoday.com/api/open/latest/EUR).
225  
226 With the BANK option it uses the latest official table published by that
227 central bank (https://allratestoday.com/api/open/central-bank/sama). Codes
228 are listed at https://allratestoday.com/central-bank-rates-api/institutions/
229 and include banks that other free aggregators do not carry, such as the
230 Saudi Central Bank (sama), Central Bank of the UAE (cbuae), Qatar Central
231 Bank (qcb), Bank of Korea (bok), Reserve Bank of New Zealand (rbnz), State
232 Bank of Vietnam (sbv) and Bangladesh Bank (bb). When a bank publishes
233 several rate types for a pair, the reference rate is used, else the
234 middle/official rate, else the mean of the buy and sell rates.
235  
236 This module is not the default currency conversion module for a
237 Finance::Quote object. It can be utilized by setting the environment
238 variable FQ_CURRENCY=AllRatesToday. The bank can also be set with the
239 environment variable ALLRATESTODAY_BANK, the API key with
240 ALLRATESTODAY_API_KEY.
241  
242 =head1 API_KEY
243  
244 The keyless endpoints do not require an API key. With the optional API_KEY
245 option (free or paid key from https://allratestoday.com/register) the
246 central-bank table is read from the keyed endpoint
247 https://allratestoday.com/api/v1/central-bank/<bank>/latest instead.
248  
249 =head1 Terms & Conditions
250  
251 Use of https://allratestoday.com/api/ is governed by the terms of that site
252 (https://allratestoday.com/terms/). Keyless use is for non-commercial use
253 and asks for a visible link to https://allratestoday.com where the rates are
254 displayed; commercial use needs a key on a paid plan.
255  
256 Finance::Quote is released under the GNU General Public License, version 2,
257 which explicitly carries a "No Warranty" clause.
258  
259 =cut
« Quote/IndiaMutual.pm ↑ Summary Quote/YahooWeb.pm »