Quote/CurrencyRates/YahooJSON.pm

filter: Statementstmt 64.1% 25 / 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 48.4% 32 / 66 CC 7 SCAR 30.0
CC
7
Cov
49%
multipliers
42.0
new
2.0
BEGIN
1.0
CRAP 20.1
« Quote/CurrencyRates/Frankfurter.pm ↑ Summary Quote/NPSNAV.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 # Changes:
22 # 2024-04-13 - Changed to use
23 # https://query1.finance.yahoo.com/v8/finance/chart/
24  
25 package Finance::Quote::CurrencyRates::YahooJSON;
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 $YIND_URL_HEAD = 'https://query1.finance.yahoo.com/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=2 SCAR=7.1
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 ### YahooJSON->new args : $args
51  
52 1 return $this;
53 }
54  
55 sub multipliers
56 0 ▶{
Subroutine multipliers: not called CC=6 SCAR=37.4
Pod undocumented
57 0 my ($this, $ua, $from, $to) = @_;
58  
59 0 my $json_data;
60 my $rate;
61 0 my $reply = $ua->get($YIND_URL_HEAD
62 . ${from}
63 . ${to}
64 . '%3DX'
65 . $YIND_URL_TAIL);
66  
67 ### HTTP Status: $reply->code
68 0 ▶ return unless ($reply->code == 200);
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->code == 200 0 0 0
69  
70 0 my $body = $reply->content;
71  
72 0 $json_data = JSON::decode_json $body;
73  
74 ### JSON: $json_data
75  
76 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
77 0 return;
78 }
79  
80 $rate =
81 0 $json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'};
82  
83 ### Rate from JSON: $rate
84  
85 0 ▶ return unless $rate + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate + 0 0 0 0
86  
87 # For small rates, request the inverse
88 0 ▶ if ($rate < 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "0.001") 0 0 0
89 ### Rate is too small, requesting inverse : $rate
90 0 my ($a, $b) = $this->multipliers($ua, $to, $from);
91 0 return ($b, $a);
92 }
93  
94 0 return (1.0, $rate);
95 }
96  
97  
98 1 1;
99  
100 =head1 NAME
101  
102 Finance::Quote::CurrencyRates::YahooJSON - Obtain currency rates from
103 https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price
104  
105 =head1 SYNOPSIS
106  
107 use Finance::Quote;
108
109 $q = Finance::Quote->new(currency_rates => {order => ['YahooJSON']});
110  
111 $value = $q->currency('18.99 EUR', 'USD');
112  
113 =head1 DESCRIPTION
114  
115 This module fetches currency rates from
116 https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price
117 provides data to Finance::Quote to convert the first argument to the equivalent
118 value in the currency indicated by the second argument.
119  
120 This module is not the default currency conversion module for a Finance::Quote
121 object.
122  
123 =head1 Terms & Conditions
124  
125 Use of https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price is
126 governed by any terms & conditions of that site.
127  
128 Finance::Quote is released under the GNU General Public License, version 2,
129 which explicitly carries a "No Warranty" clause.
130  
131 =cut
« Quote/CurrencyRates/Frankfurter.pm ↑ Summary Quote/NPSNAV.pm »