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.016';
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   5574 use Mojo::Base 'Mojolicious::Controller';
  2         4  
  2         12  
14 2     2   1032 use Mercury::Pattern::Bus;
  2         4  
  2         14  
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 3758 my ( $c ) = @_;
35              
36 6         18 my $topic = $c->stash( 'topic' );
37 6         54 my $pattern = $c->_pattern( $topic );
38 6         17 $pattern->add_peer( $c->tx );
39 6 100       18 if ( $c->param( 'echo' ) ) {
40             $c->tx->on( message => sub {
41 1     1   341 my ( $tx, $msg ) = @_;
42 1         5 $tx->send( $msg );
43 1         229 } );
44             }
45              
46 6         942 $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 1372 my ( $c ) = @_;
59 1         3 my $topic = $c->stash( 'topic' );
60 1         24 my $pattern = $c->_pattern( $topic );
61 1         4 $pattern->send_message( $c->req->body );
62 1         538 $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   16 my ( $c, $topic ) = @_;
79 7         40 my $pattern = $c->mercury->pattern( Bus => $topic );
80 7 100       39 if ( !$pattern ) {
81 3         13 $pattern = Mercury::Pattern::Bus->new;
82 3         21 $c->mercury->pattern( Bus => $topic => $pattern );
83             }
84 7         12 return $pattern;
85             }
86              
87             1;
88              
89             __END__