File Coverage

blib/lib/Search/Sitemap/URL.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Search::Sitemap::URL;
2             $Search::Sitemap::URL::VERSION = '2.13_01';
3 1     1   50574 use 5.008003;
  1         3  
  1         29  
4 1     1   4 use strict;
  1         1  
  1         21  
5 1     1   3 use warnings;
  1         1  
  1         28  
6             our $AUTHORITY = 'cpan:JASONK';
7 1     1   183 use Moose;
  0            
  0            
8             use MooseX::ClassAttribute;
9             use MooseX::Types::Moose qw( Bool );
10             use MooseX::Types::URI qw( Uri );
11             use Search::Sitemap::Types qw(
12             SitemapChangeFreq SitemapLastMod SitemapPriority
13             );
14             use XML::Twig qw();
15             use POSIX qw( strftime );
16             use HTML::Entities qw( encode_entities );
17             use Scalar::Util qw( blessed );
18             use namespace::clean -except => 'meta';
19              
20             class_has 'encode_entities' => (
21             is => 'rw',
22             isa => Bool,
23             );
24              
25             class_has 'lenient' => (
26             is => 'rw',
27             isa => Bool,
28             );
29              
30             has 'loc' => (
31             is => 'rw',
32             isa => Uri,
33             coerce => 1,
34             predicate => 'has_loc',
35             clearer => 'clear_loc',
36             );
37             has 'changefreq' => (
38             is => 'rw',
39             isa => SitemapChangeFreq,
40             coerce => 1,
41             predicate => 'has_changefreq',
42             clearer => 'clear_changefreq',
43             );
44             has 'lastmod' => (
45             is => 'rw',
46             isa => SitemapLastMod,
47             coerce => 1,
48             predicate => 'has_lastmod',
49             clearer => 'clear_lastmod',
50             );
51             has 'priority' => (
52             is => 'rw',
53             isa => SitemapPriority,
54             coerce => 1,
55             predicate => 'has_priority',
56             clearer => 'clear_priority',
57             );
58             has 'mobile' => (
59             is => 'rw',
60             isa => Bool,
61             predicate => 'has_mobile',
62             clearer => 'clear_mobile',
63             );
64              
65             sub _loc_as_elt {
66             my $self = shift;
67             return unless $self->has_loc;
68             my $loc = XML::Twig::Elt->new(
69             '#PCDATA' => encode_entities( $self->loc->as_string )
70             );
71             $loc->set_asis( 1 );
72             return $loc;
73             }
74              
75             sub _mobile_as_elt {
76             my $self = shift;
77             return unless $self->mobile;
78             return XML::Twig::Elt->new( 'mobile', namespace => 'mobile' );
79             }
80              
81             sub as_elt {
82             my ( $self, $type, @fields ) = @_;
83              
84             $type ||= 'url';
85             unless ( @fields ) { @fields = qw( loc changefreq lastmod priority ) }
86              
87             my @elements = ();
88             for my $f ( @fields ) {
89             my $exists = $self->can( "has_$f" );
90             next if $exists and not $self->$exists;
91              
92             my $method = '_'.$f.'_as_elt';
93             my $val;
94             if ( $self->can( $method ) ) {
95             $val = $self->$method();
96             } else {
97             $val = XML::Twig::Elt->new( '#PCDATA' => $self->$f() );
98             }
99             next unless $val;
100             next unless blessed $val;
101             next unless $val->isa( 'XML::Twig::Elt' );
102             push( @elements, $val->wrap_in( $f ) );
103             }
104             return XML::Twig::Elt->new( $type, {}, @elements );
105             }
106              
107             __PACKAGE__->meta->make_immutable;
108             1;
109             __END__
110              
111             =head1 NAME
112              
113             Search::Sitemap::URL - URL Helper class for Search::Sitemap
114              
115             =head1 SYNOPSIS
116              
117             use Search::Sitemap;
118              
119             =head1 DESCRIPTION
120              
121             This is a helper class that supports L<Search::Sitemap> and
122             L<Search::Sitemap::Index>.
123              
124             =head1 METHODS
125              
126             =head2 new()
127              
128             =head2 loc()
129              
130             Change the URL associated with this object. For a L<Search::Sitemap> this
131             specifies the URL to add to the sitemap, for a L<Search::Sitemap::Index>, this
132             is the URL to the sitemap.
133              
134             =head2 changefreq()
135              
136             Set the change frequency of the object. This field is not used in sitemap
137             indexes, only in sitemaps.
138              
139             =head2 lastmod()
140              
141             Set or retrieve the last modified time. This will return a L<DateTime>
142             object. When setting it, you can provide any of these types of values:
143              
144             =over 4
145              
146             =item a complete ISO8601 time string
147              
148             A complete time string will be accepted in exactly this format:
149              
150             YYYY-MM-DDTHH:MM:SS+TZ:TZ
151              
152             YYYY - 4-digit year
153             MM - 2-digit month (zero padded)
154             DD - 2-digit year (zero padded)
155             T - literal character 'T'
156             HH - 2-digit hour (24-hour, zero padded)
157             SS - 2-digit second (zero padded)
158             +TZ:TZ - Timezone offset (hours and minutes from GMT, 2-digit, zero padded)
159              
160             =item epoch time
161              
162             Seconds since the epoch, such as would be returned from time(). If you provide
163             an epoch time, then an appropriate ISO8601 time will be constructed with
164             gmtime() (which means the timezone offset will be +00:00).
165              
166             =item an ISO8601 date (YYYY-MM-DD)
167              
168             A simple date in YYYY-MM-DD format.
169              
170             =item a L<DateTime> object.
171              
172             If a L<DateTime> object is provided, then an appropriate timestamp will be
173             constructed from it.
174              
175             =item a L<HTTP::Response> object.
176              
177             If given an L<HTTP::Response> object, the last modified time will be
178             calculated from whatever time information is available in the response
179             headers. Currently this means either the Last-Modified header, or the
180             current time - the current_age() calculated by the response object.
181             This is useful for building web crawlers.
182              
183             =item a L<File::stat> object.
184              
185             If given a L<File::stat> object, the last modified time will be set from
186             L<File::stat/mtime>.
187              
188             =item a L<Path::Class::File> object.
189              
190             If given a L<Path::Class::File> object, the last modified time will be set
191             to the mtime of the referenced file.
192              
193             =back
194              
195             Note that in order to conserve memory, any of these items that you provide
196             will be converted to a complete ISO8601 time string when they are stored.
197             This means that if you pass an object to lastmod(), you can't get it back
198             out. If anyone actually has a need to get the objects back out, then I
199             might make a configuration option to store the objects internally.
200              
201             If you have suggestions for other types of date/time objects or formats
202             that would be useful, let me know and I'll consider them.
203              
204             =head2 priority()
205              
206             Get or set the priority. This field is not used in sitemap indexes, only in
207             sitemaps.
208              
209             =head2 mobile()
210              
211             Set to a true value if this URL refers to a page that is intended for mobile
212             devices. This will affect how some search engines index the URL.
213              
214             For more information on mobile sitemaps, see
215             L<http://www.google.com/support/webmasters/bin/answer.py?answer=34627>
216              
217             =head2 as_elt
218              
219             Returns this URL and it's associated data as an L<XML::Twig::Elt> object.
220             This is primarily an internal use method, you probably don't need to mess
221             with it.
222              
223              
224             =head1 SEE ALSO
225              
226             L<Search::Sitemap>
227              
228             L<Search::Sitemap::Index>
229              
230             L<Search::Sitemap::Ping>
231              
232             L<http://www.sitemaps.org/>
233              
234             L<http://www.google.com/support/webmasters/bin/answer.py?answer=34648>
235              
236             =head1 AUTHOR
237              
238             Jason Kohles, E<lt>email@jasonkohles.comE<gt>
239              
240             =head1 COPYRIGHT AND LICENSE
241              
242             Copyright (C) 2005-2009 by Jason Kohles
243              
244             This library is free software; you can redistribute it and/or modify
245             it under the same terms as Perl itself.
246              
247             =cut
248