blib/lib/App/Filite/Client.pm

filter: Statementstmt 78.1% 107 / 137 Branchbran 61.1% 22 / 36 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 82.7% 24 / 29 Podpod 100% 6 / 6 totaltotal 71.9% 164 / 228 CC 26 SCAR 17.5
CC
26
Cov
72%
execute
30.0
_guess_mode
8.0
share_text
4.4
CRAP 5.8
↑ Summary
1 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package App::Filite::Client;
6  
7 2 our $AUTHORITY = 'cpan:TOBYINK';
8 2 our $VERSION = '0.001001';
9  
10 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::XDG;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use HTTP::Tiny::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON::PP qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Class::Tiny {
Subroutine BEGIN: called CC=1 SCAR=0.2
20 0 ▶ password => sub { croak "Missing option: password" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 ▶ server => sub { croak "Missing option: server" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 1 ▶ useragent => sub { shift->_build_useragent },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 ▶ errors => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 };
25  
26 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶sub new_from_config {
Subroutine new_from_config: called CC=4 SCAR=14.5
Pod documented
29 1 my ( $class ) = ( shift );
30
31 1 state $xdg = File::XDG->new( name => 'filite-client', api => 1 );
32 1 ▶ my $config_file = $ENV{FILITE_CLIENT_CONFIG} // $xdg->config_home->child( 'config.json' );
Condition33%1/3
$ENV{"FILITE_CLIENT_CONFIG"} // $xdg->config_home->child("config.json")
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{"FILITE_CLIENT_CONFIG"} // $xdg->config_home->child("config.json")
$ENV{"FILITE_CLIENT_CONFIG"} $xdg->config_home->child("config.json")
Truth table33%1/3
A // B
A= $ENV{"FILITE_CLIENT_CONFIG"}B= $xdg->config_home->child("config.json")
ABresult
1X1
011
000
33 1 ▶ if ( not ref $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config_file) 1 0 1
34 1 $config_file = path( $config_file );
35 }
36 1 ▶ croak "Expected config file: $config_file" unless $config_file->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_file->is_file 0 1 1
37
38 1 my $args = decode_json( $config_file->slurp_utf8 );
39 1 my $self = $class->new( %$args );
40 1 return $self;
41 }
42  
43 1 ▶sub _build_useragent {
Subroutine _build_useragent: called CC=1 SCAR=0.0
44 1 my ( $self ) = ( shift );
45 1 my $auth = encode_base64( sprintf( 'admin:%s', $self->password ) );
46 1 chomp $auth;
47 1 return HTTP::Tiny->new(
48 agent => sprintf( '%s/%s ', __PACKAGE__, $VERSION ),
49 default_headers => { 'Authorization' => "Basic $auth" },
50 );
51 }
52  
53 0 ▶sub _parse_opts {
Subroutine _parse_opts: not called CC=1 SCAR=6.9
54 0 my ( $self, $args ) = ( shift, @_ );
55
56 0 my $opts = {};
57 0 GetOptionsFromArray(
58 $args => $opts,
59 'text|T',
60 'file|F',
61 'link|L',
62 'highlight|H',
63 'help|usage',
64 );
65 0 return $opts;
66 }
67  
68 ## no Test::Tabs
69 0 ▶sub _print_usage {
Subroutine _print_usage: not called CC=1 SCAR=6.9
70 0 print <<"STDERR"; return 0;
71 filite-client: share via a filite server
72  
73 Usage:
74 filite-client -T [filename]
75 filite-client -F [filename]
76 filite-client -L [url]
77 cat blah | filite-client [options]
78  
79 Options:
80 --text, -T Share as text
81 --file, -F Share as file
82 --link, -L Share as link
83 --highlight, -H Syntax highligh text
84 --help, --usage Show this usage information
85  
86 STDERR
87 }
88 ## use Test::Tabs
89  
90 0 ▶sub execute {
Subroutine execute: not called CC=5 SCAR=34.0
Pod documented
91 0 my ( $self, $args ) = ( shift, @_ );
92 0 ▶ $args //= [ @ARGV ];
Condition0%0/2
$args //= [@ARGV]
l!l
00
MC/DC0%0/1
$args //= [@ARGV]
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
93 0 my $opts = $self->_parse_opts( $args );
94 0 ▶ $args = [ '-' ] unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 0 0
95
96 0 ▶ if ( $opts->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"help"}) 0 0 0
97 0 return $self->_print_usage;
98 }
99
100 0 for my $file ( @$args ) {
101 0 my $url = $self->share( $file, $opts );
102 0 print "$url\n";
103 }
104
105 0 $self->errors;
106 }
107  
108 7 ▶sub _guess_mode {
Subroutine _guess_mode: called CC=8 SCAR=20.8
109 7 my ( $self, $file, $opts ) = ( shift, @_ );
110 7 ▶ return 'link' if $opts->{link};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"link"} 1 6 7
111 6 ▶ return 'text' if $opts->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"text"} 1 5 6
112 5 ▶ return 'file' if $opts->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"file"} 1 4 5
113 4 ▶ return 'link' if $file =~ m{\Ahttps?://\S+\z}is;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[\Ahttps?://\S+\z]is 1 3 4
114 3 ▶ return 'text' if $opts->{highlight};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"highlight"} 1 2 3
115 2 ▶ return 'text' if $file eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "-" 0 2 2
116 2 ▶ return 'file' if -B $file;
Branch: 1 branch
BranchTrueFalseTotal
if -B $file 1 1 2
117 1 return 'text';
118 }
119  
120 7 ▶sub share {
Subroutine share: called CC=2 SCAR=7.0
Pod documented
121 7 my ( $self, $file, $opts ) = ( shift, @_ );
122 7 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
70
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
123 7 my $mode = $self->_guess_mode( $file, $opts );
124 7 my $method = "share_$mode";
125 7 return $self->$method( $file, $opts );
126 }
127  
128 3 ▶sub _get_endpoint {
Subroutine _get_endpoint: called CC=3 SCAR=11.3
129 3 my ( $self, $mode ) = ( shift, @_ );
130 3 my $server = $self->server;
131 3 ▶ $server = "http://$server" unless $server =~ m{https?:}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $server =~ /https?:/i 3 0 3
132 3 ▶ $server .= '/' unless $server =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $server =~ m[/$] 3 0 3
133 3 return sprintf( '%s%s', $server, lc( substr( $mode, 0, 1 ) ) );
134 }
135  
136 3 ▶sub _handle_response {
Subroutine _handle_response: called CC=2 SCAR=10.5
137 3 my ( $self, $response ) = ( shift, @_ );
138 3 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 3 0 3
139 3 return $response->{content};
140 }
141 0 my $errs = $self->errors;
142 0 ++$errs;
143 0 $self->errors( $errs );
144 0 warn sprintf( "ERROR: %s %s\n", $response->{status}, $response->{reason} );
145 0 return "-";
146 }
147  
148 1 ▶sub share_file {
Subroutine share_file: called CC=3 SCAR=11.7
Pod documented
149 1 my ( $self, $file, $opts ) = ( shift, @_ );
150 1 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
10
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
151
152 1 my ( $filename, $content );
153 1 ▶ if ( $file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 1 1
154 0 $filename = 'file.data';
155 0 local $/;
156 0 $content = <STDIN>;
157 }
158 else {
159 1 my $pt = path( $file );
160 1 $filename = $pt->basename;
161 1 $content = $pt->slurp;
162 }
163
164 1 my $endpoint = $self->_get_endpoint( 'file' );
165 1 my $response = $self->useragent->post_multipart(
166 $endpoint => {
167 file => {
168 filename => $filename,
169 content => $content,
170 content_type => 'application/octet-stream',
171 },
172 },
173 );
174
175 1 return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
176 }
177  
178 1 ▶sub share_text {
Subroutine share_text: called CC=4 SCAR=14.8
Pod documented
179 1 my ( $self, $file, $opts ) = ( shift, @_ );
180 1 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
10
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
181
182 1 my $content;
183 1 ▶ if ( $file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 1 1
184 0 local $/;
185 0 $content = <STDIN>;
186 }
187 else {
188 1 $content = path( $file )->slurp;
189 }
190
191 my $json = encode_json( {
192 contents => $content,
193 1 ▶ highlight => $opts->{highlight} ? \1 : \0,
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'highlight'} ? : 1 0 1
194 } );
195
196 1 my $endpoint = $self->_get_endpoint( 'text' );
197 1 my $response = $self->useragent->post(
198 $endpoint => {
199 content => $json,
200 headers => { 'Content-Type' => 'application/json' },
201 },
202 );
203
204 1 return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
205 }
206  
207 1 ▶sub share_link {
Subroutine share_link: called CC=3 SCAR=11.4
Pod documented
208 1 my ( $self, $file, $opts ) = ( shift, @_ );
209 1 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
10
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
210
211 1 my $forward;
212 1 ▶ if ( $file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 1 1
213 0 local $/;
214 0 $forward = <>;
215 }
216 else {
217 1 $forward = $file;
218 }
219
220 1 chomp $forward;
221
222 1 my $json = encode_json( {
223 forward => $forward,
224 } );
225
226 1 my $endpoint = $self->_get_endpoint( 'link' );
227 1 my $response = $self->useragent->post(
228 $endpoint => {
229 content => $json,
230 headers => { 'Content-Type' => 'application/json' },
231 },
232 );
233
234 1 return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
235 }
236  
237 2 1;
238  
239 __END__
↑ Summary