| 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::CurrencyFreaksscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/; |
| 22 | |
| 23 | use strictscip-perl cpan Finance-Quote 1.70 strict/; |
| 24 | use warningsscip-perl cpan Finance-Quote 1.70 warnings/; |
| 25 | |
| 26 | use constantscip-perl cpan Finance-Quote 1.70 constant/ DEBUGscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/DEBUG(). => $ENV{DEBUG}; |
| 27 | use constantscip-perl cpan Finance-Quote 1.70 constant/ TESTINGscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/TESTING(). => $ENV{TESTING}; |
| 28 | |
| 29 | use ifscip-perl cpan Finance-Quote 1.70 if/ DEBUG, 'Smart::Comments'; |
| 30 | |
| 31 | use JSONscip-perl cpan Finance-Quote 1.70 JSON/; |
| 32 | |
| 33 | our $VERSIONscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/$VERSION. = '1.70'; # VERSION |
| 34 | |
| 35 | sub parametersscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/parameters(). { |
| 36 | return ('API_KEY'); |
| 37 | } |
| 38 | |
| 39 | sub newscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/new(). |
| 40 | { |
| 41 | my $self = shiftscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/shift().; |
| 42 | my $class = refscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/ref().($self) || $self; |
| 43 | |
| 44 | my $this = {}; |
| 45 | blessscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/bless(). $this, $class; |
| 46 | |
| 47 | my $args = shiftscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/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} = $ENVscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`%ENV`.{'CURRENCYFREAKS_API_KEY'}; |
| 56 | $this->{API_KEY} = $args->{API_KEY} if (refscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/ref(). $args eq 'HASH') and (existsscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/exists(). $args->{API_KEY}); |
| 57 | |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | sub multipliersscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/multipliers(). |
| 62 | { |
| 63 | my ($this, $ua, $from, $to) = @_scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`@_`.; |
| 64 | |
| 65 | my $reply = $ua->getscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/get().('https://api.currencyfreaks.com/v2.0/rates/latest' |
| 66 | . '?apikey=' . $this->{API_KEY} |
| 67 | . '&symbols=' . ${from}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${from}`. . ',' . ${to}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${to}`. |
| 68 | ); |
| 69 | |
| 70 | my $body = $reply->contentscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/content().; |
| 71 | my $reply_code = $reply->codescip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/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_jsonscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/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}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${to}`.} / $json_data->{'rates'}->{${from}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${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->multipliersscip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/multipliers().($ua, $to, $from); |
| 118 | return ($b, $a); |
| 119 | } |
| 120 | |
| 121 | return ($json_data->{'rates'}->{${from}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${from}`.}, $json_data->{'rates'}->{${to}scip-perl cpan Finance-Quote 1.70 Finance/Quote/CurrencyRates/CurrencyFreaks/`${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 |