File Coverage

blib/lib/Search/Sitemap/Types.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Search::Sitemap::Types;
2             $Search::Sitemap::Types::VERSION = '2.13_01';
3 1     1   58387 use 5.008003;
  1         2  
  1         29  
4 1     1   4 use strict;
  1         2  
  1         21  
5 1     1   3 use warnings;
  1         2  
  1         30  
6             our $AUTHORITY = 'cpan:JASONK';
7              
8 1     1   1235 use Class::Load;
  1         23325  
  1         56  
9 0           use MooseX::Types -declare => [qw(
10             SitemapURL SitemapUrlStore SitemapChangeFreq SitemapLastMod SitemapPriority
11             XMLPrettyPrintValue XMLTwig SitemapPinger
12 1     1   243 )];
  0            
13             use MooseX::Types::Moose qw( Object HashRef Str Int Bool Num );
14             use MooseX::Types::URI qw( Uri );
15             use POSIX qw( strftime );
16              
17             class_type( 'Search::Sitemap::URL' );
18             subtype SitemapURL, as 'Search::Sitemap::URL';
19              
20             coerce SitemapURL,
21             from HashRef, via {
22             Class::Load::load_class( 'Search::Sitemap::URL' );
23             Search::Sitemap::URL->new( $_ );
24             },
25             from Str, via {
26             Class::Load::load_class( 'Search::Sitemap::URL' );
27             Search::Sitemap::URL->new( loc => $_ );
28             };
29              
30             subtype XMLPrettyPrintValue, as Str, where {
31             $_ =~ m{ ^ (
32             none | nsgmls | nice | indented | indented_c | indented_a | cvs |
33             wrapped | record | record_c
34             ) $ }x;
35             };
36              
37             coerce XMLPrettyPrintValue,
38             from Int, via { $_ ? 'nice' : 'none' },
39             from Str, via { $_ ? 'nice' : 'none' };
40              
41             subtype SitemapChangeFreq, as Str, where {
42             $_ =~ m{ ^ (
43             always | hourly | daily | weekly | monthly | yearly | never
44             ) $ }x;
45             };
46             coerce SitemapChangeFreq, from Str, via {
47             my %types = (
48             a => 'always',
49             h => 'hourly',
50             d => 'daily',
51             w => 'weekly',
52             m => 'monthly',
53             y => 'yearly',
54             n => 'never',
55             );
56             if ( m{ ^( [ahdwmyn] ) }ix ) { return $types{ lc $1 } }
57             return;
58             };
59              
60             my $lastmod_re = qr/ ^
61             (\d{4}) # year
62             \-?
63             (\d{2})? # month
64             \-?
65             (\d{2})? # day
66             (?:
67             T
68             (\d{2}) # hours
69             :
70             (\d{2}) # minutes
71             (?:
72             :(\d{2}) # seconds
73             (?:
74             \.(\d+) # fraction of second
75             )?
76             )?
77             (?:
78             (Z|[\+\-]?\d{2}:\d{2}) # time zone
79             )?
80             )?
81             $ /xi;
82              
83             subtype SitemapLastMod, as Str, where { /$lastmod_re/ };
84              
85             class_type 'DateTime';
86             class_type 'HTTP::Response';
87             class_type 'File::stat';
88             class_type 'Path::Class::File';
89              
90             coerce SitemapLastMod,
91             from Str, via {
92             if ( $_ eq 'now' ) {
93             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( time ) );
94             } elsif ( $_ =~ /^\d+$/ ) {
95             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( $_ ) );
96             }
97             return $_;
98             },
99             from Num, via {
100             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( $_ ) );
101             },
102             from 'DateTime', via {
103             my ( $datetime, $tzoff ) = $_->strftime("%Y-%m-%dT%H:%M:%S","%z");
104             if ( $tzoff =~ /^([+\-])?(\d\d):?(\d\d)/ ) {
105             $tzoff = sprintf( '%s%02d:%02d', $1 || '+', $2, $3 || 0 );
106             } else {
107             $tzoff = '+00:00';
108             }
109             return $datetime.$tzoff;
110             },
111             from 'HTTP::Response', via {
112             my $modtime = $_->last_modified || ( time - $_->current_age );
113             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( $modtime ) );
114             },
115             from 'File::stat', via {
116             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( $_->mtime ) );
117             },
118             from 'Path::Class::File', via {
119             return strftime( "%Y-%m-%dT%H:%M:%S+00:00", gmtime( $_->stat->mtime ) );
120             };
121              
122             subtype SitemapPriority, as Num, where { $_ >= 0 && $_ <= 1 };
123             coerce SitemapPriority,
124             from Num, via { $_ };
125              
126             class_type( 'Search::Sitemap::URLStore' );
127             subtype SitemapUrlStore, as 'Search::Sitemap::URLStore';
128              
129             coerce SitemapUrlStore,
130             from HashRef, via {
131             my $type = $_->{ 'type' } || 'Memory';
132             my $class = $type =~ /::/
133             ? $type
134             : 'Search::Sitemap::URLStore::'.$type;
135             Class::Load::load_class( $class );
136             $class->new( $_ )
137             },
138             from Str, via {
139             my $class = 'Search::Sitemap::URLStore::'.$_;
140             Class::Load::load_class( $class );
141             return $class->new;
142             };
143              
144             class_type( 'XML::Twig' );
145              
146             class_type( 'Search::Sitemap::Pinger' );
147              
148             subtype SitemapPinger, as 'Search::Sitemap::Pinger';
149             coerce SitemapPinger, from Str, via {
150             my $class = 'Search::Sitemap::Pinger::'.$_;
151             Class::Load::load_class( $class );
152             return $class->new;
153             };
154              
155             class_type( 'LWP::UserAgent' );
156              
157             1;
158             __END__
159              
160             =head1 NAME
161              
162             Search::Sitemap::Types - MooseX::Types library for Search::Sitemap
163              
164             =head1 DESCRIPTION
165              
166             This is a L<MooseX::Types> library containing type constrations and coercions
167             for L<Search::Sitemap>.
168              
169             =head1 SEE ALSO
170              
171             L<Search::Sitemap>
172              
173             L<MooseX::Types>
174              
175             =head1 AUTHOR
176              
177             Jason Kohles, E<lt>email@jasonkohles.comE<gt>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             Copyright (C) 2005-2009 by Jason Kohles
182              
183             This library is free software; you can redistribute it and/or modify
184             it under the same terms as Perl itself.
185              
186             =cut
187