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

filter: Statementstmt 37.8% 42 / 111 Branchbran 10.5% 4 / 38 Conditioncond 4.0% 1 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 64.2% 9 / 14 Podpod 100% 5 / 5 totaltotal 29.3% 61 / 208 CC 33 SCAR 45.8
CC
33
Cov
27%
convert
240.0
_extract_text
72.0
add_currencies
42.0
CRAP 97.7
↑ Summary
1 package Finance::Currency::Convert::XE;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 $VERSION = '0.25';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 Finance::Currency::Convert::XE - Currency conversion module.
14  
15 =head1 SYNOPSIS
16  
17 use Finance::Currency::Convert::XE;
18 my $obj = Finance::Currency::Convert::XE->new()
19 || die "Failed to create object\n" ;
20  
21 my $value = $obj->convert(
22 'source' => 'GBP',
23 'target' => 'EUR',
24 'value' => '123.45',
25 'format' => 'text'
26 ) || die "Could not convert: " . $obj->error . "\n";
27  
28 my @currencies = $obj->currencies;
29  
30 or
31  
32 use Finance::Currency::Convert::XE;
33 my $obj = Finance::Currency::Convert::XE->new(
34 'source' => 'GBP',
35 'target' => 'EUR',
36 'format' => 'text'
37 ) || die "Failed to create object\n" ;
38  
39 my $value = $obj->convert(
40 'value' => '123.45',
41 'format' => 'abbv'
42 ) || die "Could not convert: " . $obj->error . "\n";
43  
44 $value = $obj->convert('123.45')
45 || die "Could not convert: " . $obj->error . "\n";
46  
47 my @currencies = $obj->currencies;
48  
49 =head1 DESCRIPTION
50  
51 Currency conversion module using XE.com's Universal Currency Converter (tm)
52 site.
53  
54 WARNING: Do not use this module for any commercial purposes, unless you have
55 obtain an explicit license to use the service provided by XE.com. For further
56 details please read the Terms and Conditions available at L<http://www.xe.com>.
57  
58 =over
59  
60 =item * http://www.xe.com/errors/noautoextract.htm
61  
62 =back
63  
64 =cut
65  
66 #--------------------------------------------------------------------------
67  
68 ###########################################################################
69 #Library Modules #
70 ###########################################################################
71  
72 7 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 7 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 ###########################################################################
76 #Constants #
77 ###########################################################################
78  
79 7 ▶use constant UCC => 'http://www.xe.com/currencyconverter';
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 ###########################################################################
82 #Variables #
83 ###########################################################################
84  
85 7 my %currencies; # only need to load once!
86 7 my @defaults = ('source', 'target', 'format');
87  
88 7 my $web = WWW::Mechanize->new();
89 7 $web->agent_alias( 'Mac Safari' );
90  
91 #--------------------------------------------------------------------------
92  
93 ###########################################################################
94 #Interface Functions #
95 ###########################################################################
96  
97 =head1 METHODS
98  
99 =over 4
100  
101 =item new
102  
103 Creates a new Finance::Currency::Convert::XE object. Can be supplied with
104 default values for source and target currency, and the format required of the
105 output. These can be overridden in the convert() method.
106  
107 =cut
108  
109 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
110 2 my ($this, @args) = @_;
111 2 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
112 2 my $self = {};
113 2 bless $self, $class;
114 2 $self->_initialize(@args);
115 2 return $self;
116 }
117  
118 =item currencies
119  
120 Returns a plain array of the currencies available for conversion.
121  
122 =cut
123  
124 2 ▶sub currencies {
Subroutine currencies: called CC=1 SCAR=0.0
Pod documented
125 2 my $self = shift;
126 2 return sort keys %currencies;
127 }
128  
129 =item add_currencies
130  
131 Allows the user to add currencies to the internal hash. Currencies can be added
132 as per the code below:
133  
134 $self->add_currencies(
135 ZZZ => {text => 'An Example', symbol => '$'},
136 ZZY => {text => 'Testing'}
137 );
138  
139 Note that unless otherwise stated, the symbol will be set to '&#164;'. The code
140 used must be 3 characters in length, and a text part must be included.
141  
142 =cut
143  
144 0 ▶sub add_currencies {
Subroutine add_currencies: not called CC=6 SCAR=37.4
Pod documented
145 0 my ($self,%hash) = @_;
146 0 for my $code (keys %hash) {
147 0 ▶ if($code =~ /[A-Z]{3}/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /[A-Z]{3}/i) { } 0 0 0
148 0 $code = uc $code;
149 0 ▶ if($hash{$code}->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{$code}{'text'}) { } 0 0 0
150 0 ▶ $currencies{$code}->{name} = $hash{$code}->{text} || die;
Condition0%0/2
$hash{$code}{'text'} || die()
l!l
00
MC/DC0%0/1
$hash{$code}{'text'} || die()
$hash{$code}{'text'}
Truth table0%0/2
A
A= $hash{$code}{'text'}
Aresult
11
00
151 0 ▶ $currencies{$code}->{symbol} = $hash{$code}->{symbol} || '&#164;';
Condition0%0/2
$hash{$code}{'symbol'} || '&#164;'
l!l
00
MC/DC0%0/1
$hash{$code}{'symbol'} || '&#164;'
$hash{$code}{'symbol'}
Truth table0%0/2
A
A= $hash{$code}{'symbol'}
Aresult
11
00
152 } else {
153 0 $self->{error} = "User currency '$code' has no text part";
154 }
155 } else {
156 0 $self->{error} = "User currency '$code' is invalid";
157 }
158 }
159 }
160  
161 =item convert
162  
163 Converts some currency value into another using XE.com's UCC.
164  
165 An anonymous hash is used to pass parameters. Legal hash keys and values
166 are as follows:
167  
168 convert(
169 source => $currency_from,
170 target => $currency_to,
171 value => $currency_from_value,
172 format => $print_format
173 );
174  
175 The format key is optional, and takes one of the following strings:
176  
177 'number' (returns '12.34')
178 'symbol' (returns '&#163;12.34')
179 'text' (returns '12.34 Great Britain, Pound')
180 'abbv' (returns '12.34 GBP')
181  
182 If format key is omitted, 'number' is assumed and the converted value
183 is returned.
184  
185 If only a value is passed, it is assumed that this is the value to be
186 converted and the remaining parameters will be defined by the defaults set
187 in the constructor. Note that no internal defaults are assumed.
188  
189 Note that not all countries have symbols in the standard character set.
190 Where known the appropriate currency symbol is used, otherwise the
191 generic currency symbol is used.
192  
193 It should also be noted that there is a recommendation to use only the
194 standardised three letter abbreviation ('abbv' above). However, for
195 further reading please see:
196  
197 http://www.jhall.demon.co.uk/currency/
198 http://www.jhall.demon.co.uk/currency/by_symbol.html
199  
200 =cut
201  
202 0 ▶sub convert {
Subroutine convert: not called CC=15 SCAR=54.8
Pod documented
203 0 my $self = shift;
204 0 ▶ my %params = @_ > 1 ? @_ : (value => $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
205 0 ▶ $params{$_} ||= $self->{$_} for(@defaults);
Condition0%0/3
$params{$_} ||= $self->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{$_} ||= $self->{$_}
$params{$_} $self->{$_}
Truth table0%0/3
A ||= B
A= $params{$_}B= $self->{$_}
ABresult
1X1
011
000
206  
207 0 undef $self->{error};
208 0 ▶ unless( $params{source} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"source"}) 0 0 0
209 0 $self->{error} = 'Source currency is blank. This parameter is required';
210 0 return;
211 }
212  
213 0 ▶ unless( exists($currencies{$params{source}}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $currencies{$params{"source"}}) 0 0 0
214 0 $self->{error} = 'Source currency "' . $params{source} . '" is not available';
215 0 return;
216 }
217  
218 0 ▶ unless( $params{target} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"target"}) 0 0 0
219 0 $self->{error} = 'Target currency is blank. This parameter is required';
220 0 return;
221 }
222  
223 0 ▶ unless( exists($currencies{$params{target}}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $currencies{$params{"target"}}) 0 0 0
224 0 $self->{error} = 'Target currency "' . $params{target} . '" is not available';
225 0 return;
226 }
227  
228 # store later use
229 0 $self->{code} = $params{target};
230 0 $self->{name} = $currencies{$params{target}}->{name};
231 0 $self->{symbol} = $currencies{$params{target}}->{symbol};
232 0 $self->{string} = $self->_format($params{format});
233  
234 # This "feature" is actually useful as a pass-thru filter.
235 0 ▶ if( $params{source} eq $params{target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"source"} eq $params{"target"}) 0 0 0
236 return sprintf $self->{string}, $params{value}
237 0 }
238  
239 # get the base site
240 0 $web->get( UCC );
241  
242 0 ▶ unless($web->success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($web->success) 0 0 0
243 0 $self->{error} = 'Unable to retrieve webpage';
244 0 return;
245 }
246  
247 0 my @forms = $web->forms();
248 0 my $form_number = 1;
249 0 my $found = 0;
250  
251 0 foreach my $form (@forms) {
252 0 ▶ if ($form->action eq 'http://www.xe.com/currencyconverter/convert/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->action eq "http://www.xe.com/currencyconverter/convert/") 0 0 0
253 0 $found = 1;
254 0 last;
255 }
256  
257 0 $form_number++;
258 }
259  
260 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
261 # complete and submit the form
262 $web->submit_form(
263 form_number => $form_number,
264 fields => { 'From' => $params{source},
265 'To' => $params{target},
266 'Amount' => $params{value}
267 }
268 0 );
269 }
270  
271 0 ▶ unless($found && $web->success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found and $web->success) 0 0 0
Condition0%0/3
$found and $web->success
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $web->success
$found $web->success
Truth table0%0/3
A and B
A= $foundB= $web->success
ABresult
0X0
100
111
272 0 $self->{error} = 'Unable to retrieve webform';
273 0 return;
274 }
275  
276 # return the converted value
277 0 return $self->_extract_text($web->content());
278 }
279  
280 =item error
281  
282 Returns a (hopefully) meaningful error string.
283  
284 =cut
285  
286 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
287 0 my $self = shift;
288 0 return $self->{error};
289 }
290  
291 ###########################################################################
292 #Internal Functions #
293 ###########################################################################
294  
295 2 ▶sub _initialize {
Subroutine _initialize: called CC=4 SCAR=13.9
296 2 my($self, %params) = @_;
297 # set defaults
298 2 $self->{$_} = $params{$_} for(@defaults);
299  
300 2 ▶ return if(keys %currencies);
Branch: 1 branch
BranchTrueFalseTotal
if keys %currencies 1 1 2
301 1 local($_);
302  
303 # Extract the mapping of currencies and their atrributes
304 1 ▶ while(<Finance::Currency::Convert::XE::DATA>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 169 1 170
305 169 s/\s*$//;
306 169 my ($code,$text,$symbol) = split /\|/;
307 169 $currencies{$code}->{name} = $text;
308 169 $currencies{$code}->{symbol} = $symbol;
309 }
310  
311 1 return;
312 }
313  
314 # Formats the return string to the requirements of the caller
315 0 ▶sub _format {
Subroutine _format: not called CC=3 SCAR=24.8
316 0 my($self, $form) = @_;
317  
318 my %formats = (
319 'symbol' => $self->{symbol} . '%.02f',
320 'abbv' => '%.02f ' . $self->{code},
321 'text' => '%.02f ' . $self->{name},
322 0 'number' => '%.02f',
323 );
324  
325 0 ▶ return $formats{$form} if(defined $form && $formats{$form});
Branch: 1 branch
BranchTrueFalseTotal
if defined $form and $formats{$form} 0 0 0
Condition0%0/3
defined $form and $formats{$form}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $form and $formats{$form}
defined $form $formats{$form}
Truth table0%0/3
A and B
A= defined $formB= $formats{$form}
ABresult
0X0
100
111
326 0 return '%.02f';
327 }
328  
329 # Extract the text from the html we get back from UCC and return
330 # it (keying on the fact that what we want is in the table after
331 # the faq link).
332 0 ▶sub _extract_text {
Subroutine _extract_text: not called CC=8 SCAR=42.8
333 0 my($self, $html) = @_;
334 0 my $tag;
335 0 my $p = HTML::TokeParser->new(\$html);
336  
337 # first look for the 'td' element
338 0 while (1) {
339 0 ▶ return unless ($tag = $p->get_tag('td'));
Branch: 1 branch
BranchTrueFalseTotal
unless $tag = $p->get_tag("td") 0 0 0
340 0 ▶ next unless (defined($tag->[1]{'align'}) && ($tag->[1]{'align'} eq 'left'));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag->[1]{"align"} and $tag->[1]{"align"} eq "left" 0 0 0
Condition0%0/3
defined $tag->[1]{"align"} and $tag->[1]{"align"} eq "left"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag->[1]{"align"} and $tag->[1]{"align"} eq "left"
defined $tag->[1]{"align"} $tag->[1]{"align"} eq "left"
Truth table0%0/3
A and B
A= defined $tag->[1]{"align"}B= $tag->[1]{"align"} eq "left"
ABresult
0X0
100
111
341 # this will probably be the value
342 0 my $value = $p->get_trimmed_text;
343  
344 # then make sure this has the 'span' with the target
345 # currency code
346 0 my $tag2 = $p->get_tag('span');
347 0 my $cd = $p->get_trimmed_text;
348 0 ▶ if (defined($tag2) && defined($tag2->[1]{'class'} && $tag2->[1]{class} eq 'uccResCde'
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag2 and defined($tag2->[1]{"class"} && $tag2->[1]{"class"} eq "uccResCde")) 0 0 0
Condition0%0/6
defined $tag2 and defined($tag2->[1]{"class"} && $tag2->[1]{"class"} eq "uccResCde")
!ll&&!rl&&r
000
$tag2->[1]{'class'} && $tag2->[1]{'class'} eq 'uccResCde'
!ll&&!rl&&r
000
MC/DC0%0/3
defined $tag2 and defined($tag2->[1]{"class"} && $tag2->[1]{"class"} eq "uccResCde")
defined $tag2 $tag2->[1]{'class'} $tag2->[1]{'class'} eq 'uccResCde'
Truth table0%0/4
A and B && C
A= defined $tag2B= $tag2->[1]{'class'}C= $tag2->[1]{'class'} eq 'uccResCde'
ABCresult
0XX0
10X0
1100
1111
349 )) {
350 0 ▶ if ($cd eq $self->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd eq $self->{"code"}) 0 0 0
351 # found it, return
352 0 $value =~ s/,//g;
353 0 return sprintf $self->{string}, $value;
354 }
355 }
356 }
357  
358 # didn't find anything
359 0 return;
360 }
361  
362 7 1;
363  
364 #--------------------------------------------------------------------------
365  
366 =back
367  
368 =head1 TERMS OF USE
369  
370 XE.com have a Terms of Use policy that states:
371  
372 This website is for informational purposes only and is not intended to
373 provide specific commercial, financial, investment, accounting, tax, or
374 legal advice. It is provided to you solely for your own personal,
375 non-commercial use and not for purposes of resale, distribution, public
376 display or performance, or any other uses by you in any form or manner
377 whatsoever. Unless otherwise indicated on this website, you may display,
378 download, archive, and print a single copy of any information on this
379 website, or otherwise distributed from XE.com, for such personal,
380 non-commercial use, provided it is done pursuant to the User Conduct and
381 Obligations set forth herein.
382  
383 As such this software is for personal use ONLY. No liability is accepted by
384 the author for abuse or miuse of the software herein. Use of this software
385 is only permitted under the terms stipulated by XE.com.
386  
387 The full legal document is available at L<http://www.xe.com/legal/>
388  
389 =head1 TODO
390  
391 Currency symbols are currently specified with a generic symbol, if the
392 currency symbol is unknown. Are there any other symbols available in
393 Unicode? Let me know if there are.
394  
395 =head1 SEE ALSO
396  
397 L<HTML::TokeParser>,
398 L<WWW::Mechanize>
399  
400 =head1 SUPPORT
401  
402 There are no known bugs at the time of this release. However, if you spot a
403 bug or are experiencing difficulties that are not explained within the POD
404 documentation, please submit a bug to the RT system (see link below). However,
405 it would help greatly if you are able to pinpoint problems or even supply a
406 patch.
407  
408 Fixes are dependant upon their severity and my availablity. Should a fix not
409 be forthcoming, please feel free to (politely) remind me by sending an email
410 to barbie@cpan.org .
411  
412 RT: L<http://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Currency-Convert-XE>
413  
414 =head1 AUTHOR
415  
416 Barbie, <barbie@cpan.org>
417 for Miss Barbell Productions <http://www.missbarbell.co.uk>.
418  
419 =head1 COPYRIGHT
420  
421 Copyright © 2002-2011 Barbie for Miss Barbell Productions.
422  
423 This module is free software; you can redistribute it and/or
424 modify it under the Artistic Licence v2.
425  
426 =cut
427  
428 #--------------------------------------------------------------------------
429  
430 __DATA__
↑ Summary