| 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
| |||||||||||
| 21 | 2 | $error = $@; | |||||||||
| 22 | }; | ||||||||||
| 23 | 2 | ▶ | return ($error, undef) if defined $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 49 | |||||||||||
| 50 | 1 | my $dom; | |||||||||
| 51 | 1 | my ($error, $result) = get_currencies(); | |||||||||
| 52 | 1 | ▶ | return ($error, undef) if defined $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | my $rate; | |||||||||
| 55 | 0 | for (@$result) { | |||||||||
| 56 | 0 | ▶ | if ($_->{en_currency_name} eq $from_currency) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | $rate = $_; | |||||||||
| 58 | 0 | last; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | 0 | ▶ | return ("Unknown currency: $from_currency", undef) unless $rate; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 70 | 0 | return $result->dom; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 3 | 1; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||