Mercury/Controller/PushPull.pm

filter: Statementstmt 100% 27 / 27 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 100% 38 / 38 CC 2 SCAR 2.5
CC
2
Cov
100%
_pattern
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« Mercury/Command/mercury.pm ↑ Summary
1 package Mercury::Controller::PushPull;
2 5 our $VERSION = '0.016';
3 # ABSTRACT: Push/pull message pattern controller
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # myapp.pl
8 #pod use Mojolicious::Lite;
9 #pod plugin 'Mercury';
10 #pod websocket( '/push/*topic' )
11 #pod ->to( controller => 'PushPull', action => 'push' );
12 #pod websocket( '/pull/*topic' )
13 #pod ->to( controller => 'PushPull', action => 'pull' );
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This controller enables a L<pushE<sol>pull pattern|Mercury::Pattern::PushPull> on
18 #pod a pair of endpoints (L<push|/push> and L<pull|/pull>.
19 #pod
20 #pod For more information on the push/pull pattern, see L<Mercury::Pattern::PushPull>.
21 #pod
22 #pod =head1 SEE ALSO
23 #pod
24 #pod =over
25 #pod
26 #pod =item L<Mercury::Pattern::PushPull>
27 #pod
28 #pod =item L<Mercury>
29 #pod
30 #pod =back
31 #pod
32 #pod =cut
33  
34 5 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use Mercury::Pattern::PushPull;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 #pod =method push
38 #pod
39 #pod $app->routes->websocket( '/push/*topic' )
40 #pod ->to( controller => 'PushPull', action => 'push' );
41 #pod
42 #pod Controller action to connect a websocket to a push endpoint. A push client
43 #pod sends messages through the socket. The message will be sent to one of the
44 #pod connected pull clients in a round-robin fashion.
45 #pod
46 #pod This endpoint requires a C<topic> in the stash.
47 #pod
48 #pod =cut
49  
50 2 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
51 2 my ( $c ) = @_;
52 2 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
53 2 $pattern->add_pusher( $c->tx );
54 2 $c->rendered( 101 );
55 }
56  
57 #pod =method pull
58 #pod
59 #pod $app->routes->websocket( '/pull/*topic' )
60 #pod ->to( controller => 'PushPull', action => 'pull' );
61 #pod
62 #pod Controller action to connect a websocket to a pull endpoint. A pull
63 #pod client will recieve messages from push clients in a round-robin fashion.
64 #pod One message from a pusher will be received by exactly one puller.
65 #pod
66 #pod This endpoint requires a C<topic> in the stash.
67 #pod
68 #pod =cut
69  
70 6 ▶sub pull {
Subroutine pull: called CC=1 SCAR=0.0
Pod documented
71 6 my ( $c ) = @_;
72 6 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
73 6 $pattern->add_puller( $c->tx );
74 6 $c->rendered( 101 );
75 }
76  
77 #pod =method post
78 #pod
79 #pod Post a new message to the given topic without subscribing or
80 #pod establishing a WebSocket connection. This allows new messages to be
81 #pod easily pushed by any HTTP client.
82 #pod
83 #pod =cut
84  
85 1 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
86 1 my ( $c ) = @_;
87 1 my $topic = $c->stash( 'topic' );
88 1 my $pattern = $c->_pattern( $topic );
89 1 $pattern->send_message( $c->req->body );
90 1 $c->render(
91 status => 200,
92 text => '',
93 );
94 }
95  
96 #=method _pattern
97 #
98 # my $pattern = $c->_pattern( $topic );
99 #
100 # Get or create the L<Mercury::Pattern::PushPull> object for the given
101 # topic.
102 #
103 #=cut
104  
105 9 ▶sub _pattern {
Subroutine _pattern: called CC=2 SCAR=6.9
106 9 my ( $c, $topic ) = @_;
107 9 my $pattern = $c->mercury->pattern( PushPull => $topic );
108 9 ▶ if ( !$pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern) 3 6 9
109 3 $pattern = Mercury::Pattern::PushPull->new;
110 3 $c->mercury->pattern( PushPull => $topic => $pattern );
111 }
112 9 return $pattern;
113 }
114  
115 5 1;
116  
117 __END__
« Mercury/Command/mercury.pm ↑ Summary