File Coverage

blib/lib/Search/Sitemap/Ping.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::Ping;
2             $Search::Sitemap::Ping::VERSION = '2.13_01';
3 1     1   63185 use 5.008003;
  1         3  
  1         29  
4 1     1   4 use strict;
  1         1  
  1         22  
5 1     1   3 use warnings;
  1         5  
  1         28  
6             our $AUTHORITY = 'cpan:JASONK';
7 1     1   185 use Moose;
  0            
  0            
8             use Search::Sitemap::Pinger;
9             use Class::Trigger qw(
10             progress success failure
11             before_submit after_submit
12             before_engine after_engine
13             );
14             use MooseX::Types::Moose qw( ArrayRef );
15             use namespace::clean -except => [qw( meta add_trigger call_trigger )];
16              
17             has 'urls' => (
18             is => 'rw',
19             isa => ArrayRef,
20             lazy => 1,
21             auto_deref => 1,
22             default => sub { [] },
23             );
24              
25             has 'engines' => (
26             is => 'rw',
27             isa => ArrayRef['Search::Sitemap::Pinger'],
28             auto_deref => 1,
29             lazy => 1,
30             default => sub { [
31             map { $_->new } Search::Sitemap::Pinger->ALL_PINGERS
32             ] },
33             );
34              
35             sub BUILDARGS {
36             my $class = shift;
37             my @urls = ();
38             while ( @_ && $_[0] =~ m{^https?://} ) { push( @urls, shift ) }
39             my $args = $class->SUPER::BUILDARGS( @_ );
40             push( @{ $args->{ 'urls' } ||= [] }, @urls );
41             return $args;
42             }
43              
44             sub submit {
45             my $self = shift;
46              
47             $self->call_trigger( 'before_submit' );
48             my $total = @{ $self->urls } * @{ $self->engines };
49             my $attempt = 0;
50             my $success = 0;
51             my $failure = 0;
52             my $progress = sub {
53             my $percent = sprintf( '%.02f', ( $attempt / $total ) * 100 );
54             $self->call_trigger( 'progress',
55             $percent, $total, $attempt, $success, $failure
56             );
57             };
58             $progress->();
59             for my $engine ( $self->engines ) {
60             my @urls = $self->urls;
61             $self->call_trigger( 'before_engine', $engine, \@urls );
62             next unless @urls;
63             $engine->submit( sub {
64             my ( $status, $url, $msg ) = @_;
65             $attempt++;
66             if ( $status eq 'success' ) {
67             $success++;
68             } else {
69             $failure++;
70             }
71             unless ( $progress->() ) {
72             if ( $status eq 'failure' ) {
73             warn "Submitting $url to $engine failed: $msg\n";
74             }
75             }
76             }, @urls );
77             $self->call_trigger( 'after_engine', $engine );
78             }
79             $self->call_trigger( 'after_submit' );
80             }
81              
82             __PACKAGE__->meta->make_immutable;
83             1;
84             __END__
85              
86             =head1 NAME
87              
88             Search::Sitemap::Ping - Notify search engines of sitemap updates
89              
90             =head1 SYNOPSIS
91              
92             use Search::Sitemap::Ping;
93            
94             my $ping = Search::Sitemap::Ping->new(
95             'http://www.jasonkohles.com/sitemap.gz',
96             );
97            
98             $ping->submit;
99            
100             for my $url ( $ping->urls ) {
101             print "$url\n";
102             for my $engine ( $ping->engines ) {
103             printf( " %25s %s\n", $engine, $ping->status( $url, $engine ) );
104             }
105             }
106              
107             =head1 DESCRIPTION
108              
109             This module makes it easy to notify search engines that your sitemaps, or
110             sitemap indexes, have been updated. See L<Search::Sitemap> and
111             L<Search::Sitemap::Index> for tools to help you create sitemaps and indexes.
112              
113             =head1 METHODS
114              
115             =head2 new
116              
117             Create a new L<Search::Sitemap::Ping> object.
118              
119             =head2 add_url( @urls )
120              
121             Add one or more urls to the list of URLs to submit.
122              
123             =head2 urls
124              
125             Return the list of urls that will be (or were) submitted.
126              
127             =head2 add_engine( @engines )
128              
129             Add one or more search engines to the list of search engines to submit to.
130              
131             =head2 engines
132              
133             Return the list of search engines that will be (or were) submitted to.
134              
135             =head2 submit
136              
137             Submit the urls to the search engines, returns the number of successful
138             submissions. This module uses L<LWP::UserAgent> for the web-based submissions,
139             and will honor proxy settings in the environment. See L<LWP::UserAgent> for
140             more information.
141              
142             =head2 status( $url [, $engine ] )
143              
144             Returns the status of the indicated submission. The URL must be specified,
145             If an engine is specified it will return just the status of the submission
146             to that engine, otherwise it will return a hashref of the engines that the url
147             will be (or was) submitted to, and the status for each one.
148              
149             The status may be one of:
150              
151             =over 4
152              
153             =item * undef or empty string
154              
155             Not submitted yet.
156              
157             =item * 'SUCCESS'
158              
159             Succesfully submitted. Note that this just means it was successfully
160             transferred to the search engine, if there are problems in the file the
161             search engine may reject it later when it attempts to use it.
162              
163             =item * HTTP Error String
164              
165             In case of an error, the error string will be provided as the status.
166              
167             =back
168              
169              
170             =head1 SEE ALSO
171              
172             L<Search::Sitemap>
173              
174             =head1 AUTHOR
175              
176             Jason Kohles, E<lt>email@jasonkohles.comE<gt>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             Copyright (C) 2005-2009 by Jason Kohles
181              
182             This library is free software; you can redistribute it and/or modify
183             it under the same terms as Perl itself.
184              
185             =cut
186