Debian Codegraph @

lib/Finance/Quote/CurrencyRates/UniRate.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic 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 package Finance::Quote::CurrencyRates::UniRate;
20
21 use strict;
22 use warnings;
23
24 use constant DEBUG => $ENV{DEBUG};
25 use if DEBUG, 'Smart::Comments';
26
27 use JSON;
28
29 our $VERSION = '1.71'; # VERSION
30
31 sub parameters {
32 return ('API_KEY');
33 }
34
35 sub new
36 {
37 my $self = shift;
38 my $class = ref($self) || $self;
39
40 my $this = {};
41 bless $this, $class;
42
43 my $args = shift;
44
45 ### UniRate->new args : $args
46
47 # UniRate supports an environment variable for the API key for parity
48 # with the Twelvedata and FinanceAPI currency-rate modules. An explicit
49 # API_KEY in $args overrides the environment variable.
50 $this->{API_KEY} = $ENV{'UNIRATEAPI_API_KEY'};
51 $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
52
53 return $this;
54 }
55
56 sub multipliers
57 {
58 my ($this, $ua, $from, $to) = @_;
59
60 my $url = 'https://api.unirateapi.com/api/rates'
61 . '?from=' . ${from}
62 . '&to=' . ${to}
63 . '&api_key=' . $this->{API_KEY};
64
65 my $reply = $ua->get($url,
66 'Accept' => 'application/json',
67 'User-Agent' => 'finance-quote-unirate/0.01');
68
69 return unless ($reply->code == 200);
70
71 my $body = $reply->content;
72
73 ### HTTP body: $body
74
75 my $json_data = eval { JSON::decode_json $body };
76 return if $@ or !$json_data;
77
78 return unless exists $json_data->{'rate'};
79
80 my $rate = $json_data->{'rate'};
81
82 ### Rate from JSON: $rate
83
84 return unless $rate + 0;
85
86 # For small rates, request the inverse.
87 if ($rate < 0.001) {
88 ### Rate is too small, requesting inverse : $rate
89 my ($a, $b) = $this->multipliers($ua, $to, $from);
90 return ($b, $a);
91 }
92
93 return (1.0, $rate);
94 }
95
96
97 1;
98
99 __END__
100
101 =head1 NAME
102
103 Finance::Quote::CurrencyRates::UniRate - Obtain currency rates from
104 https://unirateapi.com
105
106 =head1 SYNOPSIS
107
108 use Finance::Quote;
109
110 $q = Finance::Quote->new(currency_rates => {order => ['UniRate'],
111 unirate => {API_KEY => ...}});
112
113 $value = $q->currency('18.99 EUR', 'USD');
114
115 =head1 DESCRIPTION
116
117 This module fetches currency rates from https://unirateapi.com and provides
118 data to Finance::Quote to convert the first argument to the equivalent value
119 in the currency indicated by the second argument.
120
121 UniRate covers 1000+ currencies (fiat and crypto) with daily updates and a
122 generous free tier. This module only uses the free /api/rates endpoint.
123
124 This module is not the default currency conversion module for a Finance::Quote
125 object.
126
127 =head1 API_KEY
128
129 https://unirateapi.com requires users to register and obtain an API key.
130 The free tier covers the current-rate lookups used by this module.
131
132 The API key can be set by setting the environment variable
133 "UNIRATEAPI_API_KEY" or by providing a 'unirate' hash inside the
134 'currency_rates' hash to Finance::Quote->new as in the synopsis above.
135 An explicit value in the hash overrides the environment variable.
136
137 =head1 Terms & Conditions
138
139 Use of https://unirateapi.com is governed by any terms & conditions of that
140 site.
141
142 Finance::Quote is released under the GNU General Public License, version 2,
143 which explicitly carries a "No Warranty" clause.
144
145 =cut