Quote/CurrencyRates/Frankfurter.pm

filter: Statementstmt 61.5% 24 / 39 Branchbran 0.0% 0 / 8 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 2 totaltotal 46.9% 31 / 66 CC 7 SCAR 30.0
CC
7
Cov
47%
multipliers
42.0
new
2.0
BEGIN
1.0
CRAP 20.1
« Quote/NZX.pm ↑ Summary Quote/CurrencyRates/YahooJSON.pm »
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 <bschuck@asgard-systems.com>
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 our $VERSION = '1.72_02'; # TRIAL VERSION
36  
37 1 my $FRANKFURTER_URL_HEAD = 'https://api.frankfurter.dev/v2/rate/';
38  
39 sub new
40 1 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
41 1 my $self = shift;
42 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
43  
44 1 my $this = {};
45 1 bless $this, $class;
46  
47 1 my $args = shift;
48  
49 ### Frankfurter->new args : $args
50  
51 1 return $this;
52 }
53  
54 sub multipliers
55 0 ▶{
Subroutine multipliers: not called CC=6 SCAR=37.4
Pod undocumented
56 0 my ($this, $ua, $from, $to) = @_;
57  
58 0 my $json_data;
59 my $rate;
60  
61 # Set headers.
62 0 my @ua_headers = (
63 'Accept' => 'application/json',
64 'User-Agent' => 'finance-quote-frankfurter/1.0',
65 );
66  
67 0 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 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
77  
78 0 my $body = $reply->content;
79  
80 0 $json_data = JSON::decode_json $body;
81  
82 ### JSON: $json_data
83  
84 0 ▶ if ( !$json_data || !$json_data->{'rate'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_data and $json_data->{"rate"}) 0 0 0
Condition0%0/3
$json_data and $json_data->{"rate"}
!ll&&!rl&&r
000
MC/DC0%0/2
$json_data and $json_data->{"rate"}
$json_data $json_data->{"rate"}
Truth table0%0/3
A and B
A= $json_dataB= $json_data->{"rate"}
ABresult
0X0
100
111
85 0 return;
86 }
87  
88 $rate =
89 0 $json_data->{'rate'};
90  
91 ### Rate from JSON: $rate
92  
93 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
94  
95 # For small rates, request the inverse
96 0 ▶ if ($rate < 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "0.001") 0 0 0
97 ### Rate is too small, requesting inverse : $rate
98 0 my ($a, $b) = $this->multipliers($ua, $to, $from);
99 0 return ($b, $a);
100 }
101  
102 0 return (1.0, $rate);
103 }
104  
105  
106 1 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
« Quote/NZX.pm ↑ Summary Quote/CurrencyRates/YahooJSON.pm »