jsonrpc-client.pl untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 173 CC 11 SCAR 34.2
CC
11
Cov
0%
color
72.0
str_to_array
12.0
BEGIN
6.0
CRAP 30.5
1 #!/usr/bin/perl
2  
3 use Getopt::Long;
4 use strict;
5 use warnings;
6 use Dump::Krumo;
7 use Time::HiRes qw(time);
8 use JSON::RPC::Simple::Lite;
9  
10 # Default URL
11 my $api_url = "https://www.perturb.org/api/json-rpc/";
12  
13 ###################################################################
14  
15 my ($method,$curl);
16 my $params = "";
17 my $debug = 0;
18  
19 my $ok = GetOptions(
20 "debug+" => \$debug,
21 "url=s" => \$api_url,
22 "method=s" => \$method,
23 "params=s" => \$params,
24 "curl" => \$curl,
25 );
26  
27 my @params = ();
28  
29 # Raw called like echo_data(2,4,8)
30 if (!$method) {
31 if ($ARGV[0] && $ARGV[0] =~ /([\w.]+)\((.*?)\)/) {
32 $method = $1;
33 $params = $2;
34  
35 @params = str_to_array($params);
36 }
37 } else {
38 # Convert the params string to an array
39 @params = split(/,/, $params);
40 #@params = eval($params);
41 }
42  
43 if (!$method || !$api_url) {
44 die(usage());
45 }
46  
47 my $s = JSON::RPC::Simple::Lite->new($api_url,{debug => $debug});
48  
49 if (!@params) {
50 #@params = undef;
51 }
52  
53 ###################################################################
54  
55 if ($curl) {
56 my $curl = $s->curl_call($method,@params);
57 print $curl . "\n";
58 exit;
59 }
60  
61 if ($debug > 1) {
62 my $curl = $s->curl_call($method,@params);
63 print "curl : $curl\n";
64 }
65  
66 # Raw call method
67 my $i = $s->_call($method,@params);
68  
69 # Statically code the method name in your code (most readable), uses AUTOLOAD magic
70 # Note: requires params to be set to SOMETHING (even undef) for AUTOLOAD magic to work
71 #my $i = $s->peak->echo_data(@params);
72  
73 if (!defined($i)) {
74 my $status = $s->{response}->{status};
75 print "*** Error decoding JSON ***\nHTTP Code: $status - Response:\n";
76 print $s->{response}->{content} . "\n";
77  
78 exit(2);
79 }
80  
81 print color('white', "Response :\n");
82 out($i);
83  
84 ###################################################################
85  
86 sub usage {
87 return "$0 --method echo_data [--params \"2,4,6,eight\"] [--url http://domain.com/path/] [--debug]\n";
88 }
89  
90 # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
91 sub color {
92 my ($str, $txt) = @_;
93  
94 # If we're NOT connected to a an interactive terminal don't do color
95 if (-t STDOUT == 0) { return $txt // ""; }
96  
97 # No string sent in, so we just reset
98 if (!length($str) || $str eq 'reset') { return "\e[0m"; }
99  
100 # Some predefined colors
101 my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
102 $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
103  
104 # Get foreground/background and any commands
105 my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g;
106 my ($bc) = $str =~ /on_(\d{1,3})$/g;
107  
108 if (defined($fc) && int($fc) > 255) { $fc = undef; } # above 255 is invalid
109  
110 # Some predefined commands
111 my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
112 my $cmd_num = $cmd_map{$cmd // 0};
113  
114 my $ret = '';
115 if ($cmd_num) { $ret .= "\e[${cmd_num}m"; }
116 if (defined($fc)) { $ret .= "\e[38;5;${fc}m"; }
117 if (defined($bc)) { $ret .= "\e[48;5;${bc}m"; }
118 if (defined($txt)) { $ret .= $txt . "\e[0m"; }
119  
120 return $ret;
121 }
122  
123 # Use Data::Dump::Color if available, else Data::Dumper
124 BEGIN {
125 if(eval "require Data::Dump::Color; 1") {
126 #*out = \&Data::Dump::Color::dd;
127 *out = \&Dump::Krumo::kx;
128 } else {
129 require Data::Dumper;
130 *out = sub {
131 print Data::Dumper::Dumper(@_) . "\n"
132 };
133 }
134 }
135  
136 sub str_to_array {
137 my ($str, $sep) = @_;
138 my @ret;
139  
140 # Default separator
141 $sep ||= ",";
142  
143 # Split at the separator
144 #my @p = split(/$sep/, $str);
145 # Borrowed from: https://stackoverflow.com/questions/9855101/split-comma-separated-list-with-commas-embedded-in-quoted-arguments-in-perl
146 my @p = $str =~ m/(['"][^"']+['"]|[^,]+)(?:,\s*)?/g;
147  
148 #dd([\@p, scalar(@p)]); die;
149  
150 # Clean up each element
151 foreach my $x (@p) {
152 # Trim off trailing and leading whitespace
153 $x =~ s/^\s*//;
154 $x =~ s/\s*$//;
155  
156 # Remove surrounding quotes (convert to string)
157 if ($x =~ /^(['"](.*?)['"])$/) {
158 $x = $2;
159 # Convert numbers to an int/float
160 } elsif ($x =~ /^[\d-]/) {
161 $x += 0;
162 # If it's neither a quoted word or a number
163 } else {
164 print STDERR "Skipping $x because it's not formatted correctly. Try quoting it?\n";
165 exit();
166 $x = undef;
167 }
168  
169 push(@ret, $x);
170 }
171  
172 # Eval is another option, but has less control on invalid items
173 #$str = "($str)";
174 #my @z = eval($str);
175  
176 return @ret;
177 }