File Coverage

blib/lib/Finance/Quote/CurrencyRates/ECB.pm
Criterion Covered Total %
statement 20 32 62.5
branch 0 6 0.0
condition 1 6 16.6
subroutine 6 7 85.7
pod 0 2 0.0
total 27 53 50.9


line stmt bran cond sub pod time code
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 1     1   569 use strict;
  1         3  
  1         32  
21 1     1   4 use warnings;
  1         3  
  1         36  
22              
23 1     1   5 use constant DEBUG => $ENV{DEBUG};
  1         3  
  1         60  
24 1     1   7 use if DEBUG, 'Smart::Comments';
  1         2  
  1         8  
25              
26 1     1   742 use XML::LibXML;
  1         33195  
  1         8  
27              
28             our $VERSION = '1.57_03'; # TRIAL VERSION
29              
30             sub new
31             {
32 1     1 0 5 my $self = shift;
33 1   33     9 my $class = ref($self) || $self;
34              
35 1         3 my $this = {};
36 1         3 bless $this, $class;
37              
38 1         4 return $this;
39             }
40              
41             sub multipliers
42             {
43 0     0 0   my ($this, $ua, $from, $to) = @_;
44              
45 0 0         unless (exists $this->{cache}) {
46 0           my $url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
47              
48 0           my $reply = $ua->get($url);
49              
50 0 0         return unless ($reply->code == 200);
51 0           my $xml = XML::LibXML->load_xml(string => $reply->content);
52              
53 0           $this->{cache} = {map {$_->getAttribute('currency'), $_->getAttribute('rate')} $xml->findnodes('//*[@currency]')};
  0            
54 0           $this->{cache}->{EUR} = 1.0;
55            
56             ### cache : $this->{cache}
57             }
58              
59              
60 0 0 0       if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
61             ### from : $from, $this->{cache}->{$from}
62             ### to : $to, $this->{cache}->{$to}
63 0           return ($this->{cache}->{$from}, $this->{cache}->{$to});
64             }
65              
66             ### At least one code not found: $from, $to
67              
68 0           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