Debian Codegraph @

lib/Finance/Quote/CurrencyRates/TwelveData.pm

raw
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 <[email protected]>
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 use strict;
28 use warnings;
29
30 use constant DEBUG => $ENV{DEBUG};
31 use if DEBUG, 'Smart::Comments';
32
33 use JSON;
34
35 our $VERSION = '1.71'; # VERSION
36
37 my $TWELVEDATA_URL_HEAD = 'https://api.twelvedata.com/exchange_rate?symbol=';
38
39 sub new
40 {
41 my $self = shift;
42 my $class = ref($self) || $self;
43
44 my $this = {};
45 bless $this, $class;
46
47 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 $this->{API_KEY} = $ENV{'TWELVEDATA_API_KEY'};
56 $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
57
58 return $this;
59 }
60
61 sub multipliers
62 {
63 my ($this, $ua, $from, $to) = @_;
64
65 my $json_data;
66 my $rate;
67
68 # Set headers. API key is sent as a header.
69 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 . $this->{API_KEY}, @ua_headers);
79
80 ### HTTP Status: $reply->code
81 return unless ($reply->code == 200);
82
83 my $body = $reply->content;
84
85 $json_data = JSON::decode_json $body;
86
87 ### JSON: $json_data
88
89 if ( !$json_data || !$json_data->{'rate'} ) {
90 return;
91 }
92
93 $rate =
94 $json_data->{'rate'};
95
96 ### Rate from JSON: $rate
97
98 return unless $rate + 0;
99
100 # For small rates, request the inverse
101 if ($rate < 0.001) {
102 ### Rate is too small, requesting inverse : $rate
103 my ($a, $b) = $this->multipliers($ua, $to, $from);
104 return ($b, $a);
105 }
106
107 return (1.0, $rate);
108 }
109
110
111 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