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

filter: Statementstmt 66.2% 59 / 89 Branchbran 21.4% 9 / 42 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 53.9% 89 / 165 SCAR 46.4
CC
25
Cov
50%
update
171.1
new
4.3
rate
4.2
CRAP 103.1
↑ Summary
1 package Finance::Currency::Convert::BChile;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Finance::Currency::Convert::BChile - Currency conversion module
11 between Chilean Pesos (CLP) and USA Dollars (USD).
12  
13 =head1 VERSION
14  
15 Version 0.05
16  
17 =cut
18  
19 7 $Finance::Currency::Convert::BChile::VERSION = '0.05';
20  
21 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # As of 2013-07-24, bcentral.cl uses an iframe in homepage and
25 # feeds values from another URL
26 7 my $BCENTRAL_URL = 'http://si3.bcentral.cl/indicadoresvalores/secure/indicadoresvalores.aspx';
27 7 my $DEFAULT_UA = 'Finance::Currency::Convert::BChile perl module';
28  
29 =head1 SYNOPSIS
30  
31 Currency conversion module between Chilean Pesos (CLP) and USA
32 Dollars (USD). The conversion rate is obtained from the official
33 source in Chile: the central bank "Banco Central de Chile", from
34 their webpage http://www.bcentral.cl
35  
36 use Finance::Currency::Convert::BChile;
37  
38 my $conversor = Finance::Currency::Convert::BChile->new();
39 $conversor->update;
40 print $conversor->CLP2USD(20170);
41  
42 =head1 FUNCTIONS
43  
44 =head2 new
45  
46 Creates a new Finance::Currency::Convert::BChile object.
47 Initializes the web robot. You can pass a user agent string
48 as a parameter.
49  
50 =cut
51  
52 sub new {
53 6 ▶ my ($this, @args) = @_;
Subroutine new: called CC=4 SCAR=14.7
Pod documented
54  
55 6 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
56  
57 6 ▶ my $ua_string = $args[0] || $DEFAULT_UA;
Condition33%1/3
$args[0] || $DEFAULT_UA
l!l&&r!l&&!r
060
MC/DC0%0/2
$args[0] || $DEFAULT_UA
$args[0] $DEFAULT_UA
Truth table33%1/3
A || B
A= $args[0]B= $DEFAULT_UA
ABresult
1X1
011
000
58  
59 6 my $self = {};
60 6 bless $self, $class;
61  
62 6 ▶ $self->{'ua'} = LWP::UserAgent->new
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ua"} = "LWP::UserAgent"->new or croak("Unable to create user agent") 0 6 6
63 or croak "Unable to create user agent";
64 6 $self->{'ua'}->agent($ua_string);
65  
66 6 $self->{'dolar'} = '';
67 6 $self->{'fecha'} = '';
68  
69 6 return $self;
70 }
71  
72 =head2 update
73  
74 Obtains the data from bcentral page.
75 You can pass a fake value as a parameter. In this case there's no
76 update from bcentral site.
77  
78 =cut
79  
80 sub update {
81 5 ▶ my $self = shift;
Subroutine update: called CC=15 SCAR=51.4
Pod documented
82 5 my $simulate = shift;
83  
84 5 ▶ if ($simulate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simulate) 5 0 5
85 5 $self->{'dolar'} = $simulate;
86 5 $self->{'fecha'} = 'UNKNOWN';
87  
88 5 return 1;
89 }
90  
91 0 my $response = $self->{'ua'}->get($BCENTRAL_URL);
92  
93 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
94 0 carp "Unable to get page: " . $response->status_line;
95  
96 0 $self->{'dolar'} = '';
97 0 $self->{'fecha'} = '';
98 0 return 0;
99 }
100  
101 0 ▶ my $p = HTML::TokeParser->new(\$response->content)
Branch: 1 branch
BranchTrueFalseTotal
my $p = "HTML::TokeParser"->new(\$response->content) or croak("Unable to parse response: $!") 0 0 0
102 or croak "Unable to parse response: $!";
103  
104 0 my ($dolar, $fecha);
105  
106 0 ▶ while (my $token = $p->get_tag("div")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_tag("div")) 0 0 0
107 0 ▶ next unless $token->[1]{id} eq 'ind-dia';
Branch: 1 branch
BranchTrueFalseTotal
unless $token->[1]{"id"} eq "ind-dia" 0 0 0
108  
109 0 ▶ while (my $token2 = $p->get_tag("p")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token2 = $p->get_tag("p")) 0 0 0
110 0 ▶ next unless $token2->[1]{class} eq 'published-date';
Branch: 1 branch
BranchTrueFalseTotal
unless $token2->[1]{"class"} eq "published-date" 0 0 0
111 0 last;
112 }
113 0 ▶ while (my $token2 = $p->get_tag("span")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token2 = $p->get_tag("span")) 0 0 0
114 0 ▶ next unless $token2->[1]{id} eq 'Lbl_fecha';
Branch: 1 branch
BranchTrueFalseTotal
unless $token2->[1]{"id"} eq "Lbl_fecha" 0 0 0
115 0 last;
116 }
117 0 $fecha = $p->get_text;
118  
119 0 ▶ while (my $token2 = $p->get_tag("span")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token2 = $p->get_tag("span")) 0 0 0
120 0 ▶ next unless $token2->[1]{id} eq 'RptListado_ctl03_lbl_valo';
Branch: 1 branch
BranchTrueFalseTotal
unless $token2->[1]{"id"} eq "RptListado_ctl03_lbl_valo" 0 0 0
121 0 $dolar = $p->get_text;
122 0 last;
123 }
124 }
125  
126 0 ▶ return 0 unless $dolar and $fecha;
Branch: 1 branch
BranchTrueFalseTotal
unless $dolar and $fecha 0 0 0
Condition0%0/3
$dolar and $fecha
!ll&&!rl&&r
000
MC/DC0%0/2
$dolar and $fecha
$dolar $fecha
Truth table0%0/3
A and B
A= $dolarB= $fecha
ABresult
0X0
100
111
127 0 ▶ return 0 unless $dolar =~ /^\d+[,.]{0,1}\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dolar =~ /^\d+[,.]{0,1}\d+$/ 0 0 0
128  
129 0 $self->{'dolar'} = $dolar;
130 0 $self->{'fecha'} = $fecha;
131  
132 0 return 1;
133 }
134  
135 =head2 date
136  
137 Return the date obtained from Banco Central's site.
138 There's no formatting at all.
139  
140 =cut
141  
142 sub date {
143 1 ▶ my $self = shift;
Subroutine date: called CC=1 SCAR=0.0
Pod documented
144  
145 1 return $self->{'fecha'};
146 }
147  
148 =head2 dollarValue
149  
150 Return the value of 1 dollar in CLP.
151 There's no formatting.
152  
153 =cut
154  
155 sub dollarValue {
156 1 ▶ my $self = shift;
Subroutine dollarValue: called CC=1 SCAR=0.0
Pod documented
157  
158 1 return $self->{'dolar'};
159 }
160  
161 =head2 rate
162  
163 Return the rate of transforming CLP in USD.
164 If there's no value data, return -1
165  
166 =cut
167  
168 sub rate {
169 2 ▶ my $self = shift;
Subroutine rate: called CC=4 SCAR=14.3
Pod documented
170  
171 2 my $valor = $self->{'dolar'};
172  
173 # Changing decimal separator
174 2 $valor =~ s/,/./;
175  
176 2 ▶ return -1 unless $valor;
Branch: 1 branch
BranchTrueFalseTotal
unless $valor 0 2 2
177 2 ▶ return -1 unless $valor =~ /^\d+[,.]{0,1}\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $valor =~ /^\d+[,.]{0,1}\d+$/ 0 2 2
178  
179 2 ▶ return -1 unless $valor >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $valor >= 0 0 2 2
180  
181 2 return 1 / $valor;
182 }
183  
184 =head2 CLP2USD
185  
186 Convert the amount received as parameter (CLP) to USD,
187 rounded at two decimals.
188 Returns -1 on error.
189  
190 =cut
191  
192 sub CLP2USD {
193 1 ▶ my $self = shift;
Subroutine CLP2USD: called CC=3 SCAR=12.0
Pod documented
194 1 my $pesos = shift;
195  
196 1 ▶ if ($pesos !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pesos =~ /^\d+$/) 0 1 1
197 0 carp "Argument to CLP2USD must be an integer";
198 0 return -1;
199 }
200  
201 1 my $rate = $self->rate;
202 1 ▶ return -1 unless $rate >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate >= 0 0 1 1
203  
204 1 return sprintf("%.2f", $pesos * $rate);
205 }
206  
207 =head2 USD2CLP
208  
209 Convert the amount received as parameter (USD) to CLP,
210 rounded as integer (no decimals).
211 Returns -1 on error.
212  
213 =cut
214  
215 sub USD2CLP {
216 1 ▶ my $self = shift;
Subroutine USD2CLP: called CC=3 SCAR=11.8
Pod documented
217 1 my $dolares = shift;
218  
219 1 ▶ if ($dolares !~ /^\d+(\.\d{0,2}){0,1}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dolares =~ /^\d+(\.\d{0,2}){0,1}$/) 0 1 1
220 0 carp "Wrong format in argument to USD2CLP";
221 0 return -1;
222 }
223  
224 1 my $valor = $self->{'dolar'};
225  
226 # Changing decimal separator
227 1 $valor =~ s/,/./;
228  
229 1 ▶ return -1 unless $valor >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $valor >= 0 0 1 1
230  
231 1 return sprintf("%.0f", $dolares * $valor);
232 }
233  
234 =head1 AUTHOR
235  
236 Hugo Salgado, C<< <hsalgado at vulcano.cl> >>
237  
238 =head1 BUGS
239  
240 Please report any bugs or feature requests to C<bug-finance-currency-convert-bchile at rt.cpan.org>, or through
241 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-Currency-Convert-BChile>. I will be notified, and then you'll
242 automatically be notified of progress on your bug as I make changes.
243  
244 =head1 SUPPORT
245  
246 You can find documentation for this module with the perldoc command.
247  
248 perldoc Finance::Currency::Convert::BChile
249  
250  
251 You can also look for information at:
252  
253 =over 4
254  
255 =item * RT: CPAN's request tracker
256  
257 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-Currency-Convert-BChile>
258  
259 =item * AnnoCPAN: Annotated CPAN documentation
260  
261 L<http://annocpan.org/dist/Finance-Currency-Convert-BChile>
262  
263 =item * CPAN Ratings
264  
265 L<http://cpanratings.perl.org/d/Finance-Currency-Convert-BChile>
266  
267 =item * Search CPAN
268  
269 L<http://search.cpan.org/dist/Finance-Currency-Convert-BChile>
270  
271 =back
272  
273 =head1 SEE ALSO
274  
275 HTML::TokeParser
276 LWP::UserAgent
277  
278 =cut
279  
280 =head1 TERMS OF USE
281  
282 The data obtained from bcentral site is under this policy:
283 http://www.bcentral.cl/sitio/condiciones-uso.htm
284  
285 No liability is accepted by the author for abuse or miuse of
286 the software herein. Use of this software is only permitted under
287 the terms stipulated by bcentral.cl.
288  
289 =cut
290  
291 =head1 ACKNOWLEDGEMENTS
292  
293 This module was built for NIC Chile (http://www.nic.cl), who
294 granted its liberation as free software.
295  
296 =head1 COPYRIGHT & LICENSE
297  
298 Copyright 2007 Hugo Salgado, all rights reserved.
299  
300 This program is free software; you can redistribute it and/or modify it
301 under the same terms as Perl itself.
302  
303 =cut
304  
305 7 1; # End of Finance::Currency::Convert::BChile
↑ Summary