blib/lib/Finance/Currency/Convert/ECBdaily.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Finance::Currency::Convert::ECBdaily;
2  
3 1 ▶use vars qw/$VERSION $DATE $CHAT %currencies/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 $VERSION = 0.05;
6 $DATE = "01 March 2018";
7  
8 =head1 NAME
9  
10 Finance::Currency::Convert::ECBdaily - convert currencies using ECBdaily
11  
12 =head1 SYNOPSIS
13  
14 use Finance::Currency::Convert::ECBdaily;
15 $Finance::Currency::Convert::ECBdaily::CHAT = 1;
16 $_ = Finance::Currency::Convert::ECBdaily::convert(1,'USD','GBP');
17 print defined($_)? "Is $_\n" : "Error.";
18 exit;
19  
20 # See the currencies in a dirty way:
21 use Finance::Currency::Convert::ECBdaily;
22 use Data::Dumper;
23 warn %Finance::Currency::Convert::ECBdaily::currencies;
24 exit;
25  
26 =head1 DESCRIPTION
27  
28 Using ECBdaily, converts a sum between two currencies.
29  
30 =cut
31  
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=2.6
37  
38 #
39 # Glabal variables
40 #
41  
42 $CHAT = 0; # Set for real-time notes to STDERR
43  
44 # Should have been %CURRENCIES but too late now.
45 our %CURRENCIES = %currencies = (
46 'USD'=>'US Dollar', 'JPY'=>'Japanese Yen', 'BGN'=>'Bulgarian Lev equals',
47 'CZK'=>'Czech Koruna', 'DKK'=>'Danish Krone', 'GBP'=>'British Pound',
48 'HUF'=>'Hungarian Forint', 'PLN'=>'Polish Zloty', 'RON'=>'Romanian Leu',
49 'SEK'=>'Swedish Krona', 'CHF'=>'Swiss Franc', 'ISK'=>'Iceland Krona',
50 'NOK'=>'Norwegian Krone', 'HRK'=>'Croatian Kuna', 'RUB'=>'Russian Rouble',
51 'TRY'=>'Turkish Lira', 'AUD'=>'Australian Dollar', 'BRL'=>'Brazilian Real',
52 'CAD'=>'Canadian Dollar', 'CNY'=>'Chinese Yuan', 'HKD'=>'Hong Kong Dollar',
53 'IDR'=>'Indonesian Rupiah', 'ILS'=>'Israeli Shekel', 'INR'=>'Indian Rupee',
54 'KRW'=>'Korean Won', 'MXN'=>'Mexican Peso', 'MYR'=>'Malaysian Ringgit',
55 'NZD'=>'New Zealand Dollar', 'PHP'=>'Philippine Peso', 'SGD'=>'Singapore Dollar',
56 'THB'=>'Thai Baht', 'ZAR'=>'South African Rand', 'EUR'=>'Euro'
57 );
58  
59  
60 =head1 USE
61  
62 Call the module's C<&convert> routine, supplying three arguments:
63 the amount to convert, and the currencies to convert from and to.
64  
65 Codes are used to identify currencies: you may view them in the
66 values of the C<%currencies> hash, where keys are descriptions of
67 the currencies.
68  
69 In the event that attempts to convert fail, you will recieve C<undef>
70 in response, with errors going to STDERR, and notes displayed if
71 the modules global C<$CHAT> is defined.
72  
73 =head2 SUBROUTINE convert
74  
75 $value = &convert( $amount_to_convert, $from, $to);
76  
77 Requires the sum to convert, and two symbols to represent the source
78 and target currencies.
79  
80 In more detail, access L<https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml>,
81 where the value of C<s> (in the example, C<GBPUSD>) is the value of the source
82 and target currencies, and the rest is stuff I've not looked into....
83  
84 =cut
85  
86 sub convert {
87 my ($amount, $from, $to) = (shift,shift,shift);
88  
89 die "\nPlease call as ...::convert(\$amount,\$from,\$to) " unless (defined $amount and defined $from and defined $to);
90 carp "\nNo such currency code as <$from>." and return undef if not exists $currencies{$from};
91 carp "\nNo such currency code as <$to>." and return undef if not exists $currencies{$to};
92 carp "\nPlease supply a positive sum to convert <received $amount>." and return undef if $amount<0;
93 warn "\nConverting <$amount> from <$from> to <$to> " if $CHAT;
94  
95 my ($value);
96 for my $attempt (0..3){
97 warn "\nAttempt $attempt ...\n" if $CHAT;
98 $value = _get_document_xml($amount,$from,$to);
99 # Can't really say "last if defined $doc" as $doc may be a ECBdaily 404-like error?
100 last if defined $value;
101 }
102 return $value;
103 }
104  
105  
106 #
107 # PRIVATE SUB _get_document_xml
108 # Accepts: amount, starting currency, target currency
109 # Returns: HTML content
110 # URI: https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
111 #
112 sub _get_document_xml {
113  
114 my ($amount,$from,$to) = (shift,shift,shift);
115 die "get_document requires a \$amount,\$from_currency,\$target_currency arrity" unless (defined $amount and defined $to and defined $from);
116  
117 my $url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
118 warn "\nAttempting to access <$url> ...\n" if $CHAT;
119  
120 my $content = get $url or die "Unable to get $url\n";
121  
122 my $xml = new XML::Simple;
123 my $data = $xml->XMLin($content);
124 my $cube = $data->{Cube}->{Cube}->{Cube};
125
126 my $r1 = _get_rate($cube, $from);
127 die "\ncan not find rate for currency $from" unless $r1;
128
129 my $r2 = _get_rate($cube, $to);
130 die "\ncan not find rate for currency $to" unless $r2;
131  
132 my $r = $r2/$r1;
133  
134 return $amount * $r;
135 }
136  
137 sub _get_rate {
138  
139 my ($cube, $cur) = (shift,shift);
140  
141 if( $cur eq 'EUR') {
142 return 1;
143 }
144  
145 my $r = 0;
146 for my $n ( @{$cube} ) {
147 if( $n->{currency} eq $cur ) {
148 $r = $n->{rate};
149 last;
150 }
151 }
152  
153 $r =~ s/^\s*([\d.]+)\s*$/$1/sg;
154 if ($r eq ''){
155 warn "\n...document contained no data/unexpected data for $cur\n";
156 return undef;
157 }
158  
159 return $r;
160 }
161  
162 =head1 EXPORTS
163  
164 None.
165  
166 =head1 REVISIONS
167  
168 Please see the enclosed file CHANGES.
169  
170 =head1 PROBLEMS?
171  
172 If this doesn't work, www.ecb.europa.eu have probably changed their URI or HTML format.
173 Let me know and I'll fix the code. Or by all means send a patch.
174 Please don't just post a bad review on CPAN, I don't get CC'd them.
175  
176 =head1 SEE ALSO
177  
178 L<LWP::UserAgent>: L<HTTP::Request>: L<JSON>;
179 L<https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html>.
180  
181 =head1 AUTHOR
182  
183 berlin3, details -at- cpan -dot- org.
184  
185 =head1 COPYRIGHT
186  
187 Copyright (C) details, 2018, ff. - All Rights Reserved.
188  
189 This library is free software and may be used only under the same terms as Perl itself.
190  
191 =cut
192  
193  
194 # $Finance::Currency::Convert::ECBdaily::CHAT=1;
195 # print Finance::Currency::Convert::ECBdaily::convert(1,'EUR','GBP');
196  
197 1;
198  
199 __END__
↑ Summary