File Coverage

blib/lib/Email/Folder.pm
Criterion Covered Total %
statement 38 39 97.4
branch 3 6 50.0
condition 2 5 40.0
subroutine 10 10 100.0
pod 5 5 100.0
total 58 65 89.2


line stmt bran cond sub pod time code
1 3     3   2901 use strict;
  3         5  
  3         130  
2 3     3   18 use warnings;
  3         4  
  3         183  
3             package Email::Folder;
4             # ABSTRACT: read all the messages from a folder as Email::Simple objects
5             $Email::Folder::VERSION = '0.859';
6 3     3   18 use Carp;
  3         6  
  3         265  
7 3     3   2702 use Email::Simple;
  3         18949  
  3         116  
8 3     3   2961 use Email::FolderType 0.6 qw/folder_type/;
  3         45201  
  3         1141  
9              
10             #pod =head1 SYNOPSIS
11             #pod
12             #pod use Email::Folder;
13             #pod
14             #pod my $folder = Email::Folder->new("some_file");
15             #pod
16             #pod print join "\n", map { $_->header("Subject") } $folder->messages;
17             #pod
18             #pod =head1 METHODS
19             #pod
20             #pod =head2 new($folder, %options)
21             #pod
22             #pod Takes the name of a folder, and a hash of options
23             #pod
24             #pod If a 'reader' option is passed in then that is
25             #pod used as the class to read in messages with.
26             #pod
27             #pod =cut
28              
29             sub new {
30 11     11 1 15424 my $class = shift;
31 11   33     48 my $folder = shift || carp "Must provide a folder name\n";
32 11         38 my %self = @_;
33              
34 11         20 my $reader;
35              
36 11 50       36 if ($self{reader}) {
37 0         0 $reader = $self{reader};
38             } else {
39 11         45 $reader = "Email::Folder::".folder_type($folder);
40             }
41 11 50       50982 eval "require $reader" or die $@;
42              
43 11         130 $self{_folder} = $reader->new($folder, @_);
44              
45 11         90 return bless \%self, $class;
46             }
47              
48             #pod =head2 messages
49             #pod
50             #pod Returns a list containing all of the messages in the folder. Can only
51             #pod be called once as it drains the iterator.
52             #pod
53             #pod =cut
54              
55             sub messages {
56 9     9 1 23 my $self = shift;
57              
58 9         59 my @messages = $self->{_folder}->messages;
59 9         15 my @ret;
60 9         32 while (my $body = shift @messages) {
61 52         26371 push @ret, $self->bless_message( $body );
62             }
63 9         4365 return @ret;
64             }
65              
66              
67             #pod =head2 next_message
68             #pod
69             #pod acts as an iterator. reads the next message from a folder. returns
70             #pod false at the end of the folder
71             #pod
72             #pod =cut
73              
74             sub next_message {
75 2     2 1 9 my $self = shift;
76              
77 2 50       13 my $body = $self->{_folder}->next_message or return;
78 2         12 $self->bless_message( $body );
79             }
80              
81              
82             #pod =head2 bless_message($message)
83             #pod
84             #pod Takes a raw RFC822 message and blesses it into a class.
85             #pod
86             #pod By default this is an Email::Simple object but can easily be overridden
87             #pod in a subclass.
88             #pod
89             #pod For example, this simple subclass just returns the raw rfc822 messages,
90             #pod and exposes the speed of the parser.
91             #pod
92             #pod package Email::RawFolder;
93             #pod use base 'Email::Folder';
94             #pod sub bless_message { $_[1] };
95             #pod 1;
96             #pod
97             #pod =cut
98              
99             sub bless_message {
100 54     54 1 73 my $self = shift;
101 54   50     117 my $message = shift || die "You must pass a message\n";
102              
103 54         254 return Email::Simple->new($message);
104             }
105              
106              
107             #pod =head2 reader
108             #pod
109             #pod read-only accessor to the underlying Email::Reader subclass instance
110             #pod
111             #pod =cut
112              
113             sub reader {
114 1     1 1 1438 my $self = shift;
115 1         8 return $self->{_folder};
116             }
117              
118             1;
119              
120             #pod =head1 SEE ALSO
121             #pod
122             #pod L, L, L
123             #pod
124             #pod =cut
125              
126             __END__