| 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 <[email protected]> |
| 20 |
|
| 21 |
package Finance::Quote::CurrencyRates::CurrencyFreaks; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
use constant DEBUG => $ENV{DEBUG}; |
| 27 |
use constant TESTING => $ENV{TESTING}; |
| 28 |
|
| 29 |
use if DEBUG, 'Smart::Comments'; |
| 30 |
|
| 31 |
use JSON; |
| 32 |
|
| 33 |
our $VERSION = '1.71'; # VERSION |
| 34 |
|
| 35 |
sub parameters { |
| 36 |
return ('API_KEY'); |
| 37 |
} |
| 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 |
### CurrencyFreaks->new args : $args |
| 50 |
|
| 51 |
# CurrencyFreaks 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{'CURRENCYFREAKS_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 $reply = $ua->get('https://api.currencyfreaks.com/v2.0/rates/latest' |
| 66 |
. '?apikey=' . $this->{API_KEY} |
| 67 |
. '&symbols=' . ${from} . ',' . ${to} |
| 68 |
); |
| 69 |
|
| 70 |
my $body = $reply->content; |
| 71 |
my $reply_code = $reply->code; |
| 72 |
|
| 73 |
if (TESTING) { |
| 74 |
$body = '{ |
| 75 |
"date": "2023-03-21 13:26:00+00", |
| 76 |
"base": "USD", |
| 77 |
"rates": { |
| 78 |
"EUR": "0.9278605451274349", |
| 79 |
"GBP": "0.8172754173817152", |
| 80 |
"PKR": "281.6212943333344", |
| 81 |
"USD": "1.0", |
| 82 |
"TST": "3000.0" |
| 83 |
} |
| 84 |
}'; |
| 85 |
$reply_code = 200; |
| 86 |
} |
| 87 |
|
| 88 |
### HTTP body: $body |
| 89 |
|
| 90 |
return unless ($reply_code == 200); |
| 91 |
|
| 92 |
my $json_data = decode_json ($body); |
| 93 |
|
| 94 |
if ( !$json_data->{'rates'}->{$from} || !$json_data->{'rates'}->{$to} ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
# We really don't care what the base is as long as it is same. |
| 99 |
### rates base: $json_data->{"base"} |
| 100 |
|
| 101 |
### from: $to |
| 102 |
### to: $json_data->{"base"} |
| 103 |
### rate: ($json_data->{'rates'}->{${to}}) |
| 104 |
|
| 105 |
### from: $json_data->{"base"} |
| 106 |
### to: $from |
| 107 |
### rate: ($json_data->{'rates'}->{${from}}) |
| 108 |
|
| 109 |
my $rate = |
| 110 |
$json_data->{'rates'}->{${to}} / $json_data->{'rates'}->{${from}}; |
| 111 |
|
| 112 |
return unless $rate + 0; |
| 113 |
|
| 114 |
# For small rates, request the inverse |
| 115 |
if ($rate < 0.001) { |
| 116 |
### Rate is too small, requesting inverse : $rate |
| 117 |
my ($a, $b) = $this->multipliers($ua, $to, $from); |
| 118 |
return ($b, $a); |
| 119 |
} |
| 120 |
|
| 121 |
return ($json_data->{'rates'}->{${from}}, $json_data->{'rates'}->{${to}}); |
| 122 |
|
| 123 |
# return (1.0, $rate); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
1; |
| 129 |
|
| 130 |
__END__ |
| 131 |
|
| 132 |
=head1 NAME |
| 133 |
|
| 134 |
Finance::Quote::CurrencyRates::CurrencyFreaks - Obtain currency rates from |
| 135 |
CurrencyFreaks. |
| 136 |
|
| 137 |
=head1 SYNOPSIS |
| 138 |
|
| 139 |
use Finance::Quote; |
| 140 |
$q = Finance::Quote->new(currency_rates => |
| 141 |
{order => ['CurrencyFreaks'], currencyfreaks => {API_KEY => ...} } ); |
| 142 |
$value = $q->currency('18.99 EUR', 'USD'); |
| 143 |
|
| 144 |
=head1 DESCRIPTION |
| 145 |
|
| 146 |
This module fetches currency rates from https://currencyfreaks.com/ provides |
| 147 |
data to Finance::Quote to convert the first argument to the equivalent value |
| 148 |
in the currency indicated by the second argument. |
| 149 |
|
| 150 |
This module is not the default currency conversion module for a Finance::Quote |
| 151 |
object. |
| 152 |
|
| 153 |
=head1 API_KEY |
| 154 |
|
| 155 |
https://currencyfreaks.com/ requires users to register and obtain an API key, |
| 156 |
which is also called a token. |
| 157 |
|
| 158 |
The API key may be set by either providing a currencyfreaks hash inside the |
| 159 |
currency_rates hash to Finance::Quote->new as in the above example, or by |
| 160 |
setting the environment variable CURRENCYFREAKS_API_KEY. |
| 161 |
|
| 162 |
=head1 Terms & Conditions |
| 163 |
|
| 164 |
Use of https://currencyfreaks.com/ is governed by any terms & conditions of that |
| 165 |
site. |
| 166 |
|
| 167 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 168 |
which explicitly carries a "No Warranty" clause. |
| 169 |
|
| 170 |
=cut |