File Coverage

blib/lib/Finance/Currency/Convert/BI.pm
Criterion Covered Total %
statement 40 42 95.2
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 54 59 91.5


line stmt bran cond sub pod time code
1             package Finance::Currency::Convert::BI;
2              
3             our $DATE = '2017-07-10'; # DATE
4             our $VERSION = '0.04'; # VERSION
5              
6 2     2   19104 use 5.010001;
  2         7  
7 2     2   10 use strict;
  2         5  
  2         33  
8 2     2   10 use warnings;
  2         4  
  2         50  
9 2     2   7273 use Log::ger;
  2         217  
  2         13  
10              
11 2     2   3269 use DateTime::Format::Indonesian;
  2         1197759  
  2         105  
12 2     2   1391 use Parse::Number::ID qw(parse_number_id);
  2         11787  
  2         204  
13              
14 2     2   1179 use Exporter::Rinci qw(import);
  2         837  
  2         15  
15              
16             our %SPEC;
17              
18             $SPEC{':package'} = {
19             v => 1.1,
20             summary => 'Get/convert currencies from website of Indonesian Central Bank (BI)',
21             };
22              
23             $SPEC{get_jisdor_rates} = {
24             v => 1.1,
25             summary => 'Get JISDOR USD-IDR rates',
26             description => <<'_',
27             _
28             args => {
29             from_date => {
30             schema => 'date*',
31             },
32             to_date => {
33             schema => 'date*',
34             },
35             },
36             };
37             sub get_jisdor_rates {
38 2     2 1 2683 my %args = @_;
39              
40             #return [543, "Test parse failure response"];
41              
42 2         7 my $page;
43 2 100       9 if ($args{_page_content}) {
44 1         4 $page = $args{_page_content};
45             } else {
46 1         436 require Mojo::UserAgent;
47 1         377663 my $ua = Mojo::UserAgent->new;
48 1         20 my $tx = $ua->get("http://www.bi.go.id/id/moneter/informasi-kurs/referensi-jisdor/Default.aspx",
49             {'User-Agent' => 'Mozilla/4.0'});
50 1         3218560 my $res = $tx->success;
51 1 50       36 if ($res) {
52 1         10 $page = $res->body;
53             } else {
54 0         0 my $err = $tx->error;
55 0         0 return [500, "Can't retrieve BI page: $err->{code} - $err->{message}"];
56             }
57             }
58              
59             # XXX submit form if we want to set from_date & to_date
60              
61 2         930 my @res;
62             {
63 2 50       8 my ($table) = $page =~ m!<table class="table1">(.+?)</table>!s
  2         211  
64             or return [543, "Can't extract data table (table1)"];
65 2         37 while ($table =~ m!<tr>\s*<td>\s*(.+?)\s*</td>\s*<td>\s*(.+?)\s*</td>!gs) {
66 28         735 my $date = eval { DateTime::Format::Indonesian->parse_datetime($1) };
  28         128  
67 28 50       11734 $@ and return [543, "Can't parse date '$1'"];
68 28         108 my $rate = parse_number_id(text=>$2);
69 28         1275 push @res, {date=>$date->ymd, rate=>$rate};
70             }
71             }
72 2         88 [200, "OK", \@res];
73             }
74              
75             1;
76             # ABSTRACT: Get/convert currencies from website of Indonesian Central Bank (BI)
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Finance::Currency::Convert::BI - Get/convert currencies from website of Indonesian Central Bank (BI)
87              
88             =head1 VERSION
89              
90             This document describes version 0.04 of Finance::Currency::Convert::BI (from Perl distribution Finance-Currency-Convert-BI), released on 2017-07-10.
91              
92             =head1 SYNOPSIS
93              
94             use Finance::Currency::Convert::BI qw(get_jisdor_rates);
95              
96             my $res = get_jisdor_rates();
97              
98             =head1 DESCRIPTION
99              
100             B<EARLY RELEASE>.
101              
102             =head1 FUNCTIONS
103              
104              
105             =head2 get_jisdor_rates
106              
107             Usage:
108              
109             get_jisdor_rates(%args) -> [status, msg, result, meta]
110              
111             Get JISDOR USD-IDR rates.
112              
113             This function is not exported by default, but exportable.
114              
115             Arguments ('*' denotes required arguments):
116              
117             =over 4
118              
119             =item * B<from_date> => I<date>
120              
121             =item * B<to_date> => I<date>
122              
123             =back
124              
125             Returns an enveloped result (an array).
126              
127             First element (status) is an integer containing HTTP status code
128             (200 means OK, 4xx caller error, 5xx function error). Second element
129             (msg) is a string containing error message, or 'OK' if status is
130             200. Third element (result) is optional, the actual result. Fourth
131             element (meta) is called result metadata and is optional, a hash
132             that contains extra information.
133              
134             Return value: (any)
135              
136             =head1 HOMEPAGE
137              
138             Please visit the project's homepage at L<https://metacpan.org/release/Finance-Currency-Convert-BI>.
139              
140             =head1 SOURCE
141              
142             Source repository is at L<https://github.com/perlancar/perl-Finance-Currency-Convert-BI>.
143              
144             =head1 BUGS
145              
146             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Currency-Convert-BI>
147              
148             When submitting a bug or request, please include a test-file or a
149             patch to an existing test-file that illustrates the bug or desired
150             feature.
151              
152             =head1 SEE ALSO
153              
154             L<http://www.bi.go.id/>
155              
156             =head1 AUTHOR
157              
158             perlancar <perlancar@cpan.org>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is copyright (c) 2017, 2016, 2015 by perlancar@cpan.org.
163              
164             This is free software; you can redistribute it and/or modify it under
165             the same terms as the Perl 5 programming language system itself.
166              
167             =cut