File Coverage

blib/lib/Finance/Quote/CurrencyRates/YahooJSON.pm
Criterion Covered Total %
statement 21 35 60.0
branch 0 8 0.0
condition 1 6 16.6
subroutine 6 7 85.7
pod 0 2 0.0
total 28 58 48.2


line stmt bran cond sub pod time code
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::YahooJSON;
22              
23 1     1   525 use strict;
  1         3  
  1         30  
24 1     1   6 use warnings;
  1         1  
  1         34  
25              
26 1     1   6 use constant DEBUG => $ENV{DEBUG};
  1         3  
  1         72  
27 1     1   8 use if DEBUG, 'Smart::Comments';
  1         2  
  1         6  
28              
29 1     1   62 use JSON;
  1         4  
  1         7  
30              
31             our $VERSION = '1.57_03'; # TRIAL VERSION
32              
33             my $YIND_URL_HEAD = 'https://query2.finance.yahoo.com/v11/finance/quoteSummary/?symbol=';
34             my $YIND_URL_TAIL = '&modules=price';
35              
36             sub new
37             {
38 1     1 0 3 my $self = shift;
39 1   33     6 my $class = ref($self) || $self;
40              
41 1         2 my $this = {};
42 1         3 bless $this, $class;
43              
44 1         11 my $args = shift;
45              
46             ### YahooJSON->new args : $args
47              
48 1         8 return $this;
49             }
50              
51             sub multipliers
52             {
53 0     0 0   my ($this, $ua, $from, $to) = @_;
54              
55 0           my $json_data;
56             my $rate;
57 0           my $reply = $ua->get($YIND_URL_HEAD
58             . ${from}
59             . ${to}
60             . '%3DX'
61             . $YIND_URL_TAIL);
62              
63             ### HTTP Status: $reply->code
64 0 0         return unless ($reply->code == 200);
65              
66 0           my $body = $reply->content;
67              
68 0           $json_data = JSON::decode_json $body;
69              
70             ### JSON: $json_data
71              
72 0 0 0       if ( !$json_data || !$json_data->{'quoteSummary'}->{'result'}->[0]->{'price'}->{'regularMarketPrice'}{'raw'} ) {
73 0           return;
74             }
75              
76             $rate =
77 0           $json_data->{'quoteSummary'}->{'result'}->[0]->{'price'}->{'regularMarketPrice'}{'raw'};
78              
79             ### Rate from JSON: $rate
80              
81 0 0         return unless $rate + 0;
82              
83             # For small rates, request the inverse
84 0 0         if ($rate < 0.001) {
85             ### Rate is too small, requesting inverse : $rate
86 0           my ($a, $b) = $this->multipliers($ua, $to, $from);
87 0           return ($b, $a);
88             }
89              
90 0           return (1.0, $rate);
91             }
92              
93              
94             1;
95              
96             =head1 NAME
97              
98             Finance::Quote::CurrencyRates::YahooJSON - Obtain currency rates from
99             https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price
100              
101             =head1 SYNOPSIS
102              
103             use Finance::Quote;
104            
105             $q = Finance::Quote->new(currency_rates => {order => ['YahooJSON']});
106              
107             $value = $q->currency('18.99 EUR', 'USD');
108              
109             =head1 DESCRIPTION
110              
111             This module fetches currency rates from
112             https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price
113             provides data to Finance::Quote to convert the first argument to the equivalent
114             value in the currency indicated by the second argument.
115              
116             This module is not the default currency conversion module for a Finance::Quote
117             object.
118              
119             =head1 Terms & Conditions
120              
121             Use of https://query2.finance.yahoo.com/v11/finance/quoteSummary/...?modules=price is
122             governed by any terms & conditions of that site.
123              
124             Finance::Quote is released under the GNU General Public License, version 2,
125             which explicitly carries a "No Warranty" clause.
126              
127             =cut