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