File Coverage

blib/lib/WWW/Sitemap/XML/Types.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1             #ABSTRACT: Type constraints used by WWW::Sitemap::XML and WWW::Sitemap::XML::URL
2 14     14   555828 use strict;
  14         28  
  14         434  
3 14     14   76 use warnings;
  14         25  
  14         835  
4             package WWW::Sitemap::XML::Types;
5             BEGIN {
6 14     14   764 $WWW::Sitemap::XML::Types::AUTHORITY = 'cpan:AJGB';
7             }
8             $WWW::Sitemap::XML::Types::VERSION = '2.02';
9 14         132 use MooseX::Types -declare => [qw(
10             SitemapURL
11             SitemapIndexSitemap
12              
13             Location
14             ChangeFreq
15             LowercaseStr
16             Priority
17              
18             VideoPlayer
19             VideoObject
20             ArrayRefOfVideoObjects
21             ImageObject
22             ArrayRefOfImageObjects
23             StrBool
24             Max100CharsStr
25             Max2048CharsStr
26 14     14   12504 )];
  14         662353  
27              
28 14     14   161581 use Moose::Util::TypeConstraints;
  14         35  
  14         69  
29 14     14   42936 use MooseX::Types::Moose qw( Object Str Num Bool ArrayRef HashRef );
  14         235233  
  14         124  
30 14     14   98308 use MooseX::Types::URI qw( Uri );
  14         6554238  
  14         88  
31 14     14   27216 use Scalar::Util qw( blessed );
  14         34  
  14         15857  
32              
33             role_type SitemapURL, { role => 'WWW::Sitemap::XML::URL::Interface' };
34              
35             role_type SitemapIndexSitemap, { role => 'WWW::SitemapIndex::XML::Sitemap::Interface' };
36              
37             # <loc>
38             subtype Location,
39             as Str,
40             where {
41             my $url = to_Uri($_);
42             $url->scheme && $url->authority
43             },
44             message { "$_ is not a valid URL" };
45              
46             coerce Location,
47             from Uri,
48             via { $_->as_string };
49              
50             subtype LowercaseStr,
51             as Str,
52             where {
53             $_ eq lc($_)
54             },
55             message { "$_ contains uppercase characters" };
56              
57             coerce LowercaseStr,
58             from Str,
59             via { lc($_) };
60              
61             subtype Max100CharsStr,
62             as Str,
63             where {
64             length($_) <= 100
65             },
66             message { "Maximum of 100 characters allowed" };
67              
68             subtype Max2048CharsStr,
69             as Str,
70             where {
71             length($_) <= 2048
72             },
73             message { "Maximum of 2048 characters allowed" };
74              
75             # <changefreq>
76             my %valid_changefreqs = map { $_ => 1 } qw( always hourly daily weekly monthly yearly never );
77             subtype ChangeFreq,
78             as LowercaseStr,
79             where {
80             exists $valid_changefreqs{$_}
81             };
82              
83             coerce ChangeFreq,
84             from Str,
85             via { lc($_) };
86              
87             # <priority>
88             subtype Priority,
89             as Num,
90             where {
91             $_ >= 0 && $_ <= 1
92             },
93             message { 'Valid priority ranges from 0.0 to 1.0'};
94              
95             # allow_embed
96             my %valid_str_bool = ( yes => 1, no => 1 );
97             subtype StrBool,
98             as LowercaseStr,
99             where {
100             exists $valid_str_bool{ lc($_) }
101             };
102              
103             coerce StrBool,
104             from Bool,
105             via { $_ ? 'yes' : 'no' };
106              
107             role_type VideoObject, { role => 'WWW::Sitemap::XML::Google::Video::Interface' };
108              
109             coerce VideoObject,
110             from HashRef,
111             via {
112             return WWW::Sitemap::XML::Google::Video->new( %{ $_ || {} } )
113             };
114              
115             subtype ArrayRefOfVideoObjects,
116             as ArrayRef[VideoObject];
117              
118             coerce ArrayRefOfVideoObjects,
119             from ArrayRef[HashRef|Object],
120             via {
121             [
122             map { blessed($_) ? $_ : WWW::Sitemap::XML::Google::Video->new( %{ $_ || {} } ) } @$_
123             ]
124             };
125              
126             role_type ImageObject, { role => 'WWW::Sitemap::XML::Google::Image::Interface' };
127              
128             coerce ImageObject,
129             from HashRef,
130             via {
131             return WWW::Sitemap::XML::Google::Image->new( %{ $_ || {} } )
132             };
133              
134             subtype ArrayRefOfImageObjects,
135             as ArrayRef[ImageObject],
136             where {
137             scalar(@$_) <= 1000
138             };
139              
140             coerce ArrayRefOfImageObjects,
141             from ArrayRef[HashRef|Object],
142             via {
143             [
144             map { blessed($_) ? $_ : WWW::Sitemap::XML::Google::Image->new( %{ $_ || {} } ) } @$_
145             ]
146             };
147              
148              
149             role_type VideoPlayer, { role => 'WWW::Sitemap::XML::Google::Video::Player::Interface' };
150              
151             coerce VideoPlayer,
152             from HashRef,
153             via {
154             WWW::Sitemap::XML::Google::Video::Player->new( %{ $_ || {} } )
155             };
156              
157             coerce VideoPlayer,
158             from Str,
159             via {
160             WWW::Sitemap::XML::Google::Video::Player->new( loc => $_ )
161             };
162              
163             # runtime to avoid circular references
164             require WWW::Sitemap::XML::Google::Image;
165             require WWW::Sitemap::XML::Google::Video;
166             require WWW::Sitemap::XML::Google::Video::Player;
167              
168              
169 14     14   92 no Moose::Util::TypeConstraints;
  14         27  
  14         92  
