Quote/CurrencyRates/FinanceAPI.pm

filter: Statementstmt 64.2% 27 / 42 Branchbran 10.0% 1 / 10 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 2 totaltotal 47.3% 36 / 76 CC 9 SCAR 29.1
CC
9
Cov
49%
multipliers
42.0
new
4.5
BEGIN
1.0
CRAP 18.3
« Quote/CurrencyRates/TwelveData.pm ↑ Summary Quote/MarketWatch.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) 2024, Bruce Schuck <bschuck@asgard-systems.com>
20  
21 # Changes:
22 # 2024-09-22 - Initial version. Base code opied from
23 # CurrencyRates/YahooJSON.pm
24  
25 package Finance::Quote::CurrencyRates::FinanceAPI;
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_01'; # TRIAL VERSION
36  
37 1 my $YIND_URL_HEAD = 'https://yfapi.net/v8/finance/chart/';
38 1 my $YIND_URL_TAIL = '?metrics=high&interval=1d&range=1d';
39  
40 sub new
41 1 ▶{
Subroutine new: called CC=4 SCAR=15.0
Pod undocumented
42 1 my $self = shift;
43 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
44  
45 1 my $this = {};
46 1 bless $this, $class;
47  
48 1 my $args = shift;
49  
50 ### FinanceAPI->new args : $args
51  
52 # FinanceAPI is permitted to use an environment variable for API key
53 # (for backwards compatibility).
54 # New modules should use the API_KEY from args.
55  
56 1 $this->{API_KEY} = $ENV{'FINANCEAPI_API_KEY'};
57 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
58  
59 1 return $this;
60 }
61  
62 sub multipliers
63 0 ▶{
Subroutine multipliers: not called CC=6 SCAR=37.4
Pod undocumented
64 0 my ($this, $ua, $from, $to) = @_;
65  
66 0 my $json_data;
67 my $rate;
68  
69 # Set headers. API key is sent as a header.
70 my @ua_headers = (
71 'Accept' => 'application/json',
72 'X-API-KEY' => $this->{API_KEY},
73 0 );
74  
75 0 my $reply = $ua->get($YIND_URL_HEAD
76 . ${from}
77 . ${to}
78 . '%3DX'
79 . $YIND_URL_TAIL, @ua_headers);
80  
81 ### HTTP Status: $reply->code
82 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
83  
84 0 my $body = $reply->content;
85  
86 0 $json_data = JSON::decode_json $body;
87  
88 ### JSON: $json_data
89  
90 0 ▶ if ( !$json_data || !$json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_data and $json_data->{"chart"}{"result"}[0]{"meta"}{"regularMarketPrice"}) 0 0 0
Condition0%0/3
$json_data and $json_data->{"chart"}{"result"}[0]{"meta"}{"regularMarketPrice"}
!ll&&!rl&&r
000
MC/DC0%0/2
$json_data and $json_data->{"chart"}{"result"}[0]{"meta"}{"regularMarketPrice"}
$json_data $json_data->{"chart"}{"result"}[0]{"meta"}{"regularMarketPrice"}
Truth table0%0/3
A and B
A= $json_dataB= $json_data->{"chart"}{"result"}[0]{"meta"}{"regularMarketPrice"}
ABresult
0X0
100
111
91 0 return;
92 }
93  
94 $rate =
95 0 $json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'};
96  
97 ### Rate from JSON: $rate
98  
99 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
100  
101 # For small rates, request the inverse
102 0 ▶ if ($rate < 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "0.001") 0 0 0
103 ### Rate is too small, requesting inverse : $rate
104 0 my ($a, $b) = $this->multipliers($ua, $to, $from);
105 0 return ($b, $a);
106 }
107  
108 0 return (1.0, $rate);
109 }
110  
111  
112 1 1;
113  
114 =head1 NAME
115  
116 Finance::Quote::CurrencyRates::FinanceAPI - Obtain currency rates from
117 https://yfapi.net/v8/finance/chart/.
118  
119 =head1 SYNOPSIS
120  
121 use Finance::Quote;
122
123 $q = Finance::Quote->new(currency_rates =>
124 {order => ['FinanceAPI'],
125 financeapi => {API_KEY => ...}
126 });
127  
128 $value = $q->currency('18.99 EUR', 'USD');
129  
130 =head1 DESCRIPTION
131  
132 This module fetches currency rates from
133 https://yfapi.net/v8/finance/chart/ provides data to Finance::Quote
134 to convert the first argument to the equivalent value in the currency
135 indicated by the second argument.
136  
137 This module is not the default currency conversion module for a Finance::Quote
138 object.
139  
140 =head1 API_KEY
141  
142 https://financeapi.net requires users to register and obtain an API key.
143  
144 The API key can be set by setting the Environment variable
145 "FINANCEAPI_API_KEY" or providing a 'financeapi' hash inside the
146 'currency_rates' hash to Finance::Quote->new as in the above example.
147  
148 =head1 Terms & Conditions
149  
150 Use of https://yfapi.net/v8/finance/chart/ is
151 governed by any terms & conditions of that site.
152  
153 Finance::Quote is released under the GNU General Public License, version 2,
154 which explicitly carries a "No Warranty" clause.
155  
156 =cut
« Quote/CurrencyRates/TwelveData.pm ↑ Summary Quote/MarketWatch.pm »