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

filter: Statementstmt 61.7% 29 / 47 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 60.5% 43 / 71 CC 10 SCAR 20.0
CC
10
Cov
56%
convert_currency
13.8
get_currencies
6.4
_fetch_currency_exchange_web_page
6.0
CRAP 7.4
↑ Summary Build »
1 package Finance::Currency::Convert::SCSB;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = v0.1.0;
7  
8 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 our @EXPORT_OK = qw(get_currencies convert_currency);
10  
11 3 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub get_currencies {
15 2 ▶ my ($error, $result);
Subroutine get_currencies: called CC=4 SCAR=18.5
Pod documented
16  
17 2 my $dom;
18 eval {
19 2 $dom = _fetch_currency_exchange_web_page();
20 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dom = _fetch_currency_exchange_web_page() } } or do { $error = $@ } 2 0 2
21 2 $error = $@;
22 };
23 2 ▶ return ($error, undef) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 0 2
24  
25 0 my @col_names = qw(zh_currency_name en_currency_name buy_at sell_at);
26 0 my @cols = (
27 $dom->find("td.txt09 > span")->map('all_text')->to_array(),
28 $dom->find("td.txt09 + td > span")->map('all_text')->to_array(),
29 $dom->find("td.txt09 + td + td.txt101 > span")->map('all_text')->to_array(),
30 $dom->find("td.txt09 + td + td.txt101 + td.txt101 > span")->map('all_text')->to_array(),
31 );
32  
33 0 my @rows = ();
34 0 for my $i (0..$#{$cols[0]}) {
35 0 push @rows, {
36 zh_currency_name => $cols[0][$i],
37 en_currency_name => $cols[1][$i],
38 buy_at => $cols[2][$i],
39 sell_at => $cols[3][$i],
40 }
41 }
42  
43 0 return (undef, \@rows);
44 }
45  
46 sub convert_currency {
47 2 ▶ my ($amount, $from_currency, $to_currency) = @_;
Subroutine convert_currency: called CC=6 SCAR=26.2
Pod documented
48 2 ▶ return ("The convertion target must be 'TWD'. Cannot proceed with '$to_currency'", undef) unless $to_currency eq 'TWD';
Branch: 1 branch
BranchTrueFalseTotal
unless $to_currency eq "TWD" 1 1 2
49  
50 1 my $dom;
51 1 my ($error, $result) = get_currencies();
52 1 ▶ return ($error, undef) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 0 1
53  
54 0 my $rate;
55 0 for (@$result) {
56 0 ▶ if ($_->{en_currency_name} eq $from_currency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"en_currency_name"} eq $from_currency) 0 0 0
57 0 $rate = $_;
58 0 last;
59 }
60 }
61 0 ▶ return ("Unknown currency: $from_currency", undef) unless $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate 0 0 0
62  
63 0 return (undef, $amount * $rate->{buy_at});
64 }
65  
66 sub _fetch_currency_exchange_web_page {
67 0 ▶ my $ua = Mojo::UserAgent->new;
Subroutine _fetch_currency_exchange_web_page: not called CC=2 SCAR=17.9
68 0 my $result = $ua->get('https://ibank.scsb.com.tw/netbank.portal?_nfpb=true&_pageLabel=page_other12&_nfls=false')->result;
69 0 ▶ die $result->message if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
70 0 return $result->dom;
71 }
72  
73 3 1;
74  
75 __END__
↑ Summary Build »