File Coverage

blib/lib/Atompub/MediaType.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Atompub::MediaType;
2              
3 2     2   56098 use strict;
  2         4  
  2         84  
4 2     2   11 use warnings;
  2         5  
  2         57  
5              
6 2     2   965 use Atompub;
  0            
  0            
7             use MIME::Types;
8             use Perl6::Export::Attrs;
9              
10             use base qw(Class::Accessor::Fast);
11              
12             my %ATOM_TYPE = (
13             entry => 'application/atom+xml;type=entry',
14             feed => 'application/atom+xml;type=feed',
15             service => 'application/atomsvc+xml',
16             categories => 'application/atomcat+xml',
17             );
18              
19             __PACKAGE__->mk_accessors(qw(type subtype parameters));
20              
21             use overload (
22             q{""} => \&as_string,
23             eq => \&is_a,
24             ne => \&is_not_a,
25             fallback => 1,
26             );
27              
28             sub new {
29             my($class, $arg) = @_;
30             my $media_type = $ATOM_TYPE{$arg} || $arg or return;
31             my($type, $subtype, $param) = split m{[/;]}, $media_type;
32             bless {
33             type => $type,
34             subtype => $subtype,
35             parameters => $param,
36             }, $class;
37             }
38              
39             sub media_type :Export { __PACKAGE__->new(@_) }
40              
41             sub subtype_major {
42             my($self) = @_;
43             $self->subtype =~ /\+(.+)/ ? $1 : $self->subtype;
44             }
45              
46             sub without_parameters {
47             my($self) = @_;
48             join '/', $self->type, $self->subtype;
49             }
50              
51             sub as_string {
52             my($self) = @_;
53             join ';', grep { defined $_ } $self->without_parameters, $self->parameters;
54             }
55              
56             sub extensions {
57             my($self) = @_;
58             my $mime = MIME::Types->new->type($self->without_parameters) or return;
59             my @exts = $mime->extensions;
60             wantarray ? @exts : $exts[0];
61             }
62              
63             sub extension { scalar shift->extensions }
64              
65             sub is_a {
66             my($self, $test) = @_;
67             $test = __PACKAGE__->new($test) unless UNIVERSAL::isa($test, __PACKAGE__);
68             return 1 if $test->type eq '*';
69             return 0 unless $test->type eq $self->type;
70             return 1 if $test->subtype eq '*';
71             if ($test->subtype eq $test->subtype_major) { # ex. application/xml
72             return 0 unless $test->subtype_major eq $self->subtype_major;
73             }
74             else { # ex. application/atom+xml
75             return 0 unless $test->subtype eq $self->subtype;
76             }
77             return 1 if ! $test->parameters || ! $self->parameters;
78             return $test->parameters eq $self->parameters;
79             }
80              
81             sub is_not_a {
82             my($self, @args) = @_;
83             !$self->is_a(@args);
84             }
85              
86             1;
87             __END__
88              
89             =head1 NAME
90              
91             Atompub::MediaType - a media type object for the Atom Publishing Protocol
92              
93             =head1 SYNOPSIS
94              
95             use Atompub::MediaType qw(media_type);
96              
97             my $type = media_type('image/png');
98              
99             "$type"; # 'image/png'
100             $type->type; # 'image'
101             $type->subtype; # 'png'
102              
103             $type->extension; # 'png'
104              
105             $type->is_a('image/*'); # true
106             $type->is_a('image/gif'); # false
107              
108             my $type = media_type('entry');
109              
110             "$type"; # 'application/atom+xml;type=entry'
111             $type->type; # 'application'
112             $type->subtype; # 'atom+xml'
113             $type->parameters; # 'type=entry'
114              
115             $type->subtype_major; # 'xml'
116              
117             $type->extension; # 'atom'
118              
119             $type->is_a('application/xml'); # true
120             $type->is_a('feed'); # false
121              
122             =head1 METHODS
123              
124             =head2 Atompub::MediaType->new([ $type ])
125              
126             Returns a media type object representing the time $type.
127              
128             $type is string representing media type like 'image/png'.
129             Some aliases are defined for Atom, 'entry', 'feed', 'service', and 'categories'.
130              
131             =head2 media_type([ $str ])
132              
133             Alias for Atompub::MediaType->new
134              
135             =head2 $type->type
136              
137             =head2 $type->subtype
138              
139             =head2 $type->parameters
140              
141             =head2 $type->subtype_major
142              
143             =head2 $type->extensions
144              
145             =head2 $type->extension
146              
147             =head2 $type->is_a
148              
149             =head2 $type->is_not_a
150              
151             =head2 $type->as_string
152              
153             =head2 $type->without_parameters
154              
155              
156             =head1 SEE ALSO
157              
158             L<Atompub>
159              
160              
161             =head1 AUTHOR
162              
163             Takeru INOUE, E<lt>takeru.inoue _ gmail.comE<gt>
164              
165              
166             =head1 LICENCE AND COPYRIGHT
167              
168             Copyright (c) 2007, Takeru INOUE C<< <takeru.inoue _ gmail.com> >>. All rights reserved.
169              
170             This module is free software; you can redistribute it and/or
171             modify it under the same terms as Perl itself. See L<perlartistic>.
172              
173              
174             =head1 DISCLAIMER OF WARRANTY
175              
176             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
177             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
178             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
179             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
180             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
181             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
182             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
183             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
184             NECESSARY SERVICING, REPAIR, OR CORRECTION.
185              
186             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
187             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
188             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
189             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
190             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
191             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
192             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
193             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
194             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
195             SUCH DAMAGES.
196              
197             =cut