Debian Codegraph @

lib/Finance/Quote/CurrencyRates/OpenExchange.pm

raw
1 #!/usr/bin/perl -w
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA
17
18 package Finance::Quote::CurrencyRates::OpenExchange;
19
20 use strict;
21 use warnings;
22
23 use constant DEBUG => $ENV{DEBUG};
24 use if DEBUG, 'Smart::Comments';
25
26 use JSON;
27
28 our $VERSION = '1.71'; # VERSION
29
30 sub parameters {
31 return ('API_KEY');
32 }
33
34 sub new
35 {
36 my $self = shift;
37 my $class = ref($self) || $self;
38
39 my $this = {};
40 bless $this, $class;
41
42 my $args = shift;
43
44 ### OpenExchange->new args : $args
45
46 $this->{API_KEY} = $ENV{'OPENEXCHANGE_API_KEY'};
47
48 $this->{API_KEY} =
49 $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
50 $this->{refresh} = 0;
51 $this->{refresh} = not $args->{cache} if exists $args->{cache};
52
53 # Return nothing if API_KEY not set
54 return unless ($this->{API_KEY});
55
56 ### API_KEY: $this->{API_KEY}
57
58 return $this;
59
60 } # end new
61
62 sub multipliers
63 {
64 my ($this, $ua, $from, $to) = @_;
65
66 if ($this->{refresh} or not exists $this->{cache}) {
67 my $url = "https://openexchangerates.org/api/latest.json?app_id=$this->{API_KEY}";
68 my $reply = $ua->get($url);
69
70 return unless ($reply->code == 200);
71
72 my $body = $reply->content;
73
74 my $json_data = JSON::decode_json $body;
75 if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) {
76 ### OpenExchange error : $json_data->{description}
77 return;
78 }
79
80 $this->{cache} = $json_data->{rates};
81 ### OpenExchange rates: $this->{cache}
82 }
83
84
85 if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
86 ### from : $from, $this->{cache}->{$from}
87 ### to : $to, $this->{cache}->{$to}
88 return ($this->{cache}->{$from}, $this->{cache}->{$to});
89 }
90
91 ### At least one code not found: $from, $to
92
93 return;
94 } # end multipliers
95
96 1;
97
98 =head1 NAME
99
100 Finance::Quote::CurrencyRates::OpenExchange - Obtain currency rates from
101 https://openexchangerates.org
102
103 =head1 SYNOPSIS
104
105 use Finance::Quote;
106
107 $q = Finance::Quote->new(currency_rates =>
108 {order => ['OpenExchange'], openexchange => {API_KEY => ...}}
109 );
110
111 $value = $q->currency('18.99 EUR', 'USD');
112
113 =head1 DESCRIPTION
114
115 This module fetches currency rates from https://openexchangerates.org and
116 provides data to Finance::Quote to convert the first argument to the equivalent
117 value in the currency indicated by the second argument.
118
119 This module caches the currency rates for the lifetime of the quoter object,
120 unless 'cache => 0' is included in the 'openexchange' options hash.
121
122 =head1 Currency Module Selection
123
124 The Finance::Quote currency method to be used can also be selected by setting
125 the environment variable FQ_CURRENCY.
126
127 export FQ_CURRENCY=OpenExchange
128
129 =head1 API_KEY
130
131 https://openexchangerates.org requires users to register and obtain an API key. Their free plan allows 1000 queries per month.
132
133 The API key can be set by setting the environment variable OPENEXCHANGE_API_KEY or by providing a 'openexchange' hash inside the
134 'currency_rates' hash to Finance::Quote->new as in the above example.
135
136 =head1 Terms & Conditions
137
138 Use of https://openexchangerates.org is governed by any terms & conditions of
139 that site.
140
141 Finance::Quote is released under the GNU General Public License, version 2,
142 which explicitly carries a "No Warranty" clause.
143
144 =cut