File Coverage

blib/lib/WWW/Sitemap/XML/URL.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             #ABSTRACT: XML Sitemap url entry
2 7     7   35019 use strict;
  7         29  
  7         195  
3 7     7   39 use warnings;
  7         11  
  7         1407  
4             package WWW::Sitemap::XML::URL;
5             BEGIN {
6 7     7   183 $WWW::Sitemap::XML::URL::AUTHORITY = 'cpan:AJGB';
7             }
8             $WWW::Sitemap::XML::URL::VERSION = '2.02';
9 7     7   830 use Moose;
  7         477165  
  7         42  
10 7     7   49679 use WWW::Sitemap::XML::Types qw( Location ChangeFreq Priority ArrayRefOfImageObjects ArrayRefOfVideoObjects );
  0            
  0            
11             use MooseX::Types::DateTime::W3C qw( DateTimeW3C );
12             use XML::LibXML;
13             use WWW::Sitemap::XML::Google::Image;
14             use WWW::Sitemap::XML::Google::Video;
15              
16              
17              
18             has 'loc' => (
19             is => 'rw',
20             isa => Location,
21             required => 1,
22             coerce => 1,
23             predicate => 'has_loc',
24             );
25              
26              
27             has 'lastmod' => (
28             is => 'rw',
29             isa => DateTimeW3C,
30             required => 0,
31             coerce => 1,
32             predicate => 'has_lastmod',
33             );
34              
35              
36             has 'changefreq' => (
37             is => 'rw',
38             isa => ChangeFreq,
39             required => 0,
40             coerce => 1,
41             predicate => 'has_changefreq',
42             );
43              
44              
45             has 'priority' => (
46             is => 'rw',
47             isa => Priority,
48             required => 0,
49             predicate => 'has_priority',
50             );
51              
52              
53             has 'images' => (
54             is => 'rw',
55             isa => ArrayRefOfImageObjects,
56             required => 0,
57             coerce => 1,
58             predicate => 'has_images',
59             );
60              
61              
62             has 'videos' => (
63             is => 'rw',
64             isa => ArrayRefOfVideoObjects,
65             required => 0,
66             coerce => 1,
67             predicate => 'has_videos',
68             );
69              
70              
71             has 'mobile' => (
72             is => 'rw',
73             isa => 'Bool',
74             required => 0,
75             coerce => 0,
76             predicate => 'has_mobile',
77             );
78              
79              
80             sub as_xml {
81             my $self = shift;
82              
83             my $url = XML::LibXML::Element->new('url');
84              
85             do {
86             my $name = $_;
87             my $e = XML::LibXML::Element->new($name);
88              
89             $e->appendText( $self->$name );
90              
91             $url->appendChild( $e );
92              
93             } for 'loc',grep {
94             eval('$self->has_'.$_) || defined $self->$_()
95             } qw( lastmod changefreq priority );
96              
97             if ( $self->has_images ) {
98             for my $image ( @{ $self->images || [] } ) {
99             $url->appendChild( $image->as_xml );
100             }
101             }
102              
103             if ( $self->has_videos ) {
104             for my $video ( @{ $self->videos || [] } ) {
105             $url->appendChild( $video->as_xml );
106             }
107             }
108              
109             if ( $self->has_mobile && $self->mobile ) {
110             my $e = XML::LibXML::Element->new('mobile:mobile');
111             $url->appendChild( $e );
112             }
113              
114             return $url;
115             }
116              
117             around BUILDARGS => sub {
118             my $next = shift;
119             my $class = shift;
120              
121             if ( @_ == 1 && ! ref $_[0] ) {
122             return $class->$next(loc => $_[0]);
123             }
124              
125             return $class->$next( @_ );
126             };
127              
128             with 'WWW::Sitemap::XML::URL::Interface';
129              
130              
131             __PACKAGE__->meta->make_immutable;
132              
133             1;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             WWW::Sitemap::XML::URL - XML Sitemap url entry
144              
145             =head1 VERSION
146              
147             version 2.02
148              
149             =head1 SYNOPSIS
150              
151             my $url = WWW::Sitemap::XML::URL->new(
152             loc => 'http://mywebsite.com/',
153             lastmod => '2010-11-26',
154             changefreq => 'always',
155             priority => 1.0,
156             );
157              
158             XML output:
159              
160             <?xml version="1.0" encoding="UTF-8"?>
161             <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
162             <url>
163             <loc>http://mywebsite.com/</loc>
164             <lastmod>2010-11-26</lastmod>
165             <changefreq>always</changefreq>
166             <priority>1.0</priority>
167             </url>
168             </urlset>
169              
170             Google sitemap video, image and mobile extensions:
171              
172             my $url2 = WWW::Sitemap::XML::URL->new(
173             loc => 'http://mywebsite.com/',
174             lastmod => '2010-11-26',
175             changefreq => 'always',
176             priority => 1.0,
177             mobile => 1,
178             images => [
179             {
180             loc => 'http://mywebsite.com/image1.jpg',
181             caption => 'Caption 1',
182             title => 'Title 1',
183             license => 'http://www.mozilla.org/MPL/2.0/',
184             geo_location => 'Town, Region',
185             },
186             {
187             loc => 'http://mywebsite.com/image2.jpg',
188             caption => 'Caption 2',
189             title => 'Title 2',
190             license => 'http://www.mozilla.org/MPL/2.0/',
191             geo_location => 'Town, Region',
192             }
193             ],
194             videos => [
195             content_loc => 'http://mywebsite.com/video1.flv',
196             player => {
197             loc => 'http://mywebsite.com/video_player.swf?video=1',
198             allow_embed => "yes",
199             autoplay => "ap=1",
200             }
201             thumbnail_loc => 'http://mywebsite.com/thumbs/1.jpg',
202             title => 'Video Title 1',
203             description => 'Video Description 1',
204             ]
205              
206             );
207              
208             XML output:
209              
210             <?xml version="1.0" encoding="UTF-8"?>
211             <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
212             xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
213             xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
214             xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
215             <url>
216             <loc>http://mywebsite.com/</loc>
217             <lastmod>2010-11-26</lastmod>
218             <changefreq>always</changefreq>
219             <priority>1.0</priority>
220             <mobile:mobile/>
221             <image:image>
222             <image:loc>http://mywebsite.com/image1.jpg</image:loc>
223             <image:caption>Caption 1</image:caption>
224             <image:title>Title 1</image:title>
225             <image:license>http://www.mozilla.org/MPL/2.0/</image:license>
226             <image:geo_location>Town, Region</image:geo_location>
227             </image:image>
228             <image:image>
229             <image:loc>http://mywebsite.com/image2.jpg</image:loc>
230             <image:caption>Caption 2</image:caption>
231             <image:title>Title 2</image:title>
232             <image:license>http://www.mozilla.org/MPL/2.0/</image:license>
233             <image:geo_location>Town, Region</image:geo_location>
234             </image:image>
235             <video:video>
236             <video:content_loc>http://mywebsite.com/video1.flv</video:content_loc>
237             <video:title>Video Title 1</video:title>
238             <video:description>Video Description 1</video:description>
239             <video:thumbnail_loc>http://mywebsite.com/thumbs/1.jpg</video:thumbnail_loc>
240             <video:player_loc allow_embed="yes" autoplay="ap=1">http://mywebsite.com/video_player.swf?video=1</video:player_loc>
241             </video:video>
242              
243             </url>
244             </urlset>
245              
246             =head1 DESCRIPTION
247              
248             WWW::Sitemap::XML::URL represents single url entry in sitemap file.
249              
250             Class implements L<WWW::Sitemap::XML::URL::Interface>.
251              
252             =head1 ATTRIBUTES
253              
254             =head2 loc
255              
256             URL of the page.
257              
258             isa: L<WWW::Sitemap::XML::Types/"Location">
259              
260             Required.
261              
262             =head2 lastmod
263              
264             The date of last modification of the page.
265              
266             isa: L<MooseX::Types::DateTime::W3C/"DateTimeW3C">
267              
268             Optional.
269              
270             =head2 changefreq
271              
272             How frequently the page is likely to change.
273              
274             isa: L<WWW::Sitemap::XML::Types/"ChangeFreq">
275              
276             Optional.
277              
278             =head2 priority
279              
280             The priority of this URL relative to other URLs on your site.
281              
282             isa: L<WWW::Sitemap::XML::Types/"Priority">
283              
284             Optional.
285              
286             =head2 images
287              
288             Array reference of images on page.
289              
290             Note: This is a Google sitemap extension.
291              
292             isa: L<WWW::Sitemap::XML::Types/"ArrayRefOfImageObjects">
293              
294             Optional.
295              
296             =head2 videos
297              
298             Array reference of videos on page.
299              
300             Note: This is a Google sitemap extension.
301              
302             isa: L<WWW::Sitemap::XML::Types/"ArrayRefOfVideoObjects">
303              
304             Optional.
305              
306             =head2 mobile
307              
308             Flag indicating that page serves feature phone content.
309              
310             A mobile sitemap must contain only URLs that serve feature phone web content.
311             All other URLs are ignored by the Google crawling mechanisms so, if you have
312             non-featurephone content, create a separate sitemap for those URLs.
313              
314             Note: This is a Google sitemap extension.
315              
316             isa: C<Bool>
317              
318             Optional.
319              
320             =head1 METHODS
321              
322             =head2 as_xml
323              
324             Returns L<XML::LibXML::Element> object representing the C<E<lt>urlE<gt>> entry in the sitemap.
325              
326             =head1 SEE ALSO
327              
328             L<http://www.sitemaps.org/protocol.php>
329              
330             L<WWW::Sitemap::XML::Google::Image>
331              
332             L<WWW::Sitemap::XML::Google::Video>
333              
334             L<https://support.google.com/webmasters/answer/183668>
335              
336             =head1 AUTHOR
337              
338             Alex J. G. BurzyÅ„ski <ajgb@cpan.org>
339              
340             =head1 COPYRIGHT AND LICENSE
341              
342             This software is copyright (c) 2014 by Alex J. G. BurzyÅ„ski <ajgb@cpan.org>.
343              
344             This is free software; you can redistribute it and/or modify it under
345             the same terms as the Perl 5 programming language system itself.
346              
347             =cut