blib/lib/Bootylicious/Plugin/Pingback.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 148 CC 17 SCAR 33.7
CC
17
Cov
0%
_pingback
56.0
_parse_xmlrpc
56.0
register
20.0
CRAP 29.0
1 package Bootylicious::Plugin::Pingback;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Mojolicious::Plugin';
7  
8 use Mojo::DOM;
9  
10 sub register {
11 my ($self, $app, $conf) = @_;
12  
13 $conf ||= {};
14  
15 $app->routes->route('/pingback')->to(cb => \&_pingback)->name('pingback');
16 unshift @{$app->renderer->classes}, __PACKAGE__;
17  
18 $app->plugins->on(
19 after_dispatch => sub {
20 my ($c) = @_;
21  
22 return unless $c->req->method =~ m/GET|HEAD/;
23  
24 return unless $c->res->code && $c->res->code == 200;
25  
26 return unless $c->match->endpoint->name eq 'article';
27  
28 $c->res->headers->header(
29 'X-Pingback' => $c->url_for('pingback', format => undef)->to_abs);
30 }
31 );
32 }
33  
34 sub _pingback {
35 my $self = shift;
36  
37 my ($source_uri, $target_uri) = _parse_xmlrpc($self);
38 return _render_bad_request($self) unless $source_uri && $target_uri;
39  
40 return _render_target_invalid($self)
41 unless $target_uri =~ m{^/articles/(\d+)/(\d+)/(.*)};
42  
43 my ($year, $month, $name) = ($1, $2, $3);
44 $name =~ s/\..*$//;
45  
46 my $article = $self->get_article($year, $month, $name);
47 return _render_target_not_found($self) unless $article;
48  
49 $self->app->log->debug("Fetching $source_uri...");
50  
51 $self->ua->get(
52 $source_uri => sub {
53 my $tx = pop;
54  
55 $self->app->log->debug("Fetched $source_uri");
56  
57 return _render_source_not_found($self)
58 unless $tx->res->code && $tx->res->code == 200;
59  
60 return _render_source_invalid($self)
61 unless $tx->res->body =~ m{\Q$target_uri\E};
62  
63 return _render_pingback_already_registered($self)
64 if $article->has_pingback($source_uri);
65  
66 $article->pingback($source_uri);
67  
68 return _render_success($self);
69 }
70 );
71 }
72  
73 sub _parse_xmlrpc {
74 my $self = shift;
75  
76 return unless $self->req->method eq 'POST' && $self->req->body;
77  
78 my $dom = Mojo::DOM->new;
79 $dom = $dom->parse($self->req->body);
80  
81 my $method = $dom->at('methodCall');
82 return unless $method;
83  
84 my $method_name = $method->at('methodName');
85 return unless $method_name->text eq 'pingback.ping';
86  
87 my ($source_uri, $target_uri) =
88 $method->find('params > param > value > string')->each;
89 return unless $source_uri && $target_uri;
90  
91 $source_uri = $source_uri->text;
92 $target_uri = $target_uri->text;
93  
94 my $url = $self->url_for('/')->to_abs;
95 return unless $target_uri =~ s/^\Q$url\E//;
96  
97 $target_uri = "/$target_uri" unless $target_uri =~ m{^/};
98  
99 return ($source_uri, $target_uri);
100 }
101  
102 sub _render_success {
103 my $self = shift;
104 my $message = shift;
105  
106 $message ||= 'Success';
107  
108 $self->render(
109 'success',
110 message => $message,
111 layout => undef
112 );
113 }
114  
115 sub _render_bad_request { _render_error(shift, 0 => 'Bad request') }
116  
117 sub _render_target_not_found {
118 _render_error(shift, 32 => 'The specified target URI does not exist.');
119 }
120  
121 sub _render_target_invalid {
122 _render_error(shift,
123 33 => 'The specified target URI cannot be used as a target.');
124 }
125  
126 sub _render_source_not_found {
127 _render_error(shift, 16 => 'The source URI does not exist.');
128 }
129  
130 sub _render_source_invalid {
131 _render_error(shift,
132 17 =>
133 'The source URI does not contain a link to the target URI, and so cannot be used as a source.'
134 );
135 }
136  
137 sub _render_pingback_already_registered {
138 _render_error(shift, 48 => 'The pingback has already been registered.');
139 }
140  
141 sub _render_error {
142 my $self = shift;
143 my ($code, $message) = @_;
144  
145 $self->res->code(400) unless $code;
146  
147 $self->render(
148 'fault',
149 code => $code,
150 message => $message,
151 layout => undef
152 );
153 }
154  
155 1;
156 __DATA__