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.07;
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 unless (defined $amount and defined $to and defined $from) {
116 warn "get_document requires a \$amount,\$from_currency,\$target_currency arrity";
117 return undef;
118 };
119  
120 my $url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
121 warn "\nAttempting to access <$url> ...\n" if $CHAT;
122  
123 my $content = get $url;
124 unless (defined $content) {
125 warn "Unable to get $url\n";
126 return undef;
127 }
128  
129 my $xml = new XML::Simple;
130 my $data = $xml->XMLin($content);
131 my $cube = $data->{Cube}->{Cube}->{Cube};
132
133 my $r1 = _get_rate($cube, $from);
134 unless($r1) {
135 warn "\nwarn:: can not find rate for currency $from";
136 return undef;
137 }
138
139 my $r2 = _get_rate($cube, $to);
140 unless( $r2) {
141 warn "\nwarn:: can not find rate for currency $from or $to";
142 return undef;
143 }
144  
145 my $r = $r2/$r1;
146  
147 return $amount * $r;
148 }
149  
150 sub _get_rate {
151  
152 my ($cube, $cur) = (shift,shift);
153  
154 if( $cur eq 'EUR') {
155 return 1;
156 }
157  
158 my $r = 0;
159 for my $n ( @{$cube} ) {
160 if( $n->{currency} eq $cur ) {
161 $r = $n->{rate};
162 last;
163 }
164 }
165  
166 $r =~ s/^\s*([\d.]+)\s*$/$1/sg;
167 if ($r eq ''){
168 warn "\n...document contained no data/unexpected data for $cur\n";
169 return undef;
170 }
171  
172 return $r;
173 }
174  
175 =head1 EXPORTS
176  
177 None.
178  
179 =head1 REVISIONS
180  
181 Please see the enclosed file CHANGES.
182  
183 =head1 PROBLEMS?
184  
185 If this doesn't work, www.ecb.europa.eu have probably changed their URI or HTML format.
186 Let me know and I'll fix the code. Or by all means send a patch.
187 Please don't just post a bad review on CPAN, I don't get CC'd them.
188  
189 =head1 SEE ALSO
190  
191 L<LWP::UserAgent>: L<HTTP::Request>: L<JSON>;
192 L<https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html>.
193  
194 =head1 AUTHOR
195  
196 berlin3, details -at- cpan -dot- org.
197  
198 =head1 COPYRIGHT
199  
200 Copyright (C) details, 2018, ff. - All Rights Reserved.
201  
202 This library is free software and may be used only under the same terms as Perl itself.
203  
204 =cut
205  
206  
207 # $Finance::Currency::Convert::ECBdaily::CHAT=1;
208 # print Finance::Currency::Convert::ECBdaily::convert(1,'EUR','GBP');
209  
210 1;
211  
212 __END__
↑ Summary