Quote/CurrencyRates/CurrencyFreaks.pm

filter: Statementstmt 65.9% 29 / 44 Branchbran 10.0% 1 / 10 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 3 totaltotal 49.3% 40 / 81 CC 9 SCAR 27.9
CC
9
Cov
51%
multipliers
42.0
new
4.5
BEGIN
1.0
CRAP 16.3
« Quote/MorningstarJP.pm ↑ Summary Quote/ZA.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 ic noai 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) 2023, Bruce Schuck <bschuck@asgard-systems.com>
20  
21 package Finance::Quote::CurrencyRates::CurrencyFreaks;
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant TESTING => $ENV{TESTING};
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our $VERSION = '1.72_01'; # TRIAL VERSION
34  
35 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
36 1 return ('API_KEY');
37 }
38  
39 sub new
40 1 ▶{
Subroutine new: called CC=4 SCAR=15.0
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 ### CurrencyFreaks->new args : $args
50  
51 # CurrencyFreaks is permitted to use an environment variable for API key
52 # (for backwards compatibility).
53 # New modules should use the API_KEY from args.
54  
55 1 $this->{API_KEY} = $ENV{'CURRENCYFREAKS_API_KEY'};
56 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
57  
58 1 return $this;
59 }
60  
61 sub multipliers
62 0 ▶{
Subroutine multipliers: not called CC=6 SCAR=37.4
Pod undocumented
63 0 my ($this, $ua, $from, $to) = @_;
64  
65 my $reply = $ua->get('https://api.currencyfreaks.com/v2.0/rates/latest'
66 . '?apikey=' . $this->{API_KEY}
67 0 . '&symbols=' . ${from} . ',' . ${to}
68 );
69
70 0 my $body = $reply->content;
71 0 my $reply_code = $reply->code;
72  
73 0 if (TESTING) {
74 $body = '{
75 "date": "2023-03-21 13:26:00+00",
76 "base": "USD",
77 "rates": {
78 "EUR": "0.9278605451274349",
79 "GBP": "0.8172754173817152",
80 "PKR": "281.6212943333344",
81 "USD": "1.0",
82 "TST": "3000.0"
83 }
84 }';
85 $reply_code = 200;
86 }
87  
88 ### HTTP body: $body
89  
90 0 ▶ return unless ($reply_code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply_code == 200 0 0 0
91  
92 0 my $json_data = decode_json ($body);
93  
94 0 ▶ if ( !$json_data->{'rates'}->{$from} || !$json_data->{'rates'}->{$to} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_data->{"rates"}{$from} and $json_data->{"rates"}{$to}) 0 0 0
Condition0%0/3
$json_data->{"rates"}{$from} and $json_data->{"rates"}{$to}
!ll&&!rl&&r
000
MC/DC0%0/2
$json_data->{"rates"}{$from} and $json_data->{"rates"}{$to}
$json_data->{"rates"}{$from} $json_data->{"rates"}{$to}
Truth table0%0/3
A and B
A= $json_data->{"rates"}{$from}B= $json_data->{"rates"}{$to}
ABresult
0X0
100
111
95 0 return;
96 }
97  
98 # We really don't care what the base is as long as it is same.
99 ### rates base: $json_data->{"base"}
100  
101 ### from: $to
102 ### to: $json_data->{"base"}
103 ### rate: ($json_data->{'rates'}->{${to}})
104  
105 ### from: $json_data->{"base"}
106 ### to: $from
107 ### rate: ($json_data->{'rates'}->{${from}})
108  
109 my $rate =
110 0 $json_data->{'rates'}->{${to}} / $json_data->{'rates'}->{${from}};
111
112 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
113  
114 # For small rates, request the inverse
115 0 ▶ if ($rate < 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "0.001") 0 0 0
116 ### Rate is too small, requesting inverse : $rate
117 0 my ($a, $b) = $this->multipliers($ua, $to, $from);
118 0 return ($b, $a);
119 }
120  
121 0 return ($json_data->{'rates'}->{${from}}, $json_data->{'rates'}->{${to}});
122  
123 # return (1.0, $rate);
124 }
125  
126  
127  
128 1 1;
129  
130 __END__
« Quote/MorningstarJP.pm ↑ Summary Quote/ZA.pm »