blib/lib/Mojolicious/Plugin/Surveil.pm

filter: Statementstmt 87.1% 27 / 31 Branchbran 75.0% 3 / 4 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 82.7% 48 / 58 CC 8 SCAR 12.2
CC
8
Cov
84%
register
5.0
_hook_after_render
4.1
_default_message_handler
2.0
CRAP 3.4
↑ Summary
1 package Mojolicious::Plugin::Surveil;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.03';
7  
8 2 ▶sub register {
Subroutine register: called CC=5 SCAR=16.1
Pod documented
9 2 my ($self, $app, $config) = @_;
10  
11 2 ▶ $config->{enable_param} ||= '_surveil';
Condition100%2/2
$config->{"enable_param"} ||= "_surveil"
l!l
11
MC/DC100%1/1
$config->{"enable_param"} ||= "_surveil"
$config->{"enable_param"}
Truth table100%2/2
A
A= $config->{"enable_param"}
Aresult
11
00
12 2 ▶ $config->{events} ||= [qw(blur click focus touchstart touchcancel touchend)];
Condition50%1/2
$config->{"events"} ||= ["blur", "click", "focus", "touchstart", "touchcancel", "touchend"]
l!l
02
MC/DC0%0/1
$config->{"events"} ||= ["blur", "click", "focus", "touchstart", "touchcancel", "touchend"]
$config->{"events"}
Truth table50%1/2
A
A= $config->{"events"}
Aresult
11
00
13 2 ▶ $config->{handler} ||= \&_default_message_handler;
Condition100%2/2
$config->{"handler"} ||= \&Mojolicious::Plugin::Surveil::_default_message_handler
l!l
11
MC/DC100%1/1
$config->{"handler"} ||= \&Mojolicious::Plugin::Surveil::_default_message_handler
$config->{"handler"}
Truth table100%2/2
A
A= $config->{"handler"}
Aresult
11
00
14 2 ▶ $config->{path} ||= '/mojolicious/plugin/surveil';
Condition100%2/2
$config->{"path"} ||= "/mojolicious/plugin/surveil"
l!l
11
MC/DC100%1/1
$config->{"path"} ||= "/mojolicious/plugin/surveil"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
Aresult
11
00
15  
16 2 push @{$app->renderer->classes}, __PACKAGE__;
17 2 ▶ $app->hook(after_render => sub { _hook_after_render($config, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 $app->routes->websocket($config->{path})->to(
20 1 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 1 my $c = shift->inactivity_timeout(60);
22 1 $c->on(json => $config->{handler});
23 }
24 2 );
25 }
26  
27 0 ▶sub _default_message_handler {
Subroutine _default_message_handler: not called CC=1 SCAR=6.9
28 0 my ($c, $e) = @_;
29 0 my ($type, $target) = delete @$e{qw(type target)};
30 0 $c->app->log->debug(qq(Event "$type" on "$target" @{[encode_json $e]}));
31 }
32  
33 3 ▶sub _hook_after_render {
Subroutine _hook_after_render: called CC=4 SCAR=14.0
34 3 my ($config, $c, $output, $format) = @_;
35 3 ▶ return if $format ne 'html';
Branch: 1 branch
BranchTrueFalseTotal
if $format ne "html" 0 3 3
36 3 ▶ return if !$c->param($config->{enable_param});
Branch: 1 branch
BranchTrueFalseTotal
unless $c->param($config->{"enable_param"}) 1 2 3
37  
38 2 ▶ my $scheme = $c->req->url->to_abs->scheme || 'http';
Condition50%1/2
$c->req->url->to_abs->scheme || 'http'
l!l
20
MC/DC0%0/1
$c->req->url->to_abs->scheme || 'http'
$c->req->url->to_abs->scheme
Truth table50%1/2
A
A= $c->req->url->to_abs->scheme
Aresult
11
00
39 2 $scheme =~ s!^http!ws!;
40  
41 my $js = $c->render_to_string(
42 template => 'mojolicious/plugin/surveil',
43 events => encode_json($config->{events}),
44 2 surveil_url => $c->url_for($config->{path})->to_abs->scheme($scheme),
45 );
46  
47 2 $$output =~ s!</head>!$js</head>!;
48 }
49  
50 2 1;
51  
52 =encoding utf8
53  
54 =head1 NAME
55  
56 Mojolicious::Plugin::Surveil - Surveil user actions
57  
58 =head1 VERSION
59  
60 0.03
61  
62 =head1 DESCRIPTION
63  
64 L<Mojolicious::Plugin::Surveil> is a plugin which allow you to see every
65 event a user trigger on your web page. It is meant as a debug tool for
66 seeing events, even if the browser does not have a JavaScript console.
67  
68 CAVEAT: The JavaScript that is injected require WebSocket in the browser to
69 run. The surveil events are attached to the "body" element, so any other event
70 that prevent events from bubbling will not emit this to the WebSocket
71 resource.
72  
73 =head1 SYNOPSIS
74  
75 =head2 Application
76  
77 use Mojolicious::Lite;
78 plugin "surveil";
79  
80 =head2 In your browser
81  
82 Visit L<http://localhost:3000?_surveil=1> to enable the logging. Try clicking
83 around on your page and look in the console for log messages.
84  
85 =head2 Custom event handler
86  
87 use Mojo::Redis;
88 use Mojo::JSON "encode_json";
89  
90 plugin "surveil", {
91 handler => sub {
92 my ($c, $event) = @_;
93 my $ip = $c->tx->remote_address;
94 $c->redis->pubsub->notify("surveil:$ip" => encode_json $event);
95 }
96 };
97  
98 The above example is useful if you want to publish the events to
99 L<Redis|Mojo::Redis> instead of a log file. A developer can then run commands
100 below to see what a given user is doing:
101  
102 $ redis-cli psubscribe "surveil:*"
103 $ redis-cli subscribe "surveil:192.168.0.100"
104  
105 =head1 METHODS
106  
107 =head2 register
108  
109 $self->register($app, \%config);
110 $app->plugin("surveil" => \%config);
111  
112 Used to add an "after_render" hook into the application which adds a
113 JavaScript to every HTML document when the L</enable_param> is set.
114  
115 C<%config> can have the following settings:
116  
117 =over 2
118  
119 =item * enable_param
120  
121 Used to specify a query parameter to be part of the URL to enable surveil.
122  
123 Default is "_surveil".
124  
125 =item * events
126  
127 The events that should be reported back over the WebSocket.
128  
129 Defaults to blur, click, focus, touchstart, touchcancel and touchend.
130  
131 Note that the default list might change in the future.
132  
133 =item * handler
134  
135 A code ref that handles the events from the web page. This is useful if you
136 want to post them to an event bus instead of in the log file.
137  
138 =item * path
139  
140 The path to the WebSocket route.
141  
142 Defaults to C</mojolicious/plugin/surveil>.
143  
144 =back
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 Copyright (C) 2014-2018, Jan Henning Thorsen
149  
150 This program is free software, you can redistribute it and/or modify it under
151 the terms of the Artistic License version 2.0.
152  
153 =head1 AUTHOR
154  
155 Jan Henning Thorsen - C<jhthorsen@cpan.org>
156  
157 =cut
158  
159 __DATA__
↑ Summary