| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | 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 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 14 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 14 | my $this = {}; | |||||||||||||||||||||||||||
| 40 | 14 | bless $this, $class; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 14 | 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 | $this->{API_KEY} = $ENV{'ALPHAVANTAGE_API_KEY'}; | |||||||||||||||||||||||||||
| 51 | 14 | ▶ | $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $args eq "HASH" and exists $args->{"API_KEY"}
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"}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 14 | return $this; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub multipliers | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
multipliers: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 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 | # AlphaVantage is limiting free/basic api use to one query per second. | ||||||||||||||||||||||||||||
| 67 | # sleep 2 ensures a query for currency exchange is not immediately | ||||||||||||||||||||||||||||
| 68 | # following a price query. | ||||||||||||||||||||||||||||
| 69 | 0 | sleep 2; | |||||||||||||||||||||||||||
| 70 | my $reply = $ua->get($url | ||||||||||||||||||||||||||||
| 71 | . '&from_currency=' . ${from} | ||||||||||||||||||||||||||||
| 72 | . '&to_currency=' . ${to} | ||||||||||||||||||||||||||||
| 73 | 0 | . '&apikey=' . $this->{API_KEY}); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return unless ($reply->code == 200); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $body = $reply->content; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | $json_data = JSON::decode_json $body; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( !$json_data || $json_data->{'Error Message'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $json_data or $json_data->{"Error Message"}
MC/DC0%0/2
not $json_data or $json_data->{"Error Message"}
not $json_data
$json_data->{"Error Message"}
Truth table0%0/3
not(A) or B
A= not $json_dataB= $json_data->{"Error Message"}
| |||||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ### JSON: $json_data | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sleep (20) if (($try_cnt < 5) && ($json_data->{'Note'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$try_cnt < 5 and $json_data->{"Note"}
MC/DC0%0/2
$try_cnt < 5 and $json_data->{"Note"}
$try_cnt < 5
$json_data->{"Note"}
Truth table0%0/3
A and B
A= $try_cnt < 5B= $json_data->{"Note"}
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | } while (($try_cnt < 5) && ($json_data->{'Note'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$try_cnt < 5 and $json_data->{"Note"}
MC/DC0%0/2
$try_cnt < 5 and $json_data->{"Note"}
$try_cnt < 5
$json_data->{"Note"}
Truth table0%0/3
A and B
A= $try_cnt < 5B= $json_data->{"Note"}
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if( !$json_data->{'Realtime Currency Exchange Rate'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | ### No data in JSON | ||||||||||||||||||||||||||||
| 91 | 0 | $rate = 0.0; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | $rate = | ||||||||||||||||||||||||||||
| 94 | 0 | $json_data->{'Realtime Currency Exchange Rate'}->{'5. Exchange Rate'}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | ### Rate from JSON: $rate | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return unless $rate + 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # For small rates, request the inverse | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($rate < 0.001) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 5 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 NAME | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Finance::Quote::CurrencyRates::AlphaVantage - Obtain currency rates from | ||||||||||||||||||||||||||||
| 117 | https://www.alphavantage.co | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | use Finance::Quote; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | $q = Finance::Quote->new(currency_rates => {order => ['AlphaVantage'], | ||||||||||||||||||||||||||||
| 124 | alphavantage => {API_KEY => ...}}); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | $value = $q->currency('18.99 EUR', 'USD'); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This module fetches currency rates from https://www.alphavantage.co and | ||||||||||||||||||||||||||||
| 131 | provides data to Finance::Quote to convert the first argument to the equivalent | ||||||||||||||||||||||||||||
| 132 | value in the currency indicated by the second argument. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | This module is the default currency conversion module for a Finance::Quote | ||||||||||||||||||||||||||||
| 135 | object. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 API_KEY | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | https://www.alphavantage.co requires users to register and obtain an API key, | ||||||||||||||||||||||||||||
| 140 | which is also called a token. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | The API key may be set by either providing a alphavantage hash inside the | ||||||||||||||||||||||||||||
| 143 | currency_rates hash to Finance::Quote->new as in the above example, or by | ||||||||||||||||||||||||||||
| 144 | setting the environment variable ALPHAVANTAGE_API_KEY. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 Terms & Conditions | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Use of https://www.alphavantage.co is governed by any terms & conditions of | ||||||||||||||||||||||||||||
| 149 | that site. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Finance::Quote is released under the GNU General Public License, version 2, | ||||||||||||||||||||||||||||
| 152 | which explicitly carries a "No Warranty" clause. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||