Jifty/Plugin/RequestInspector.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 187 CC 20 SCAR 23.2
CC
20
Cov
0%
after_request
20.0
should_handle_request
20.0
requests
12.0
CRAP 10.2
1 package Jifty::Plugin::RequestInspector;
2 use strict;
3 use warnings;
4 use base 'Jifty::Plugin';
5 use Time::HiRes 'time';
6  
7 sub version { '0.0.2' }
8  
9 __PACKAGE__->mk_accessors(qw(url_filter on_cookie persistent));
10  
11 my $current_inspection;
12 my @requests;
13  
14 sub init {
15 my $self = shift;
16 return if $self->_pre_init;
17  
18 my %opt = (
19 url_filter => '.*',
20 on_cookie => undef,
21 persistent => 0,
22 @_
23 );
24  
25 $self->url_filter(qr/$opt{url_filter}/);
26 $self->on_cookie($opt{on_cookie});
27 $self->persistent($opt{persistent});
28  
29 Jifty::Handler->add_trigger(before_request => sub {
30 $self->before_request(@_);
31 });
32  
33 Jifty::Handler->add_trigger(after_request => sub {
34 $self->after_request(@_);
35 });
36 }
37  
38 sub requests {
39 my $self = shift;
40 my %args = (
41 after => 0,
42 @_,
43 );
44  
45 if ($self->persistent) {
46 my $requests = Jifty::Plugin::RequestInspector::Model::RequestCollection->new(
47 current_user => Jifty->app_class('CurrentUser')->superuser
48 );
49 $requests->unlimit;
50 $requests->limit( column => "id", operator => ">", value => $args{after}) if $args{after};
51 return map { {%{$_->data}, id => $_->id} } @{$requests->items_array_ref};
52 } else {
53 return @requests[$args{after}..$#requests];
54 }
55 }
56  
57 sub get_request {
58 my $self = shift;
59 my $id = shift;
60  
61 if ($self->persistent) {
62 my $req = Jifty::Plugin::RequestInspector::Model::Request->new(
63 current_user => Jifty->app_class('CurrentUser')->superuser
64 );
65 $req->load( $id );
66 return undef unless $req->id;
67 return { %{$req->data}, id => $req->id };
68 } else {
69 return $requests[$id - 1]; # 1-based
70 }
71 }
72  
73 sub add_request {
74 my $self = shift;
75  
76 return unless $current_inspection;
77  
78 if ($self->persistent) {
79 my $req = Jifty::Plugin::RequestInspector::Model::Request->new(
80 current_user => Jifty->app_class('CurrentUser')->superuser
81 );
82 my ($ok, $msg) = $req->create(
83 data => $current_inspection
84 );
85 } else {
86 push @requests, $current_inspection;
87 $requests[-1]{id} = scalar @requests;
88 }
89 }
90  
91 sub clear_requests {
92 my $self = shift;
93  
94 if ($self->persistent) {
95 Jifty->handle->simple_query( "DELETE FROM ".Jifty::Plugin::RequestInspector::Model::Request->table );
96 } else {
97 @requests = ();
98 }
99 undef $current_inspection;
100 }
101  
102 sub last_id {
103 my $self = shift;
104 if ($self->persistent) {
105 return Jifty->handle->fetch_result( "SELECT MAX(id) FROM ". Jifty::Plugin::RequestInspector::Model::Request->table );
106 } else {
107 return scalar @requests;
108 }
109 }
110  
111 sub get_plugin_data {
112 my $self = shift;
113 my $id = shift;
114 my $plugin = shift;
115  
116 return $self->get_request($id)->{plugin_data}{$plugin};
117 }
118  
119 sub get_all_plugin_data {
120 my $self = shift;
121 my $plugin = shift;
122  
123 return map {$_->{plugin_data}{$plugin}} $self->requests;
124 }
125  
126 sub new_request_inspection {
127 my ($self, $req) = @_;
128  
129 my $ret = {
130 start => time,
131 url => $req->request_uri,
132 };
133  
134 if (my $cookie_name = $self->on_cookie) {
135 $ret->{cookie} = $req->cookies->{$cookie_name};
136 }
137 return $ret;
138 }
139  
140 do {
141 my $inspector_plugins;
142 sub inspector_plugins {
143 if (!defined($inspector_plugins)) {
144 $inspector_plugins = [
145 grep {
146 $_->can('inspect_before_request') ||
147 $_->can('inspect_after_request')
148 } Jifty->plugins
149 ];
150 }
151 return @$inspector_plugins;
152 }
153 };
154  
155 sub before_request {
156 my ($self, $handler, $req) = @_;
157  
158 return unless $self->should_handle_request($req);
159  
160 $current_inspection = $self->new_request_inspection($req);
161  
162 for my $plugin ($self->inspector_plugins) {
163 next unless $plugin->can('inspect_before_request');
164 my $plugin_data = $plugin->inspect_before_request($req);
165 $current_inspection->{plugin_data}{ref $plugin} = $plugin_data;
166 }
167 }
168  
169 sub after_request {
170 my ($self, $handler, $req) = @_;
171  
172 if ($current_inspection) {
173 for my $plugin (reverse $self->inspector_plugins) {
174 next unless $plugin->can('inspect_after_request');
175 my $plugin_data = $current_inspection->{plugin_data}{ref $plugin};
176 my $new_plugin_data =
177 $plugin->inspect_after_request( $plugin_data, $req );
178 if (defined($new_plugin_data)) {
179 $current_inspection->{plugin_data}{ref $plugin} = $new_plugin_data;
180 }
181 }
182 $current_inspection->{end} = time;
183 $self->add_request;
184 }
185  
186 undef $current_inspection;
187 }
188  
189 sub should_handle_request {
190 my $self = shift;
191 my $req = shift;
192  
193 my $url = $req->request_uri;
194 return unless $url =~ $self->url_filter;
195  
196 if (my $cookie_name = $self->on_cookie) {
197 return unless $req->cookies->{$cookie_name};
198 }
199  
200 return 1;
201 }
202  
203 1;
204  
205 __END__