File Coverage

blib/lib/Email/Assets.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Email::Assets;
2 1     1   17202 use Moose;
  0            
  0            
3              
4             =head1 NAME
5              
6             Email::Assets - Manage assets for Email
7              
8             =head1 VERSION
9              
10             Version 0.09
11              
12             =cut
13              
14             our $VERSION = '0.09';
15              
16             use MIME::Types;
17             use Email::Assets::File;
18              
19             has base => (
20             is => 'ro',
21             );
22              
23             has mime_types => (
24             is => 'ro',
25             lazy => 1,
26             default => sub { return MIME::Types->new(); },
27             );
28              
29             has _assets => (
30             traits => ['Hash'],
31             is => 'ro',
32             isa => 'HashRef[Email::Assets::File]',
33             default => sub { {} },
34             handles => {
35             _asset_exists => 'exists',
36             names => 'keys',
37             _set_asset => 'set',
38             _get_asset => 'get',
39             _all_assets => 'values',
40             }
41             );
42              
43             sub include {
44             my ($self, $filename, $options) = @_;
45             $options ||= { inline_only => 0 };
46              
47             if ($self->_asset_exists($filename)) {
48             return $self->_get_asset($filename);
49             }
50              
51             my $asset = Email::Assets::File->new({
52             mime_types => $self->mime_types,
53             base_paths => $self->base,
54             relative_filename => $filename,
55             inline_only => $options->{inline_only}
56             });
57             $self->_set_asset($filename => $asset);
58             return $asset;
59             }
60              
61             sub include_base64 {
62             my ($self, $base64_string, $filename, $options) = @_;
63             $options ||= { inline_only => 0 };
64             my $asset = Email::Assets::File->new({
65             mime_types => $self->mime_types,
66             base_paths => $self->base,
67             relative_filename => $filename,
68             base64_data => $base64_string,
69             inline_only => $options->{inline_only},
70             url_encoding => $options->{url_encoding},
71             });
72             $self->_set_asset($filename => $asset);
73             return $asset;
74             }
75              
76              
77             sub exports {
78             return shift->_all_assets;
79             }
80              
81             sub get {
82             my ($self, $filename) = @_;
83             return $self->_get_asset($filename);
84             }
85              
86             sub attachments {
87             return [ grep { $_->not_inline_only } shift()->_all_assets ];
88             }
89              
90             sub to_mime_parts {
91             my $self = shift;
92             return [ map { $_->as_mime_part } @{$self->attachments} ];
93             }
94              
95             =head1 DESCRIPION
96              
97             Simple to use library for file assets in email, allowing you to link using cid or inline, with export
98             to MIME::Lite message parts
99              
100             =head1 SYNOPSIS
101              
102             use Email::Assets;
103              
104             my $assets = Email::Assets->new( base => [ $uri_root, $dir_root ] );
105              
106             # Email::Assets will automatically detect the type based on the extension
107             my $asset = $assets->include("/static/foo.gif");
108              
109             # or
110              
111             my $asset = $assets->include_base64( $image_base64 );
112              
113             # This asset won't get attached twice, as Email::Assets will ignore repeats of a path
114             my $cid = $assets->include("/static/foo.gif")->cid;
115              
116             # Or you can iterate (in order)
117             for my $asset ($assets->exports) {
118             print $asset->cid, "\n";
119             my $mime_part = $asset->as_mime_part;
120             }
121              
122             ..or in a template :
123              
124             <img src="cid://[% assets.include('static/foo.gif').cid %]">
125             [% # or %]
126             <img src="data:[% assets.include('static/foo.gif', {inline_only => 1}).inline_data -%]">
127             <p>
128             <img src="cid:[% assets.include_base64(image_base64, filename, { url_encoded => 1 }).cid %]">
129             </p>
130             <p>
131              
132              
133             =head1 ATTRIBUTES
134              
135             =head2 mime_types
136              
137             MIME::Types object
138              
139             =head2 base
140              
141             arrayref of paths to find files in
142              
143             =head1 METHODS
144              
145             =head2 include
146              
147             Add an asset, takes filename (required), then optional hashref of options (inline_only currently only one supported), returns Email::Assets::File object
148              
149             =head2 include_base64
150              
151             Object method. Adds an asset already encoded in base64, returns that asset.
152              
153             Takes string holding base64 encoded file, filename, then optional hashref of options:
154              
155             =over 4
156              
157             =item inline_only - exclude asset from to_mime_parts, to avoid adding un-necessary size & attachments
158              
159             =item url_encoded - base64 encoding is "url safe" so use base64url decoder, and re-encode for MIME::Lite, etc as not suited for most email use.
160              
161             =back
162              
163             Returns Email::Assets::File object.
164              
165             =head2 exports
166              
167             Get all assets, returns list of Email::Assets::File objects
168              
169             =head2 get
170              
171             Get an asset by name, takes relative path, returns Email::Assets::File object
172              
173             =head2 attachments
174              
175             Get assets that aren't inline_only, returns arrayref of Email::Assets::File objects
176              
177             =head2 to_mime_parts
178              
179             Returns assets that aren't inline_only as arrayref of MIME::Lite objects
180              
181             =head1 SEE ALSO
182              
183             =over 4
184              
185             =item L<Email::Assets::File>
186              
187             =item L<MIME::Lite>
188              
189             =item L<File::Assets>
190              
191             =back
192              
193             =head1 AUTHOR
194              
195             Aaron J Trevena, C<< <teejay at cpan.org> >>
196              
197             =head1 BUGS
198              
199             Please report any bugs or feature requests to C<bug-email-assets at rt.cpan.org>, or through
200             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Email-Assets>. I will be notified, and then you'll
201             automatically be notified of progress on your bug as I make changes.
202              
203             =head1 SUPPORT
204              
205             You can find documentation for this module with the perldoc command.
206              
207             perldoc Email::Assets
208              
209              
210             You can also look for information at:
211              
212             =over 4
213              
214             =item * RT: CPAN's request tracker (report bugs here)
215              
216             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Email-Assets>
217              
218             =item * AnnoCPAN: Annotated CPAN documentation
219              
220             L<http://annocpan.org/dist/Email-Assets>
221              
222             =item * CPAN Ratings
223              
224             L<http://cpanratings.perl.org/d/Email-Assets>
225              
226             =item * Search CPAN
227              
228             L<http://search.cpan.org/dist/Email-Assets/>
229              
230             =back
231              
232              
233             =head1 ACKNOWLEDGEMENTS
234              
235              
236             =head1 LICENSE AND COPYRIGHT
237              
238             Copyright 2014 Aaron J Trevena.
239              
240             This program is free software; you can redistribute it and/or modify it
241             under the terms of the the Artistic License (2.0). You may obtain a
242             copy of the full license at:
243              
244             L<http://www.perlfoundation.org/artistic_license_2_0>
245              
246             Any use, modification, and distribution of the Standard or Modified
247             Versions is governed by this Artistic License. By using, modifying or
248             distributing the Package, you accept this license. Do not use, modify,
249             or distribute the Package, if you do not accept this license.
250              
251             If your Modified Version has been derived from a Modified Version made
252             by someone other than you, you are nevertheless required to ensure that
253             your Modified Version complies with the requirements of this license.
254              
255             This license does not grant you the right to use any trademark, service
256             mark, tradename, or logo of the Copyright Holder.
257              
258             This license includes the non-exclusive, worldwide, free-of-charge
259             patent license to make, have made, use, offer to sell, sell, import and
260             otherwise transfer the Package with respect to any patent claims
261             licensable by the Copyright Holder that are necessarily infringed by the
262             Package. If you institute patent litigation (including a cross-claim or
263             counterclaim) against any party alleging that the Package constitutes
264             direct or contributory patent infringement, then this Artistic License
265             to you shall terminate on the date that such litigation is filed.
266              
267             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
268             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
269             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
270             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
271             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
272             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
273             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
274             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275              
276              
277             =cut
278              
279             1; # End of Email::Assets