Notification.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 121 CC 5 SCAR 22.0
CC
5
Cov
0%
from_email
30.0
new
2.0
email_name
2.0
CRAP 9.0
1 package Finance::Bank::SK::SLSP::Notification;
2  
3 use warnings;
4 use strict;
5  
6 our $VERSION = '0.02';
7  
8 use Email::MIME;
9 use File::Temp qw(tempdir);
10 use Path::Class qw(file dir);
11 use Archive::Extract;
12 use File::Find::Rule;
13 use Encode 'from_to';
14 use Email::Address;
15  
16 use Finance::Bank::SK::SLSP::Notification::Transaction;
17  
18 use base 'Class::Accessor::Fast';
19  
20 __PACKAGE__->mk_accessors(qw{
21 header_obj
22 attached_files
23 transactions
24 _tmpdir
25 });
26  
27 sub new {
28 my ($class, %params) = @_;
29  
30 $params{attached_files} ||= [];
31 $params{transactions} ||= [];
32 my $self = $class->SUPER::new({ %params });
33  
34 return $self;
35 }
36  
37 sub email_name {
38 my ($self) = @_;
39 my ($from) = Email::Address->parse($self->header_obj->header('From'));
40 return $from->name;
41 }
42  
43 sub email_from {
44 my ($self) = @_;
45 my ($from) = Email::Address->parse($self->header_obj->header('From'));
46 return $from->address;
47 }
48  
49 sub from_email {
50 my ($class, $email) = @_;
51  
52 my $parsed = Email::MIME->new($email);
53 foreach my $part ($parsed->parts) {
54  
55 my $filename = $part->filename;
56 next unless $filename;
57 next unless $filename =~ m/\.zip$/;
58 my $body = $part->body;
59  
60 my $tmpdir = tempdir( CLEANUP => 1 );
61 my $zip_file = file($tmpdir, $filename);
62 $zip_file->spew($body);
63 my $extract_dir = dir($tmpdir, 'extracted');
64 $extract_dir->mkpath;
65  
66 my $ae = Archive::Extract->new( archive => $zip_file ) || die 'fail '.$!;
67 $ae->extract( to => $extract_dir ) || die $ae->error;
68  
69 my @transactions;
70 my @files =
71 map { file($_) }
72 File::Find::Rule
73 ->file()
74 ->name( '*' )
75 ->in( $extract_dir );
76 foreach my $file (@files) {
77 next unless $file->basename =~ m/\.txt$/;
78 my $content = $file->slurp;
79 from_to($content,"windows-1250","utf-8");
80 $file->spew($content);
81  
82 # process transactions
83 if ($file->basename =~ m/^K\d+\.txt$/) {
84 push(@transactions, Finance::Bank::SK::SLSP::Notification::Transaction->from_txt($content));
85 }
86 }
87  
88 return $class->new(
89 header_obj => $parsed->header_obj,
90 attached_files => \@files,
91 transactions => \@transactions,
92 _tmpdir => $tmpdir,
93 )
94 }
95 }
96  
97 sub has_transactions {
98 my ($self) = @_;
99 return @{$self->transactions || []};
100 }
101  
102 1;
103  
104  
105 __END__