| 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) 2025, Bruce Schuck <bschuck@asgard-systems.com> | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Changes: | ||||||||||||||||||||||||||||
| 22 | # 2025-12-22 - Initial version. Base code opied from | ||||||||||||||||||||||||||||
| 23 | # CurrencyRates/FinanceAPI.pm | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package Finance::Quote::CurrencyRates::TwelveData; | ||||||||||||||||||||||||||||
| 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 $TWELVEDATA_URL_HEAD = 'https://api.twelvedata.com/exchange_rate?symbol='; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub new | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | my $this = {}; | |||||||||||||||||||||||||||
| 45 | 1 | bless $this, $class; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | my $args = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ### TwelveData->new args : $args | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # TwelveData is permitted to use an environment variable for API key | ||||||||||||||||||||||||||||
| 52 | # (for backwards compatibility). | ||||||||||||||||||||||||||||
| 53 | # New modules should use the API_KEY from args. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | $this->{API_KEY} = $ENV{'TWELVEDATA_API_KEY'}; | |||||||||||||||||||||||||||
| 56 | 1 | ▶ | $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"}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | return $this; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub multipliers | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
multipliers: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($this, $ua, $from, $to) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $json_data; | |||||||||||||||||||||||||||
| 66 | my $rate; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Set headers. API key is sent as a header. | ||||||||||||||||||||||||||||
| 69 | 0 | my @ua_headers = ( | |||||||||||||||||||||||||||
| 70 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | my $reply = $ua->get($TWELVEDATA_URL_HEAD | ||||||||||||||||||||||||||||
| 74 | . ${from} | ||||||||||||||||||||||||||||
| 75 | . '%2F' | ||||||||||||||||||||||||||||
| 76 | . ${to} | ||||||||||||||||||||||||||||
| 77 | . '&apikey=' | ||||||||||||||||||||||||||||
| 78 | 0 | . $this->{API_KEY}, @ua_headers); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ### HTTP Status: $reply->code | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return unless ($reply->code == 200); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $body = $reply->content; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $json_data = JSON::decode_json $body; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ### JSON: $json_data | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( !$json_data || !$json_data->{'rate'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$json_data and $json_data->{"rate"}
MC/DC0%0/2
$json_data and $json_data->{"rate"}
$json_data
$json_data->{"rate"}
Truth table0%0/3
A and B
A= $json_dataB= $json_data->{"rate"}
| |||||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | $rate = | ||||||||||||||||||||||||||||
| 94 | 0 | $json_data->{'rate'}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ### Rate from JSON: $rate | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return unless $rate + 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # For small rates, request the inverse | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($rate < 0.001) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | ### Rate is too small, requesting inverse : $rate | ||||||||||||||||||||||||||||
| 103 | 0 | my ($a, $b) = $this->multipliers($ua, $to, $from); | |||||||||||||||||||||||||||
| 104 | 0 | return ($b, $a); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | return (1.0, $rate); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 NAME | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Finance::Quote::CurrencyRates::TwelveData - Obtain currency rates from | ||||||||||||||||||||||||||||
| 116 | https://api.twelvedata.com/exchange_rate | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | use Finance::Quote; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | $q = Finance::Quote->new(currency_rates => | ||||||||||||||||||||||||||||
| 123 | {order => ['TwelveData'], | ||||||||||||||||||||||||||||
| 124 | twelvedata => {API_KEY => ...} | ||||||||||||||||||||||||||||
| 125 | }); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | $value = $q->currency('18.99 EUR', 'USD'); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | This module fetches currency rates from | ||||||||||||||||||||||||||||
| 132 | https://api.twelvedata.com/exchange_rate provides data to Finance::Quote | ||||||||||||||||||||||||||||
| 133 | to convert the first argument to the equivalent value in the currency | ||||||||||||||||||||||||||||
| 134 | indicated by the second argument. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This module is not the default currency conversion module for a Finance::Quote | ||||||||||||||||||||||||||||
| 137 | object. | ||||||||||||||||||||||||||||
| 138 | It can be utilized by setting the environment variable FQ_CURRENCY=TwelveData. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 API_KEY | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | https://api.twelvedata.com/exchange_rate requires users to register and obtain an API key. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | The API key can be set by setting the Environment variable | ||||||||||||||||||||||||||||
| 145 | "TWELVEDATA_API_KEY" or providing a 'twelvedata' 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://api.twelvedata.com/exchange_rate 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 | ||||||||||||||||||||||||||||