| 1 | package Mojolicious::Matterbridge; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Moo::Role 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use API::Matterbridge::Message; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use feature 'current_sub'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Filter::signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use feature 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | ▶ | use API::Matterbridge::Message; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 1 | our $VERSION = '0.02'; | |||||||||||
| 18 | |||||||||||||
| 19 | =head1 NAME | ||||||||||||
| 20 | |||||||||||||
| 21 | Mojolicious::Matterbridge - a simplistic module to connect to chat servers via matterbridge | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||
| 24 | |||||||||||||
| 25 | use Mojolicious::Matterbridge; | ||||||||||||
| 26 | |||||||||||||
| 27 | my $client = Mojolicious::Matterbridge->new( | ||||||||||||
| 28 | url => 'http://localhost:4242/api/', | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | |||||||||||||
| 31 | $client->on('message' => sub( $c, $message ) { | ||||||||||||
| 32 | print sprintf "<%s> %s\n", $message->username, $message->text; | ||||||||||||
| 33 | $client->send( "Haha!" ); | ||||||||||||
| 34 | }); | ||||||||||||
| 35 | $client->connect(); | ||||||||||||
| 36 | |||||||||||||
| 37 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | with 'API::Matterbridge'; | |||||||||||
| 42 | #use Mojo::Future; | ||||||||||||
| 43 | |||||||||||||
| 44 | has 'stream_ua' => ( | ||||||||||||
| 45 | is => 'lazy', | ||||||||||||
| 46 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 47 | 0 | Mojo::UserAgent->new( | |||||||||||
| 48 | inactivity_timeout => 0, # permanent connection to localhost | ||||||||||||
| 49 | max_response_size => 0, # we will be retrieving streams | ||||||||||||
| 50 | ), | ||||||||||||
| 51 | }, | ||||||||||||
| 52 | 1 | ); | |||||||||||
| 53 | |||||||||||||
| 54 | has 'short_ua' => ( | ||||||||||||
| 55 | is => 'lazy', | ||||||||||||
| 56 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 57 | 0 | Mojo::UserAgent->new( | |||||||||||
| 58 | inactivity_timeout => 0, # permanent connection to localhost | ||||||||||||
| 59 | ), | ||||||||||||
| 60 | }, | ||||||||||||
| 61 | 1 | ); | |||||||||||
| 62 | |||||||||||||
| 63 | 1 | has 'token' => ( | |||||||||||
| 64 | is => 'ro', | ||||||||||||
| 65 | ); | ||||||||||||
| 66 | |||||||||||||
| 67 | # has 'on_message' # use the Mojo-dispatch feature here | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub build_request( $self, %parameters ) { | ||||||||||
|
Subroutine
build_request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 70 | 0 | ▶ | my $ua = $parameters{ ua } or die "No UA?!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | my $method = delete $parameters{ method }; | |||||||||||
| 72 | 0 | my $url = delete $parameters{ url }; | |||||||||||
| 73 | 0 | ▶ | my $headers = delete $parameters{ headers } || {}; | ||||||||||
|
Condition0%0/2
delete $parameters{'headers'} || {}
MC/DC0%0/1
delete $parameters{'headers'} || {}
delete $parameters{'headers'}
Truth table0%0/2
A
A= delete $parameters{'headers'}
| |||||||||||||
| 74 | 0 | my $data = delete $parameters{ data }; | |||||||||||
| 75 | 0 | my $res = $ua->build_tx( | |||||||||||
| 76 | $method => "$url", | ||||||||||||
| 77 | $headers, | ||||||||||||
| 78 | $data, | ||||||||||||
| 79 | ); | ||||||||||||
| 80 | #warn "Built request"; | ||||||||||||
| 81 | 0 | return $res; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | sub connect( $self ) { | ||||||||||
|
Subroutine
connect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 85 | # Fetch all the pent up rage | ||||||||||||
| 86 | #$self->get_messages(); | ||||||||||||
| 87 | 0 | $self->get_stream(); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub get_messages( $self ) { | ||||||||||
|
Subroutine
get_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 91 | 0 | my $tx = $self->build_get_messages(); | |||||||||||
| 92 | 0 | ▶ | $self->short_ua->start_p($tx)->then(sub($tx) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 93 | 0 | my $payload = $self->json->decode( $tx->result->body ); | |||||||||||
| 94 | 0 | for my $message (@$payload) { | |||||||||||
| 95 | 0 | my $m = API::Matterbridge::Message->new( $message ); | |||||||||||
| 96 | 0 | $self->emit('message', $m ); | |||||||||||
| 97 | }; | ||||||||||||
| 98 | 0 | }); | |||||||||||
| 99 | }; | ||||||||||||
| 100 | |||||||||||||
| 101 | 0 | ▶ | sub get_stream( $self ) { | ||||||||||
|
Subroutine
get_stream: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 102 | 0 | my $tx = $self->build_get_message_stream(); | |||||||||||
| 103 | |||||||||||||
| 104 | # Just in case we read half a JSON message | ||||||||||||
| 105 | 0 | state $buffer = ''; | |||||||||||
| 106 | |||||||||||||
| 107 | # Replace "read" events to disable default content parser | ||||||||||||
| 108 | 0 | ▶ | $tx->res->content->unsubscribe('read')->on(read => sub($content,$bytes) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 109 | 0 | $buffer .= $bytes; | |||||||||||
| 110 | |||||||||||||
| 111 | # Every (full) line should be a JSON stanza | ||||||||||||
| 112 | 0 | ▶ | while( $buffer =~ s!^(.*?)\n!! ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | my $m = API::Matterbridge::Message->from_bytes( $1 ); | |||||||||||
| 114 | 0 | $self->emit('message', $m ); | |||||||||||
| 115 | }; | ||||||||||||
| 116 | 0 | }); | |||||||||||
| 117 | # Process transaction | ||||||||||||
| 118 | 0 | $tx = $self->stream_ua->start_p($tx); | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | sub send( $self, @messages ) { | ||||||||||
|
Subroutine
send: not called CC=1 SCAR=4.1
Pod
undocumented
| |||||||||||||
| 122 | 0 | my $msg = shift @messages; | |||||||||||
| 123 | 0 | my $tx = $self->build_post_message(%$msg); | |||||||||||
| 124 | 0 | state @queue; | |||||||||||
| 125 | 0 | ▶ | my $message_sender = sub($tx) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 126 | 0 | my $next = shift @messages; | |||||||||||
| 127 | 0 | ▶ | if( $next ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 0 | push @queue, $self->short_ua->start_p($next)->then(__SUB__); | |||||||||||
| 129 | } else { | ||||||||||||
| 130 | 0 | @queue = (); | |||||||||||
| 131 | }; | ||||||||||||
| 132 | 0 | }; | |||||||||||
| 133 | |||||||||||||
| 134 | 0 | ▶ | push @queue, $self->short_ua->start_p($tx)->then($message_sender)->catch(sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||
| 135 | 0 | ▶ | use Data::Dumper; warn "Error: " . Dumper \@_; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||
| 136 | 0 | }); | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | 1; | |||||||||||
| 140 | |||||||||||||
| 141 | =head1 REPOSITORY | ||||||||||||
| 142 | |||||||||||||
| 143 | The public repository of this module is | ||||||||||||
| 144 | L<https://github.com/Corion/Mojolicious-Matterbridge>. | ||||||||||||
| 145 | |||||||||||||
| 146 | =head1 SUPPORT | ||||||||||||
| 147 | |||||||||||||
| 148 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 BUG TRACKER | ||||||||||||
| 151 | |||||||||||||
| 152 | Please report bugs in this module via the Github bug queue at | ||||||||||||
| 153 | L<https://github.com/Corion/Mojolicious-Matterbridge/issues> | ||||||||||||
| 154 | |||||||||||||
| 155 | =head1 AUTHOR | ||||||||||||
| 156 | |||||||||||||
| 157 | Max Maischein C<corion@cpan.org> | ||||||||||||
| 158 | |||||||||||||
| 159 | =head1 COPYRIGHT (c) | ||||||||||||
| 160 | |||||||||||||
| 161 | Copyright 2020 by Max Maischein C<corion@cpan.org>. | ||||||||||||
| 162 | |||||||||||||
| 163 | =head1 LICENSE | ||||||||||||
| 164 | |||||||||||||
| 165 | This module is released under the same terms as Perl itself. | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||