blib/lib/Mojolicious/Command/replget.pm

filter: Statementstmt 35.3% 29 / 82 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 40.0% 8 / 20 Podpod 100% 1 / 1 totaltotal 19.4% 38 / 195 CC 32 SCAR 43.0
CC
32
Cov
18%
run
182.0
_select
110.0
_json
30.0
CRAP 73.8
↑ Summary
1 package Mojolicious::Command::replget;
2 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.01';
5 1 $VERSION = eval $VERSION;
6  
7 1 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::JSON qw(encode_json j);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mojo::Util qw(decode encode getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 has description => 'Perform HTTP requests in a REPL';
16 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 0 ▶sub run {
Subroutine run: not called CC=13 SCAR=52.0
Pod documented
19 0 my ($self) = @_;
20  
21 0 my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
22 0 while ( 1 ) {
23 0 print "\nreplget> ";
24 0 chomp(my $stdin = <STDIN>);
25 0 my @args = split / +/, $stdin;
26 getopt \@args,
27 'C|charset=s' => \my $charset,
28 'c|content=s' => \(my $content = ''),
29 'H|header=s' => \my @headers,
30 0 ▶ 'i|inactivity-timeout=i' => sub { $ua->inactivity_timeout($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 'M|method=s' => \(my $method = 'GET'),
32 0 ▶ 'o|connect-timeout=i' => sub { $ua->connect_timeout($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 'r|redirect' => \my $redirect,
34 0 ▶ 'S|response-size=i' => sub { $ua->max_response_size($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 'v|verbose' => \my $verbose;
36  
37 0 @args = map { decode 'UTF-8', $_ } @args;
38 0 ▶ $self->usage and next unless my $url = shift @args;
Branch: 1 branch
BranchTrueFalseTotal
unless my $url = shift @args 0 0 0
Condition0%0/5
(my $url = shift @args) || $self->usage && (next)
l!l&&r!l&&!r
000
$self->usage and next
!ll
00
MC/DC0%0/2
(my $url = shift @args) || $self->usage && (next)
(my $url = shift @args) $self->usage
Truth table0%0/3
A || B
A= (my $url = shift @args)B= $self->usage
ABresult
1X1
011
000
39 0 my $selector = shift @args;
40  
41 # Parse header pairs
42 0 ▶ my %headers = map { /^\s*([^:]+)\s*:\s*(.*+)$/ ? ($1, $2) : () } @headers;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*([^:]+)\s*:\s*(.*+)$/u ? : 0 0 0
43  
44 # Detect proxy for absolute URLs
45 0 ▶ $url !~ m!^/! ? $ua->proxy->detect : $ua->server->app($self->app);
Branch: 1 branch
BranchTrueFalseTotal
!($url =~ m[^/]u) ? : 0 0 0
46 0 ▶ $ua->max_redirects(10) if $redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirect 0 0 0
47  
48 0 my $buffer = '';
49 $ua->on(
50 0 ▶ start => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my ($ua, $tx) = @_;
52  
53 # Verbose
54 0 weaken $tx;
55 $tx->res->content->on(
56 0 ▶ body => sub { warn _header($tx->req), _header($tx->res) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 ▶ if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
58  
59 # Stream content (ignore redirects)
60 $tx->res->content->unsubscribe('read')->on(
61 0 ▶ read => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
62 0 ▶ return if $redirect && $tx->res->is_redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirect and $tx->res->is_redirect 0 0 0
Condition0%0/3
$redirect and $tx->res->is_redirect
!ll&&!rl&&r
000
MC/DC0%0/2
$redirect and $tx->res->is_redirect
$redirect $tx->res->is_redirect
Truth table0%0/3
A and B
A= $redirectB= $tx->res->is_redirect
ABresult
0X0
100
111
63 0 ▶ defined $selector ? ($buffer .= pop) : print pop;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 0 0 0
64 }
65 0 );
66 }
67 0 );
68  
69 # Switch to verbose for HEAD requests
70 0 ▶ $verbose = 1 if $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 0 0 0
71 0 STDOUT->autoflush(1);
72 0 my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));
73 0 my $res = $tx->result;
74  
75 # JSON Pointer
76 0 ▶ next unless defined $selector;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selector 0 0 0
77 0 ▶ _json($buffer, $selector) and next if !length $selector || $selector =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if not length $selector or $selector =~ m[^/]u 0 0 0
Condition0%0/8
(!length($selector) || $selector =~ m[^/]u) && (_json($buffer, $selector) && (next))
!ll&&!rl&&r
000
not length $selector or $selector =~ m[^/]u
l!l&&r!l&&!r
000
_json($buffer, $selector) and next
!ll
00
MC/DC0%0/3
(!length($selector) || $selector =~ m[^/]u) && (_json($buffer, $selector) && (next))
not length $selector $selector =~ m[^/]u _json($buffer, $selector)
Truth table0%0/5
not(A) or B && C
A= not length $selectorB= $selector =~ m[^/]uC= _json($buffer, $selector)
ABCresult
00X0
1X00
0100
1X11
0111
78  
79 # Selector
80 0 ▶ $charset //= $res->content->charset || $res->default_charset;
Condition0%0/6
$charset //= $res->content->charset || $res->default_charset
l!l&&r!l&&!r
000
$res->content->charset || $res->default_charset
l!l&&r!l&&!r
000
MC/DC0%0/3
$charset //= $res->content->charset || $res->default_charset
$charset $res->content->charset $res->default_charset
Truth table0%0/4
A //= B || C
A= $charsetB= $res->content->charsetC= $res->default_charset
ABCresult
1XX1
01X1
0011
0000
81 0 _select($buffer, $selector, $charset, @args);
82 }
83 }
84  
85 0 ▶sub _header { $_[0]->build_start_line, $_[0]->headers->to_string, "\n\n" }
Subroutine _header: not called CC=1 SCAR=6.9
86  
87 0 ▶sub _json {
Subroutine _json: not called CC=5 SCAR=34.0
88 0 ▶ return unless my $data = j(shift);
Branch: 1 branch
BranchTrueFalseTotal
unless my $data = j(shift()) 0 0 0
89 0 ▶ return unless defined($data = Mojo::JSON::Pointer->new($data)->get(shift));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($data = "Mojo::JSON::Pointer"->new($data)->get(shift())) 0 0 0
90 0 ▶ return _say($data) unless ref $data eq 'HASH' || ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" or ref $data eq "ARRAY" 0 0 0
Condition0%0/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table0%0/3
A or B
A= ref $data eq "HASH"B= ref $data eq "ARRAY"
ABresult
1X1
011
000
91 0 say encode_json($data);
92 }
93  
94 0 ▶sub _say { length && say encode('UTF-8', $_) for @_ }
Subroutine _say: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
length $_ and say encode("UTF-8", $_) 0 0 0
95  
96 0 ▶sub _select {
Subroutine _select: not called CC=10 SCAR=47.0
97 0 my ($buffer, $selector, $charset, @args) = @_;
98  
99 # Keep a strong reference to the root
100 0 ▶ $buffer = decode($charset, $buffer) // $buffer if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 0 0 0
Condition0%0/3
decode($charset, $buffer) // $buffer
l!l&&r!l&&!r
000
MC/DC0%0/2
decode($charset, $buffer) // $buffer
decode($charset, $buffer) $buffer
Truth table0%0/3
A // B
A= decode($charset, $buffer)B= $buffer
ABresult
1X1
011
000
101 0 my $dom = Mojo::DOM->new($buffer);
102 0 my $results = $dom->find($selector);
103  
104 0 ▶ while (defined(my $command = shift @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $command = shift @args)) 0 0 0
105  
106 # Number
107 0 ▶ ($results = $results->slice($command)) and next if $command =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^\d+$/u 0 0 0
Condition0%0/5
$command =~ /^\d+$/u && (($results = $results->slice($command)) && (next))
!ll&&!rl&&r
000
$results = $results->slice($command) and next
!ll
00
MC/DC0%0/2
$command =~ /^\d+$/u && (($results = $results->slice($command)) && (next))
$command =~ /^\d+$/u $results = $results->slice($command)
Truth table0%0/3
A && B
A= $command =~ /^\d+$/uB= $results = $results->slice($command)
ABresult
0X0
100
111
108  
109 # Text
110 0 ▶ return _say($results->map('text')->each) if $command eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "text" 0 0 0
111  
112 # All text
113 0 ▶ return _say($results->map('all_text')->each) if $command eq 'all';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "all" 0 0 0
114  
115 # Attribute
116 0 ▶ return _say($results->map(attr => $args[0] // '')->each)
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "attr" 0 0 0
Condition0%0/2
$args[0] // ""
l!l
00
MC/DC0%0/1
$args[0] // ""
$args[0]
Truth table0%0/2
A
A= $args[0]
Aresult
11
00
117 if $command eq 'attr';
118  
119 # Unknown
120 0 die qq{Unknown command "$command".\n};
121 }
122  
123 0 _say($results->each);
124 }
125  
126 1 1;
127  
128 =encoding utf8
129  
130 =head1 NAME
131  
132 Mojolicious::Command::replget - Get command in a REPL
133  
134 =head1 SYNOPSIS
135  
136 Usage: APPLICATION replget
137
138 replget> [OPTIONS] URL [SELECTOR|JSON-POINTER] [COMMANDS]
139  
140 ./myapp.pl replget
141
142 replget> /
143 replget -H 'Accept: text/html' /hello.html 'head > title' text
144 replget //sri:secr3t@/secrets.json /1/content
145 replget mojolicious.org
146 replget -v -r -o 25 -i 50 google.com
147 replget -v -H 'Host: mojolicious.org' -H 'Accept: */*' mojolicious.org
148 replget -M POST -H 'Content-Type: text/trololo' -c 'trololo' perl.org
149 replget mojolicious.org 'head > title' text
150 replget mojolicious.org .footer all
151 replget mojolicious.org a attr href
152 replget mojolicious.org '*' attr id
153 replget mojolicious.org 'h1, h2, h3' 3 text
154 replget https://api.metacpan.org/v0/author/SRI /name
155 replget -H 'Host: example.com' http+unix://%2Ftmp%2Fmyapp.sock/index.html
156  
157 Options:
158 -C, --charset <charset> Charset of HTML/XML content, defaults
159 to auto-detection
160 -c, --content <content> Content to send with request
161 -H, --header <name:value> Additional HTTP header
162 -h, --help Show this summary of available options
163 --home <path> Path to home directory of your
164 application, defaults to the value of
165 MOJO_HOME or auto-detection
166 -i, --inactivity-timeout <seconds> Inactivity timeout, defaults to the
167 value of MOJO_INACTIVITY_TIMEOUT or 20
168 -M, --method <method> HTTP method to use, defaults to "GET"
169 -m, --mode <name> Operating mode for your application,
170 defaults to the value of
171 MOJO_MODE/PLACK_ENV or "development"
172 -o, --connect-timeout <seconds> Connect timeout, defaults to the value
173 of MOJO_CONNECT_TIMEOUT or 10
174 -r, --redirect Follow up to 10 redirects
175 -S, --response-size <size> Maximum response size in bytes,
176 defaults to 2147483648 (2GB)
177 -v, --verbose Print request and response headers to
178 STDERR
179  
180 =head1 DESCRIPTION
181  
182 L<Mojolicious::Command::replget> is a command line interface for
183 L<Mojo::UserAgent> in a REPL.
184  
185 =head1 ATTRIBUTES
186  
187 L<Mojolicious::Command::replget> performs requests to remote hosts or local
188 applications.
189  
190 =head2 description
191  
192 my $description = $replget->description;
193 $replget = $replget->description('Foo');
194  
195 Short description of this command, used for the command list.
196  
197 =head2 usage
198  
199 my $usage = $replget->usage;
200 $replget = $replget->usage('Foo');
201  
202 Usage information for this command, used for the help screen.
203  
204 =head1 METHODS
205  
206 L<Mojolicious::Command::replget> inherits all methods from L<Mojolicious::Command>
207 and implements the following new ones.
208  
209 =head2 run
210  
211 $get->run(@ARGV);
212  
213 Run this command.
214  
215 =head1 SEE ALSO
216  
217 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
218  
219 =cut
↑ Summary