Quote/CurrencyRates/OpenExchange.pm

filter: Statementstmt 66.6% 28 / 42 Branchbran 21.4% 3 / 14 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 41.6% 40 / 96 CC 14 SCAR 37.1
CC
14
Cov
45%
multipliers
90.0
new
7.5
BEGIN
1.0
CRAP 41.0
« Quote/CurrencyRates/Fixer.pm ↑ Summary Quote/ASX.pm »
1 #!/usr/bin/perl -w
2  
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA
17  
18 package Finance::Quote::CurrencyRates::OpenExchange;
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 our $VERSION = '1.72_01'; # TRIAL VERSION
29  
30 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
31 1 return ('API_KEY');
32 }
33  
34 sub new
35 1 ▶{
Subroutine new: called CC=6 SCAR=20.2
Pod undocumented
36 1 my $self = shift;
37 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
38  
39 1 my $this = {};
40 1 bless $this, $class;
41  
42 1 my $args = shift;
43  
44 ### OpenExchange->new args : $args
45  
46 1 $this->{API_KEY} = $ENV{'OPENEXCHANGE_API_KEY'};
47  
48 $this->{API_KEY} =
49 1 ▶ $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
50 1 $this->{refresh} = 0;
51 1 ▶ $this->{refresh} = not $args->{cache} if exists $args->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"cache"} 0 1 1
52  
53 # Return nothing if API_KEY not set
54 1 ▶ return unless ($this->{API_KEY});
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"API_KEY"} 1 0 1
55  
56 ### API_KEY: $this->{API_KEY}
57  
58 0 return $this;
59  
60 } # end new
61  
62 sub multipliers
63 0 ▶{
Subroutine multipliers: not called CC=9 SCAR=45.0
Pod undocumented
64 0 my ($this, $ua, $from, $to) = @_;
65  
66 0 ▶ if ($this->{refresh} or not exists $this->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"refresh"} or not exists $this->{"cache"}) 0 0 0
Condition0%0/3
$this->{"refresh"} or not exists $this->{"cache"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"refresh"} or not exists $this->{"cache"}
$this->{"refresh"} not exists $this->{"cache"}
Truth table0%0/3
A or not(B)
A= $this->{"refresh"}B= not exists $this->{"cache"}
ABresult
1X1
011
000
67 0 my $url = "https://openexchangerates.org/api/latest.json?app_id=$this->{API_KEY}";
68 0 my $reply = $ua->get($url);
69  
70 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
71  
72 0 my $body = $reply->content;
73  
74 0 my $json_data = JSON::decode_json $body;
75 0 ▶ if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $json_data or $json_data->{"error"} or not exists $json_data->{"rates"}) 0 0 0
Condition0%0/6
not $json_data or $json_data->{"error"} or not exists $json_data->{"rates"}
l!l&&r!l&&!r
000
not $json_data or $json_data->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $json_data or $json_data->{"error"} or not exists $json_data->{"rates"}
not $json_data $json_data->{"error"} not exists $json_data->{"rates"}
Truth table0%0/4
not(A) or B or not(C)
A= not $json_dataB= $json_data->{"error"}C= not exists $json_data->{"rates"}
ABCresult
1XX1
01X1
0011
0000
76 ### OpenExchange error : $json_data->{description}
77 0 return;
78 }
79  
80 0 $this->{cache} = $json_data->{rates};
81 ### OpenExchange rates: $this->{cache}
82 }
83  
84  
85 0 ▶ if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}) 0 0 0
Condition0%0/3
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
exists $this->{"cache"}{$from} exists $this->{"cache"}{$to}
Truth table0%0/3
A and B
A= exists $this->{"cache"}{$from}B= exists $this->{"cache"}{$to}
ABresult
0X0
100
111
86 ### from : $from, $this->{cache}->{$from}
87 ### to : $to, $this->{cache}->{$to}
88 0 return ($this->{cache}->{$from}, $this->{cache}->{$to});
89 }
90  
91 ### At least one code not found: $from, $to
92  
93 0 return;
94 } # end multipliers
95  
96 1 1;
97  
98 =head1 NAME
99  
100 Finance::Quote::CurrencyRates::OpenExchange - Obtain currency rates from
101 https://openexchangerates.org
102  
103 =head1 SYNOPSIS
104  
105 use Finance::Quote;
106
107 $q = Finance::Quote->new(currency_rates =>
108 {order => ['OpenExchange'], openexchange => {API_KEY => ...}}
109 );
110  
111 $value = $q->currency('18.99 EUR', 'USD');
112  
113 =head1 DESCRIPTION
114  
115 This module fetches currency rates from https://openexchangerates.org and
116 provides data to Finance::Quote to convert the first argument to the equivalent
117 value in the currency indicated by the second argument.
118  
119 This module caches the currency rates for the lifetime of the quoter object,
120 unless 'cache => 0' is included in the 'openexchange' options hash.
121  
122 =head1 Currency Module Selection
123  
124 The Finance::Quote currency method to be used can also be selected by setting
125 the environment variable FQ_CURRENCY.
126  
127 export FQ_CURRENCY=OpenExchange
128  
129 =head1 API_KEY
130  
131 https://openexchangerates.org requires users to register and obtain an API key. Their free plan allows 1000 queries per month.
132  
133 The API key can be set by setting the environment variable OPENEXCHANGE_API_KEY or by providing a 'openexchange' hash inside the
134 'currency_rates' hash to Finance::Quote->new as in the above example.
135  
136 =head1 Terms & Conditions
137  
138 Use of https://openexchangerates.org is governed by any terms & conditions of
139 that site.
140  
141 Finance::Quote is released under the GNU General Public License, version 2,
142 which explicitly carries a "No Warranty" clause.
143  
144 =cut
« Quote/CurrencyRates/Fixer.pm ↑ Summary Quote/ASX.pm »