Quote/CurrencyRates/UniRate.pm

filter: Statementstmt 63.4% 26 / 41 Branchbran 8.3% 1 / 12 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 45.5% 36 / 79 CC 10 SCAR 32.0
CC
10
Cov
47%
multipliers
56.0
new
4.5
BEGIN
1.0
CRAP 24.5
« Quote/BorsaItaliana.pm ↑ Summary Quote/NZX.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic 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 package Finance::Quote::CurrencyRates::UniRate;
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = '1.72_02'; # TRIAL VERSION
30  
31 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
32 1 return ('API_KEY');
33 }
34  
35 sub new
36 1 ▶{
Subroutine new: called CC=4 SCAR=15.0
Pod undocumented
37 1 my $self = shift;
38 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
39  
40 1 my $this = {};
41 1 bless $this, $class;
42  
43 1 my $args = shift;
44  
45 ### UniRate->new args : $args
46  
47 # UniRate supports an environment variable for the API key for parity
48 # with the Twelvedata and FinanceAPI currency-rate modules. An explicit
49 # API_KEY in $args overrides the environment variable.
50 1 $this->{API_KEY} = $ENV{'UNIRATEAPI_API_KEY'};
51 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
52  
53 1 return $this;
54 }
55  
56 sub multipliers
57 0 ▶{
Subroutine multipliers: not called CC=7 SCAR=40.3
Pod undocumented
58 0 my ($this, $ua, $from, $to) = @_;
59  
60 my $url = 'https://api.unirateapi.com/api/rates'
61 . '?from=' . ${from}
62 . '&to=' . ${to}
63 0 . '&api_key=' . $this->{API_KEY};
64  
65 0 my $reply = $ua->get($url,
66 'Accept' => 'application/json',
67 'User-Agent' => 'finance-quote-unirate/0.01');
68  
69 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
70  
71 0 my $body = $reply->content;
72  
73 ### HTTP body: $body
74  
75 0 my $json_data = eval { JSON::decode_json $body };
76 0 ▶ return if $@ or !$json_data;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $json_data 0 0 0
Condition0%0/3
$@ or not $json_data
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $json_data
$@ not $json_data
Truth table0%0/3
A or not(B)
A= $@B= not $json_data
ABresult
1X1
011
000
77  
78 0 ▶ return unless exists $json_data->{'rate'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json_data->{"rate"} 0 0 0
79  
80 0 my $rate = $json_data->{'rate'};
81  
82 ### Rate from JSON: $rate
83  
84 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
85  
86 # For small rates, request the inverse.
87 0 ▶ if ($rate < 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "0.001") 0 0 0
88 ### Rate is too small, requesting inverse : $rate
89 0 my ($a, $b) = $this->multipliers($ua, $to, $from);
90 0 return ($b, $a);
91 }
92  
93 0 return (1.0, $rate);
94 }
95  
96  
97 1 1;
98  
99 __END__
« Quote/BorsaItaliana.pm ↑ Summary Quote/NZX.pm »