File Coverage

blib/lib/Courriel/Header/ContentType.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 38 39 97.4


line stmt bran cond sub pod time code
1             package Courriel::Header::ContentType;
2              
3 9     9   231644 use strict;
  9         18  
  9         382  
4 9     9   58 use warnings;
  9         15  
  9         391  
5 9     9   1709 use namespace::autoclean;
  9         59436  
  9         80  
6              
7             our $VERSION = '0.44';
8              
9 9     9   2467 use Courriel::Types qw( Maybe NonEmptyStr );
  9         23  
  9         85  
10              
11 9     9   66716 use Moose;
  9         17  
  9         80  
12 9     9   58978 use MooseX::StrictConstructor;
  9         66740  
  9         70  
13              
14             extends 'Courriel::Header';
15              
16             with 'Courriel::Role::HeaderWithAttributes' => {
17             main_value_key => 'mime_type',
18             main_value_method => '_original_mime_type',
19             };
20              
21             has '+value' => (
22             required => 0,
23             lazy => 1,
24             builder => 'as_header_value',
25             );
26              
27             has '+name' => (
28             default => 'Content-Type',
29             );
30              
31             has mime_type => (
32             is => 'ro',
33             isa => NonEmptyStr,
34             required => 1,
35             );
36              
37             has _original_mime_type => (
38             is => 'ro',
39             isa => NonEmptyStr,
40             required => 1,
41             );
42              
43             has charset => (
44             is => 'ro',
45             isa => Maybe [NonEmptyStr],
46             init_arg => undef,
47             lazy => 1,
48             builder => '_build_charset',
49             );
50              
51             around BUILDARGS => sub {
52             my $orig = shift;
53             my $class = shift;
54              
55             my $p = $class->$orig(@_);
56              
57             $p->{name} = 'Content-Type' unless exists $p->{name};
58              
59             return unless defined $p->{mime_type};
60              
61             $p->{_original_mime_type} = $p->{mime_type};
62             $p->{mime_type} = lc $p->{mime_type};
63              
64             return $p;
65             };
66              
67             sub _build_charset {
68 13     13   18 my $self = shift;
69              
70 13 100       452 return unless exists $self->_attributes()->{charset};
71              
72 6         186 return $self->_attributes()->{charset}->value();
73             }
74              
75             sub is_binary {
76 13     13 1 23 my $self = shift;
77              
78 13 100 66     417 return defined $self->charset() && $self->charset() ne 'binary' ? 0 : 1;
79             }
80              
81             __PACKAGE__->meta()->make_immutable();
82              
83             1;
84              
85             # ABSTRACT: The content type for an email part
86              
87             __END__
88              
89             =pod
90              
91             =encoding UTF-8
92              
93             =head1 NAME
94              
95             Courriel::Header::ContentType - The content type for an email part
96              
97             =head1 VERSION
98              
99             version 0.44
100              
101             =head1 SYNOPSIS
102              
103             my $ct = $part->content_type();
104             print $ct->mime_type();
105             print $ct->charset();
106              
107             my %attr = $ct->attributes();
108             while ( my ( $k, $v ) = each %attr ) {
109             print "$k => $v\n";
110             }
111              
112             =head1 DESCRIPTION
113              
114             This class represents the contents of a "Content-Type" header attached to an
115             email part. Such headers always include a mime type, and may also include
116             additional information such as a charset or other attributes.
117              
118             Here are some typical headers:
119              
120             Content-Type: text/plain; charset=utf-8
121              
122             Content-Type: multipart/alternative; boundary=abcdefghijk
123              
124             Content-Type: image/jpeg; name="Filename.jpg"
125              
126             =head1 API
127              
128             This class supports the following methods:
129              
130             =head2 Courriel::Header::ContentType->new_from_value( ... )
131              
132             This takes two parameters, C<name> and C<value>. The C<name> is optional, and
133             defaults to "Content-Type".
134              
135             The C<value> is parsed and split up into the mime type and attributes.
136              
137             =head2 Courriel::Header::ContentType->new( ... )
138              
139             This method creates a new object. It accepts the following parameters:
140              
141             =over 4
142              
143             =item * name
144              
145             This defaults to 'Content-Type'.
146              
147             =item * value
148              
149             This is the full header value.
150              
151             =item * mime_type
152              
153             A string like "text/plain" or "multipart/alternative". This is required.
154              
155             =item * attributes
156              
157             A hash reference of attributes from the header, such as a boundary, charset,
158             etc. The keys are attribute names and the values can either be strings or
159             L<Courriel::HeaderAttribute> objects. Values which are strings will be
160             inflated into objects by the constructor.
161              
162             This is optional, and can be an empty hash reference or omitted entirely.
163              
164             =back
165              
166             =head2 $ct->name()
167              
168             The header name, usually "Content-Type".
169              
170             =head2 $ct->value()
171              
172             The raw header value.
173              
174             =head2 $ct->mime_type()
175              
176             Returns the mime type value passed to the constructor. However, this value
177             will be in all lower-case, regardless of the original casing passed to the
178             constructor.
179              
180             =head2 $ct->charset()
181              
182             Returns the charset for the content type, which will be the value found in the
183             C<attributes>, if one exists.
184              
185             =head2 $ct->attributes()
186              
187             Returns a hash (not a reference) of the attributes passed to the constructor.
188              
189             Attributes are L<Courriel::HeaderAttribute> objects.
190              
191             The keys of the hash are all lower case, though the original casing is
192             preserved in the C<name()> returned by the L<Courriel::HeaderAttribute>
193             object.
194              
195             =head2 $ct->is_binary()
196              
197             Returns true unless the attachment looks like text data. Currently, this means
198             that is has a charset defined and the charset is not "binary".
199              
200             =head2 $ct->attribute($key)
201              
202             Given a key, returns the named L<Courriel::HeaderAttribute> object. Obviously,
203             this value can be C<undef> if the attribute doesn't exist. Name lookup is
204             case-insensitive.
205              
206             =head2 $ct->attribute_value($key)
207              
208             Given a key, returns the named attribute's value as a string. Obviously, this
209             value can be C<undef> if the attribute doesn't exist. Name lookup is
210             case-insensitive.
211              
212             The attribute is a L<Courriel::HeaderAttribute> object.
213              
214             =head2 $ct->as_header_value()
215              
216             Returns the object as a string suitable for a header value (but not
217             folded). Note that this uses the original casing of the mime type as passed to
218             the constructor.
219              
220             =head1 EXTENDS
221              
222             This class extends L<Courriel::Header>.
223              
224             =head1 ROLES
225              
226             This class does the C<Courriel::Role::HeaderWithAttributes> role.
227              
228             =head1 SUPPORT
229              
230             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Courriel>
231             (or L<bug-courriel@rt.cpan.org|mailto:bug-courriel@rt.cpan.org>).
232              
233             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
234              
235             =head1 AUTHOR
236              
237             Dave Rolsky <autarch@urth.org>
238              
239             =head1 COPYRIGHT AND LICENSE
240              
241             This software is Copyright (c) 2016 by Dave Rolsky.
242              
243             This is free software, licensed under:
244              
245             The Artistic License 2.0 (GPL Compatible)
246              
247             =cut