File Coverage

blib/lib/Mercury/Controller/Bus.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package Mercury::Controller::Bus;
2             our $VERSION = '0.015';
3             # ABSTRACT: A messaging pattern where all subscribers share messages
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod =head1 DESCRIPTION
8             #pod
9             #pod =head1 SEE ALSO
10             #pod
11             #pod =cut
12              
13 2     2   4145 use Mojo::Base 'Mojolicious::Controller';
  2         22  
  2         18  
14 2     2   1289 use Mercury::Pattern::Bus;
  2         7  
  2         21  
15              
16             #pod =method connect
17             #pod
18             #pod Establish a WebSocket message bus to send/receive messages on the given
19             #pod C. All clients connected to the topic will receive all messages
20             #pod published on the topic.
21             #pod
22             #pod This is a shorter way of doing both C and C,
23             #pod without the hierarchical message passing.
24             #pod
25             #pod One difference is that by default a sender will not receive a message
26             #pod that they sent. To enable this behavior, pass a true value as the C
27             #pod query parameter when establishing the websocket.
28             #pod
29             #pod $ua->websocket('/bus/foo?echo=1' => sub { ... });
30             #pod
31             #pod =cut
32              
33             sub connect {
34 6     6 1 6225 my ( $c ) = @_;
35              
36 6         33 my $topic = $c->stash( 'topic' );
37 6         94 my $pattern = $c->_pattern( $topic );
38 6         44 $pattern->add_peer( $c->tx );
39 6 100       35 if ( $c->param( 'echo' ) ) {
40             $c->tx->on( message => sub {
41 1     1   554 my ( $tx, $msg ) = @_;
42 1         6 $tx->send( $msg );
43 1         377 } );
44             }
45              
46 6         1636 $c->rendered( 101 );
47             };
48              
49             #pod =method post
50             #pod
51             #pod Post a new message to the given topic without subscribing or
52             #pod establishing a WebSocket connection. This allows new messages to be
53             #pod pushed by any HTTP client.
54             #pod
55             #pod =cut
56              
57             sub post {
58 1     1 1 1734 my ( $c ) = @_;
59 1         4 my $topic = $c->stash( 'topic' );
60 1         21 my $pattern = $c->_pattern( $topic );
61 1         4 $pattern->send_message( $c->req->body );
62 1         784 $c->render(
63             status => 200,
64             text => '',
65             );
66             }
67              
68             #=method _pattern
69             #
70             # my $pattern = $c->_pattern( $topic );
71             #
72             # Get or create the L object for the given
73             # topic.
74             #
75             #=cut
76              
77             sub _pattern {
78 7     7   25 my ( $c, $topic ) = @_;
79 7         63 my $pattern = $c->mercury->pattern( Bus => $topic );
80 7 100       69 if ( !$pattern ) {
81 3         20 $pattern = Mercury::Pattern::Bus->new;
82 3         36 $c->mercury->pattern( Bus => $topic => $pattern );
83             }
84 7         23 return $pattern;
85             }
86              
87             1;
88              
89             __END__