| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '1.72'; # VERSION | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub new | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | my $this = {}; | |||||||||||||||||||||||||||
| 36 | 1 | bless $this, $class; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | return $this; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub multipliers | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
multipliers: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 0 | my ($this, $ua, $from, $to) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless (exists $this->{cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | return unless ($reply->code == 200); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | my $xml = XML::LibXML->load_xml(string => $reply->content); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | $this->{cache} = {map {$_->getAttribute('currency'), $_->getAttribute('rate')} $xml->findnodes('//*[@currency]')}; | |||||||||||||||||||||||||||
| 54 | 0 | $this->{cache}->{EUR} = 1.0; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ### cache : $this->{cache} | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
MC/DC0%0/2
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
exists $this->{"cache"}{$from}
exists $this->{"cache"}{$to}
Truth table0%0/3
A and B
A= exists $this->{"cache"}{$from}B= 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 | 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 | ||||||||||||||||||||||||||||