Debian Codegraph @

lib/Finance/Quote/CurrencyRates/ECB.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::ECB;
19
20 use strict;
21 use warnings;
22
23 use constant DEBUG => $ENV{DEBUG};
24 use if DEBUG, 'Smart::Comments';
25
26 use XML::LibXML;
27
28 our $VERSION = '1.71'; # VERSION
29
30 sub new
31 {
32 my $self = shift;
33 my $class = ref($self) || $self;
34
35 my $this = {};
36 bless $this, $class;
37
38 return $this;
39 }
40
41 sub multipliers
42 {
43 my ($this, $ua, $from, $to) = @_;
44
45 unless (exists $this->{cache}) {
46 my $url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
47
48 my $reply = $ua->get($url);
49
50 return unless ($reply->code == 200);
51 my $xml = XML::LibXML->load_xml(string => $reply->content);
52
53 $this->{cache} = {map {$_->getAttribute('currency'), $_->getAttribute('rate')} $xml->findnodes('//*[@currency]')};
54 $this->{cache}->{EUR} = 1.0;
55
56 ### cache : $this->{cache}
57 }
58
59
60 if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
61 ### from : $from, $this->{cache}->{$from}
62 ### to : $to, $this->{cache}->{$to}
63 return ($this->{cache}->{$from}, $this->{cache}->{$to});
64 }
65
66 ### At least one code not found: $from, $to
67
68 return;
69 }
70
71 1;
72
73 =head1 NAME
74
75 Finance::Quote::CurrencyRates::ECB - Obtain currency rates from
76 https://www.ecb.europa.eu
77
78 =head1 SYNOPSIS
79
80 use Finance::Quote;
81
82 $q = Finance::Quote->new(currency_rates => {order => ['ECB']});
83
84 $value = $q->currency('18.99 EUR', 'CAD');
85
86 =head1 DESCRIPTION
87
88 This module fetches currency rates from https://www.ecb.europa.eu and
89 provides data to Finance::Quote to convert the first argument to the equivalent
90 value in the currency indicated by the second argument.
91
92 The European Central Bank provides a small list of currencies, quoted
93 against the Euro. This module caches the table of rates for the lifetime
94 of the Finance::Quote object after the first currency conversion.
95
96 =head1 Terms & Conditions
97
98 Use of https://www.ecb.europa.eu is governed by any terms & conditions of that
99 site.
100
101 Finance::Quote is released under the GNU General Public License, version 2,
102 which explicitly carries a "No Warranty" clause.
103
104 =cut