line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id$ |
2
|
|
|
|
|
|
|
package Finance::Currency::Convert::WebserviceX; |
3
|
1
|
|
|
1
|
|
29948
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
5
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
46
|
|
6
|
1
|
|
|
1
|
|
2915
|
use LWP::UserAgent (); |
|
1
|
|
|
|
|
154274
|
|
|
1
|
|
|
|
|
30
|
|
7
|
1
|
|
|
1
|
|
2359
|
use Memoize::Expire (); |
|
1
|
|
|
|
|
13545
|
|
|
1
|
|
|
|
|
1488
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$VERSION = '0.07001'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $expires = tie my %cache => 'Memoize::Expire', LIFETIME => 300; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
15
|
0
|
|
0
|
|
|
|
my $self = bless{response => undef}, ref $class || $class; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
18
|
0
|
|
|
|
|
|
$ua->timeout(10); |
19
|
0
|
|
|
|
|
|
$ua->env_proxy; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
$self->{'ua'} = $ua; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
return $self; |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub cache { |
27
|
0
|
|
|
0
|
1
|
|
return \%cache; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub convert { |
31
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
32
|
0
|
|
0
|
|
|
|
my $value = shift || ''; |
33
|
0
|
|
0
|
|
|
|
my $from = shift || ''; |
34
|
0
|
|
0
|
|
|
|
my $to = shift || ''; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$from = uc($from); |
37
|
0
|
|
|
|
|
|
$to = uc($to); |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my $key = "$from-$to"; |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
0
|
|
|
|
return unless length $value && $from =~ /^[A-Z]{3}$/ && $to =~ /^[A-Z]{3}$/; |
|
|
|
0
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if ($from eq $to) { |
44
|
0
|
|
|
|
|
|
return $value; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if (exists $cache{$key}) { |
48
|
0
|
|
|
|
|
|
return $cache{$key}*$value; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $uri = sprintf('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=%s&ToCurrency=%s', $from, $to); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
eval { |
54
|
0
|
|
|
|
|
|
$self->{'response'} = undef; |
55
|
0
|
|
|
|
|
|
$self->{'response'} = $self->{'ua'}->get($uri); |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
return if $@; |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if (!$self->{'response'}->is_success) { |
61
|
0
|
|
|
|
|
|
return; |
62
|
|
|
|
|
|
|
} else { |
63
|
0
|
0
|
0
|
|
|
|
if (($self->{'response'}->content || '') =~ /(.*)<\/double>/i) { |
64
|
0
|
|
0
|
|
|
|
my $rate = ($1 || 1); |
65
|
0
|
|
|
|
|
|
$cache{$key} = $rate; |
66
|
0
|
|
|
|
|
|
return $rate*$value; |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
|
return; |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
__END__ |