File Coverage

blib/lib/Finance/Quote/CurrencyRates/AlphaVantage.pm
Criterion Covered Total %
statement 24 45 53.3
branch 1 14 7.1
condition 2 15 13.3
subroutine 7 8 87.5
pod 0 3 0.0
total 34 85 40.0


line stmt bran cond sub pod time code
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::AlphaVantage;
19              
20 5     5   2780 use strict;
  5         17  
  5         156  
21 5     5   26 use warnings;
  5         12  
  5         170  
22              
23 5     5   29 use constant DEBUG => $ENV{DEBUG};
  5         14  
  5         307  
24 5     5   33 use if DEBUG, 'Smart::Comments';
  5         12  
  5         30  
25              
26 5     5   197 use JSON;
  5         16  
  5         50  
27              
28             our $VERSION = '1.57_03'; # TRIAL VERSION
29              
30             sub parameters {
31 1     1 0 4 return ('API_KEY');
32             }
33              
34             sub new
35             {
36 14     14 0 26 my $self = shift;
37 14   33     75 my $class = ref($self) || $self;
38              
39 14         26 my $this = {};
40 14         30 bless $this, $class;
41              
42 14         25 my $args = shift;
43              
44             ### AlphaVantage->new args : $args
45              
46             # AlphaVantage is the ONLY module permitted to use an environment variable
47             # for API key (for backwards compatibility). New modules MUST use the
48             # API_KEY from args.
49              
50 14         47 $this->{API_KEY} = $ENV{'ALPHAVANTAGE_API_KEY'};
51 14 50 33     76 $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
52              
53 14         40 return $this;
54             }
55              
56             sub multipliers
57             {
58 0     0 0   my ($this, $ua, $from, $to) = @_;
59              
60 0           my $url = 'https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE';
61 0           my $try_cnt = 0;
62 0           my $json_data;
63             my $rate;
64             do {
65 0           $try_cnt += 1;
66             my $reply = $ua->get($url
67             . '&from_currency=' . ${from}
68             . '&to_currency=' . ${to}
69 0           . '&apikey=' . $this->{API_KEY});
70              
71 0 0         return unless ($reply->code == 200);
72              
73 0           my $body = $reply->content;
74              
75 0           $json_data = JSON::decode_json $body;
76 0 0 0       if ( !$json_data || $json_data->{'Error Message'} ) {
77 0           return;
78             }
79              
80             ### JSON: $json_data
81            
82 0 0 0       sleep (20) if (($try_cnt < 5) && ($json_data->{'Note'}));
83 0   0       } while (($try_cnt < 5) && ($json_data->{'Note'}));
84              
85 0 0         if( !$json_data->{'Realtime Currency Exchange Rate'} ) {
86             ### No data in JSON
87 0           $rate = 0.0;
88             } else {
89             $rate =
90 0           $json_data->{'Realtime Currency Exchange Rate'}->{'5. Exchange Rate'};
91             }
92              
93             ### Rate from JSON: $rate
94              
95 0 0         return unless $rate + 0;
96              
97             # For small rates, request the inverse
98 0 0         if ($rate < 0.001) {
99             ### Rate is too small, requesting inverse : $rate
100 0           my ($a, $b) = $this->multipliers($ua, $to, $from);
101 0           return ($b, $a);
102             }
103              
104 0           return (1.0, $rate);
105             }
106              
107              
108             1;
109              
110             =head1 NAME
111              
112             Finance::Quote::CurrencyRates::AlphaVantage - Obtain currency rates from
113             https://www.alphavantage.co
114              
115             =head1 SYNOPSIS
116              
117             use Finance::Quote;
118            
119             $q = Finance::Quote->new(currency_rates => {order => ['AlphaVantage'],
120             alphavantage => {API_KEY => ...}});
121              
122             $value = $q->currency('18.99 EUR', 'USD');
123              
124             =head1 DESCRIPTION
125              
126             This module fetches currency rates from https://www.alphavantage.co and
127             provides data to Finance::Quote to convert the first argument to the equivalent
128             value in the currency indicated by the second argument.
129              
130             This module is the default currency conversion module for a Finance::Quote
131             object.
132              
133             =head1 API_KEY
134              
135             https://www.alphavantage.co requires users to register and obtain an API key,
136             which is also called a token.
137              
138             The API key may be set by either providing a alphavantage hash inside the
139             currency_rates hash to Finance::Quote->new as in the above example, or by
140             setting the environment variable ALPHAVANTAGE_API_KEY.
141              
142             =head1 Terms & Conditions
143              
144             Use of https://www.alphavantage.co is governed by any terms & conditions of
145             that site.
146              
147             Finance::Quote is released under the GNU General Public License, version 2,
148             which explicitly carries a "No Warranty" clause.
149              
150             =cut