170              
171             1;
172              
173             __END__
174              
175             =pod
176              
177             =encoding UTF-8
178              
179             =head1 NAME
180              
181             WWW::Sitemap::XML::Types - Type constraints used by WWW::Sitemap::XML and WWW::Sitemap::XML::URL
182              
183             =head1 VERSION
184              
185             version 2.02
186              
187             =head1 DESCRIPTION
188              
189             Type constraints used by L<WWW::Sitemap::XML> and L<WWW::Sitemap::XML::URL>.
190              
191             =head1 TYPES
192              
193             =head2 Location
194              
195             has 'loc' => (
196             is => 'rw',
197             isa => Location,
198             );
199              
200             URL location, coerced from L<Uri|MooseX::Types::URI> via C<{ $_-E<gt>as_string }>.
201              
202             =head2 ChangeFreq
203              
204             has 'changefreq' => (
205             is => 'rw',
206             isa => ChangeFreq,
207             );
208              
209             Valid values are:
210              
211             =over
212              
213             =item * always
214              
215             =item * hourly
216              
217             =item * daily
218              
219             =item * weekly
220              
221             =item * monthly
222              
223             =item * yearly
224              
225             =item * never
226              
227             =back
228              
229             =head2 Priority
230              
231             has 'priority' => (
232             is => 'rw',
233             isa => Priority,
234             );
235              
236             Subtype of C<Num> with values in range from C<0.0> to C<1.0>.
237              
238             =head2 SitemapURL
239              
240             has 'url' => (
241             is => 'rw',
242             isa => SitemapURL,
243             );
244              
245             Role type, argument needs to implement L<WWW::Sitemap::XML::URL::Interface>.
246              
247             =head2 SitemapIndexSitemap
248              
249             has 'sitemap' => (
250             is => 'rw',
251             isa => SitemapIndexSitemap,
252             );
253              
254             Role type, argument needs to implement L<WWW::SitemapIndex::XML::Sitemap::Interface>.
255              
256             =head2 LowercaseStr
257              
258             has 'lowercase' => (
259             is => 'rw',
260             coerce => 1,
261             isa => LowercaseStr,
262             );
263              
264             Subtype of C<Str>, with only lowercase characters.
265              
266             Coerces from C<Str> using C<lc>.
267              
268             =head2 Max100CharsStr
269              
270             has 'short_str' => (
271             is => 'rw',
272             isa => Max100CharsStr,
273             );
274              
275             Subtype of C<Str>, up to 100 characters.
276              
277             =head2 Max2048CharsStr
278              
279             has 'longer_str' => (
280             is => 'rw',
281             isa => Max2048CharsStr,
282             );
283              
284             Subtype of C<Str>, up to 2048 characters.
285              
286             =head2 StrBool
287              
288             has 'yes_no' => (
289             is => 'rw',
290             coerce => 1,
291             isa => StrBool,
292             );
293              
294             Subtype of C<LowercaseStr>, with valid values I<yes> and I<no>.
295              
296             Coerces from C<Bool>.
297              
298             =head2 ImageObject
299              
300             has 'image' => (
301             is => 'rw',
302             coerce => 1,
303             isa => ImageObject,
304             );
305              
306             Role type, argument needs to implement L<WWW::Sitemap::XML::Google::Image::Interface>.
307              
308             Coerces from C<HashRef> by creating L<WWW::Sitemap::XML::Google::Image> object.
309              
310             =head2 ArrayRefOfImageObjects
311              
312             has 'images' => (
313             is => 'rw',
314             coerce => 1,
315             isa => ArrayRefOfImageObjects,
316             );
317              
318             Subtype of C<ArrayRef>, were values are L<"ImageObject"> elements.
319              
320             Coerces from C<ArrayRef[HashRef]> by creating an array of L<WWW::Sitemap::XML::Google::Image> objects.
321              
322             =head2 VideoObject
323              
324             has 'video' => (
325             is => 'rw',
326             coerce => 1,
327             isa => VideoObject,
328             );
329              
330             Role type, argument needs to implement L<WWW::Sitemap::XML::Google::Video::Interface>.
331              
332             Coerces from C<HashRef> by creating L<WWW::Sitemap::XML::Google::Video> object.
333              
334             =head2 ArrayRefOfVideoObjects
335              
336             has 'videos' => (
337             is => 'rw',
338             coerce => 1,
339             isa => ArrayRefOfVideoObjects,
340             );
341              
342             Subtype of C<ArrayRef>, were values are L<"VideoObject"> elements.
343              
344             Coerces from C<ArrayRef[HashRef]> by creating an array of L<WWW::Sitemap::XML::Google::Video> objects.
345              
346             =head2 VideoPlayer
347              
348             has 'video_player' => (
349             is => 'rw',
350             coerce => 1,
351             isa => VideoPlayer,
352             );
353              
354             Role type, argument needs to implement L<WWW::Sitemap::XML::Google::Video::Player::Interface>.
355              
356             Coerces from C<HashRef> by creating L<WWW::Sitemap::XML::Google::Video::Player> object.
357              
358             Coerces from C<Str> by creating L<WWW::Sitemap::XML::Google::Video::Player>
359             object, where the string is used as L<WWW::Sitemap::XML::Google::Video::Player/"loc">.
360              
361             =head1 AUTHOR
362              
363             Alex J. G. BurzyÅ„ski <ajgb@cpan.org>
364              
365             =head1 COPYRIGHT AND LICENSE
366              
367             This software is copyright (c) 2014 by Alex J. G. BurzyÅ„ski <ajgb@cpan.org>.
368              
369             This is free software; you can redistribute it and/or modify it under
370             the same terms as the Perl 5 programming language system itself.
371              
372             =cut