blib/lib/Parse/PayPal/TxDetailReport/Old.pm

filter: Statementstmt 25.0% 20 / 80 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 62.5% 5 / 8 Podpod 100% 1 / 1 totaltotal 18.5% 26 / 140 CC 24 SCAR 47.2
CC
24
Cov
16%
parse_paypal_old_txdetail_report
210.0
__ANON__
56.0
_parse_date
30.0
CRAP 112.5
↑ Summary
1 package Parse::PayPal::TxDetailReport::Old;
2  
3 1 our $DATE = '2018-11-07'; # DATE
4 1 our $VERSION = '0.002'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = qw(parse_paypal_old_txdetail_report);
12  
13 1 ▶use DateTime; # XXX find a more lightweight alternative
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our %SPEC;
16  
17 0 ▶sub _parse_date {
Subroutine _parse_date: not called CC=5 SCAR=34.0
18 0 my ($fmt, $date) = @_;
19 0 ▶ if ($fmt eq 'MM/DD/YYYY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq 'MM/DD/YYYY') { } 0 0 0
elsif ($fmt eq 'DD/MM/YYYY') { } 0 0 0
20 0 ▶ $date =~ m!^(\d\d?)/(\d\d?)/(\d\d\d\d)$!
Branch: 1 branch
BranchTrueFalseTotal
$date =~ m[^(\d\d?)/(\d\d?)/(\d\d\d\d)$] or die "Invalid date format in '${date}', must be MM/DD/YYYY" 0 0 0
21 or die "Invalid date format in '$date', must be MM/DD/YYYY";
22 0 return DateTime->new(year => $3, month => $1, day => $2)->epoch;
23 } elsif ($fmt eq 'DD/MM/YYYY') {
24 0 ▶ $date =~ m!^(\d\d?)/(\d\d?)/(\d\d\d\d)$!
Branch: 1 branch
BranchTrueFalseTotal
$date =~ m[^(\d\d?)/(\d\d?)/(\d\d\d\d)$] or die "Invalid date format in '${date}', must be DD/MM/YYYY" 0 0 0
25 or die "Invalid date format in '$date', must be DD/MM/YYYY";
26 0 return DateTime->new(year => $3, month => $2, day => $1)->epoch;
27 } else {
28 0 die "Unknown date format, please use MM/DD/YYYY or DD/MM/YYYY";
29 }
30 }
31  
32 $SPEC{parse_paypal_old_txdetail_report} = {
33 1 v => 1.1,
34 summary => 'Parse PayPal transaction detail report (older version, 2015 and earlier) into data structure',
35 description => <<'_',
36  
37 The result will be a hashref. The main key is `transactions` which will be an
38 arrayref of hashrefs.
39  
40 Dates will be converted into Unix timestamps.
41  
42 _
43 args => {
44 file => {
45 schema => 'filename*',
46 description => <<'_',
47  
48 File can all be in tab-separated or comma-separated (CSV) format.
49  
50 _
51 pos => 0,
52 },
53 string => {
54 schema => ['str*'],
55 description => <<'_',
56  
57 Instead of `file`, you can alternatively provide the file content in `string`.
58  
59 _
60 },
61 format => {
62 schema => ['str*', in=>[qw/tsv csv/]],
63 description => <<'_',
64  
65 If unspecified, will be deduced from the first filename's extension (/csv/ for
66 CSV, or /txt|tsv/ for tab-separated).
67  
68 _
69 },
70 date_format => {
71 schema => ['str*', in=>['MM/DD/YYYY', 'DD/MM/YYYY']],
72 default => 'MM/DD/YYYY',
73 },
74 },
75 args_rels => {
76 req_one => ['file', 'string'],
77 },
78 };
79 0 ▶sub parse_paypal_old_txdetail_report {
Subroutine parse_paypal_old_txdetail_report: not called CC=14 SCAR=53.5
Pod documented
80 0 my %args = @_;
81  
82 0 my $format = $args{format};
83 0 ▶ my $date_format = $args{date_format} // 'MM/DD/YYYY';
Condition0%0/2
$args{"date_format"} // "MM/DD/YYYY"
l!l
00
MC/DC0%0/1
$args{"date_format"} // "MM/DD/YYYY"
$args{"date_format"}
Truth table0%0/2
A
A= $args{"date_format"}
Aresult
11
00
84  
85 0 my $fh;
86 my $file;
87 0 ▶ if (defined(my $str = $args{string})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $str = $args{'string'})) { } 0 0 0
elsif (defined(my $file0 = $args{'file'})) { } 0 0 0
88 0 require IO::Scalar;
89 0 require String::BOM;
90  
91 0 ▶ if (!$format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 0 0 0
92 0 ▶ $format = $str =~ /\t/ ? 'tsv' : 'csv';
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /\t/ ? : 0 0 0
93 }
94 0 $str = String::BOM::strip_bom_from_string($str);
95 0 $fh = IO::Scalar->new(\$str);
96 0 $file = "string";
97 } elsif (defined(my $file0 = $args{file})) {
98 0 require File::BOM;
99  
100 0 ▶ if (!$format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 0 0 0
101 0 ▶ $format = $file0 =~ /\.(csv)\z/i ? 'csv' : 'tsv';
Branch: 1 branch
BranchTrueFalseTotal
$file0 =~ /\.(csv)\z/i ? : 0 0 0
102 }
103 0 ▶ open $fh, "<:encoding(utf8):via(File::BOM)", $file0
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(utf8):via(File::BOM)", $file0 or return [500, "Can't open file '${file}': $!"] 0 0 0
104 or return [500, "Can't open file '$file': $!"];
105 0 $file = $file0;
106 } else {
107 0 return [400, "Please specify file (or string)"];
108 }
109  
110 0 my $res = [200, "OK", {
111 format => "txdetail_old",
112 transactions => [],
113 }];
114  
115 0 ▶ my $code_parse_row = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
116 0 my ($rownum, $row) = @_;
117  
118 0 ▶ if ($rownum == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rownum == 1) 0 0 0
119 0 ▶ unless (@$row > 10 && $row->[0] eq 'Date') {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$row > 10 and $row->[0] eq "Date") 0 0 0
Condition0%0/3
@$row > 10 and $row->[0] eq "Date"
!ll&&!rl&&r
000
MC/DC0%0/2
@$row > 10 and $row->[0] eq "Date"
@$row > 10 $row->[0] eq "Date"
Truth table0%0/3
A and B
A= @$row > 10B= $row->[0] eq "Date"
ABresult
0X0
100
111
120 0 $res = [400, "Doesn't look like old transaction detail ".
121 "format, I expect first row to be column names ".
122 "and first column header to be Date"];
123 0 goto RETURN_RES;
124 }
125 0 $res->[2]{_transaction_columns} = $row;
126 0 return;
127 }
128 0 my $tx = {};
129 0 my $txcols = $res->[2]{_transaction_columns};
130 0 for (0..$#{$row}) {
131 0 my $field = $txcols->[$_];
132 0 ▶ if ($field =~ /Date$/ && $row->[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /Date$/ and $row->[$_]) { } 0 0 0
Condition0%0/3
$field =~ /Date$/ and $row->[$_]
!ll&&!rl&&r
000
MC/DC0%0/2
$field =~ /Date$/ and $row->[$_]
$field =~ /Date$/ $row->[$_]
Truth table0%0/3
A and B
A= $field =~ /Date$/B= $row->[$_]
ABresult
0X0
100
111
133 0 $tx->{$field} = _parse_date($date_format, $row->[$_]);
134 } else {
135 0 $tx->{$field} = $row->[$_];
136 }
137 }
138 0 push @{ $res->[2]{transactions} }, $tx;
139 0 };
140  
141 0 my $csv;
142 0 ▶ if ($format eq 'csv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "csv") 0 0 0
143 0 require Text::CSV;
144 0 ▶ $csv = Text::CSV->new({binary=>1})
Branch: 1 branch
BranchTrueFalseTotal
$csv = "Text::CSV"->new({"binary", 1}) or return [500, 'Cannot use CSV: ' . "Text::CSV"->error_diag] 0 0 0
145 or return [500, "Cannot use CSV: ".Text::CSV->error_diag];
146 }
147  
148 0 ▶ if ($format eq 'csv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'csv') { } 0 0 0
149 0 my $rownum = 0;
150 0 ▶ while (my $row = $csv->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 0 0 0
151 0 $rownum++;
152 0 $code_parse_row->($rownum, $row);
153 }
154 } else {
155 0 my $rownum = 0;
156 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
157 0 $rownum++;
158 0 chomp($line);
159 0 $code_parse_row->($rownum, [split /\t/, $line]);
160 }
161 }
162 0 delete $res->[2]{_transaction_columns};
163  
164 0 RETURN_RES:
165 $res;
166 }
167  
168 1 1;
169 # ABSTRACT: Parse PayPal transaction detail report (older version, 2015 and earlier) into data structure
170  
171 __END__
↑ Summary