nopaste/Service.pm

filter: Statementstmt 19.1% 22 / 115 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 12.5% 4 / 32 Podpod 20.0% 1 / 5 totaltotal 13.5% 27 / 199 CC 22 SCAR 29.1
CC
22
Cov
14%
run
56.0
post_to_irc
56.0
_xclip_paste
20.0
CRAP 18.4
« nopaste/Service/mathbin.pm ↑ Summary nopaste/Service/pastie.pm »
1 package Mojolicious::Command::nopaste::Service;
2 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Util qw/decode monkey_patch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Getopt::Long qw(GetOptionsFromArray :config no_ignore_case); # no_auto_abbrev
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $USAGE = <<END;
9 USAGE:
10  
11 $0 command SERVICE [OPTIONS] [FILES]
12  
13 OPTIONS:
14 Note that not all options are relevant for all services.
15  
16 --channel, -c The channel for the service's pastebot or to post via Mojo::IRC
17 e.g. perl, #perl, irc://irc.perl.org:6667/perl
18 --copy, -x Copy the resulting URL to the clipboard (requires Clipboard.pm)
19 --description, -d Description or title of the nopaste
20 --name, -n Your name or nick, used for the pastebin and/or IRC
21 --language, -l Language for syntax highlighting, defaults to 'perl'
22 --open, -o Open a browser to the url (requires Browser::Open)
23 --paste, -p Read contents from clipboard (requires Clipboard.pm)
24 --private, -P Mark the paste as private (note: silently ignored if not relevant for service)
25 --token, -t A file containing an access token, or else the token string itself
26 --update, -u Update a paste of a given id
27  
28 END
29  
30 0 ▶has usage => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
31 0 my $self = shift;
32 0 my $usage = $USAGE;
33 0 ▶ if (my $add = $self->service_usage) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $add = $self->service_usage) 0 0 0
34 0 $usage .= "\n$add";
35 }
36 0 return $usage;
37 1 };
38  
39 1 has [qw/channel name desc service_usage token update/];
40 1 has [qw/copy open private irc_handled/] => 0;
41 0 ▶has clip => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
42 0 ▶ die "Clipboard module not available. Do you need to install it?\n"
Branch: 1 branch
BranchTrueFalseTotal
unless eval "use Clipboard; 1" 0 0 0
43 unless eval 'use Clipboard; 1';
44 0 monkey_patch 'Clipboard::Xclip',
45 copy => \&_xclip_copy,
46 paste => \&_xclip_paste;
47 0 return 'Clipboard';
48 1 };
49 0 ▶has files => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
50 1 has language => 'perl';
51 0 ▶has text => sub { shift->slurp };
Subroutine __ANON__: not called CC=1 SCAR=1.2
52 0 ▶has ua => sub { Mojo::UserAgent->new->max_redirects(10) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
53  
54 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
55 0 my ($self, @args) = @_;
56 GetOptionsFromArray( \@args,
57 0 ▶ 'channel|c=s' => sub { $self->channel($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 ▶ 'copy|x' => sub { $self->copy($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 ▶ 'description|d=s' => sub { $self->desc($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 ▶ 'name|n=s' => sub { $self->name($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 ▶ 'language|l=s' => sub { $self->language($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 ▶ 'open|o' => sub { $self->open($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 ▶ 'paste|p' => sub { $self->text($self->clip->paste) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ 'private|P' => sub { $self->private($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 ▶ 'token|t=s' => sub { $self->add_token($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ 'update|u=s' => sub { $self->update($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 );
68 0 $self->files(\@args);
69 0 ▶ my $url = $self->paste or return;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $self->paste or return 0 0 0
70 0 say $url;
71 0 ▶ $self->clip->copy($url) if $self->copy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->copy 0 0 0
72 0 ▶ if ($self->open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->open) 0 0 0
73 die "Browser::Open module not available. Do you need to install it?\n"
74 0 ▶ unless eval { require Browser::Open; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Browser::Open; 1 } } 0 0 0
75 0 Browser::Open::open_browser($url);
76 }
77 0 ▶ if ($self->channel and not $self->irc_handled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel and not $self->irc_handled) 0 0 0
Condition0%0/3
$self->channel and not $self->irc_handled
!ll&&!rl&&r
000
MC/DC0%0/2
$self->channel and not $self->irc_handled
$self->channel not $self->irc_handled
Truth table0%0/3
A and not(B)
A= $self->channelB= not $self->irc_handled
ABresult
0X0
100
111
78 0 $self->post_to_irc($url);
79 }
80 }
81  
82 0 ▶sub add_token {
Subroutine add_token: not called CC=2 SCAR=17.9
Pod undocumented
83 0 my ($self, $token) = @_;
84 0 ▶ if (-e $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $token) 0 0 0
85 0 $token = $self->slurp($token);
86 }
87 0 chomp $token;
88 0 $self->token($token);
89 }
90  
91 0 ▶sub paste { die 'Not implemented' }
Subroutine paste: not called CC=1 SCAR=6.9
Pod undocumented
92  
93 0 ▶sub slurp {
Subroutine slurp: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ($self, @files) = @_;
95 0 ▶ @files = @{ $self->files } unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
96  
97 0 my $content = do {
98 0 local $/;
99 0 local @ARGV = @files;
100 0 decode 'UTF-8', <>;
101 };
102  
103 # Remove trailing newline as some sites won't do it for us
104 0 chomp $content;
105 0 return $content;
106 }
107  
108 0 ▶sub post_to_irc {
Subroutine post_to_irc: not called CC=7 SCAR=40.3
Pod undocumented
109 0 my ($self, $paste) = @_;
110 die "This service requires Mojo::IRC to post to IRC, but it is not available. Do you need to install it?\n"
111 0 ▶ unless eval { require Mojo::IRC; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Mojo::IRC; 1 } } 0 0 0
112 0 require Mojo::IOLoop;
113 0 require Mojo::URL;
114  
115 0 my $url = Mojo::URL->new($self->channel);
116 0 ▶ my $chan = $url->fragment || $url->path->[-1];
Condition0%0/3
$url->fragment || $url->path->[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$url->fragment || $url->path->[-1]
$url->fragment $url->path->[-1]
Truth table0%0/3
A || B
A= $url->fragmentB= $url->path->[-1]
ABresult
1X1
011
000
117 0 ▶ die "Could not parse IRC channel\n" unless $chan;
Branch: 1 branch
BranchTrueFalseTotal
unless $chan 0 0 0
118 0 ▶ my $server = $url->host_port || 'irc.perl.org:6667';
Condition0%0/2
$url->host_port || 'irc.perl.org:6667'
l!l
00
MC/DC0%0/1
$url->host_port || 'irc.perl.org:6667'
$url->host_port
Truth table0%0/2
A
A= $url->host_port
Aresult
11
00
119 0 my $irc = Mojo::IRC->new(server => $server, nick => 'MojoNoPaste', user => 'MojoNoPaste');
120 0 $irc->register_default_event_handlers;
121  
122 0 ▶ my $name = $self->name || 'someone';
Condition0%0/2
$self->name || 'someone'
l!l
00
MC/DC0%0/1
$self->name || 'someone'
$self->name
Truth table0%0/2
A
A= $self->name
Aresult
11
00
123  
124 0 my $err;
125 0 ▶ my $catch = sub { $err = pop; Mojo::IOLoop->stop };
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 $irc->on(error => $catch);
127 0 $irc->on(irc_error => $catch);
128  
129 0 ▶ $irc->on(irc_join => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 my ($irc, $message) = @_;
131 0 my $chan = $message->{params}[0];
132 my $delay = Mojo::IOLoop->delay(
133 0 ▶ sub { $irc->write( privmsg => $chan, ":$name pasted $paste", shift->begin ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ sub { $irc->disconnect( shift->begin ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ sub { Mojo::IOLoop->stop },
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 );
137 0 $delay->catch($catch);
138 0 });
139  
140 0 ▶ $irc->connect(sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
141 0 my ($irc, $err) = @_;
142 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
143 0 say 'Connected to IRC';
144 0 $irc->write(join => "#$chan");
145 0 });
146  
147 0 Mojo::IOLoop->start;
148  
149 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
150 }
151  
152 0 ▶sub _xclip_copy {
Subroutine _xclip_copy: not called CC=2 SCAR=17.9
153 0 my ($self, $input) = @_;
154 0 eval { $self->copy_to_selection($_, $input) } for $self->all_selections();
155 }
156  
157 0 ▶sub _xclip_paste {
Subroutine _xclip_paste: not called CC=4 SCAR=30.0
158 0 my $self = shift;
159 0 my $data;
160 0 for my $sel ($self->all_selections) {
161 0 $data = eval { $self->paste_from_selection($sel) };
162 0 ▶ last if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
163 }
164 0 ▶ return decode 'UTF-8', $data || '';
Condition0%0/2
$data || ''
l!l
00
MC/DC0%0/1
$data || ''
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
165 }
166  
167 1 1;
168  
« nopaste/Service/mathbin.pm ↑ Summary nopaste/Service/pastie.pm »