blib/lib/WWW/GoDaddy/REST/Shell/QueryCommand.pm

filter: Statementstmt 19.2% 16 / 83 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 13.8% 21 / 152 CC 21 SCAR 47.0
CC
21
Cov
12%
comp_query
182.0
run_query
90.0
smry_query
2.0
CRAP 109.8
« blib/lib/WWW/GoDaddy/REST/Shell.pm ↑ Summary blib/lib/WWW/GoDaddy/REST/Shell/DocsCommand.pm »
1 package WWW::GoDaddy::REST::Shell::QueryCommand;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::MoreUtils qw( natatime );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 use Sub::Exporter -setup => {
9 exports => [qw(run_query smry_query help_query comp_query)],
10 groups => { default => [qw(run_query smry_query help_query comp_query)] }
11 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub run_query {
14 0 ▶ my ( $self, $schema_name, @args ) = @_;
Subroutine run_query: not called CC=9 SCAR=45.0
Pod undocumented
15  
16 0 my $usage = "Usage: query [schema] [[field] [modifier] [value]] ...\n";
17  
18 0 ▶ if ( !$schema_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_name) 0 0 0
19 0 warn($usage);
20 0 return 0;
21 }
22  
23 0 my $client = $self->client();
24 0 my $schema = $client->schema($schema_name);
25 0 ▶ if ( !$schema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 0 0
26 0 warn("'$schema_name' is not a recognized schema");
27 0 return 0;
28 }
29  
30 0 ▶ if ( !$schema->is_queryable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->is_queryable) 0 0 0
31 0 warn(
32 "This schema has no 'collection' link. It does not look like it can be queried. You can always try using a direct URL as a way around this if you know a URL exists to query this."
33 );
34 0 return 0;
35 }
36  
37 0 my @filters = grep { !/=/ } @args;
38 0 my %uri_params = map { split '=' } grep {/=/} @args;
39  
40 0 ▶ if ( @filters % 3 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@filters % 3 != 0) 0 0 0
41 0 warn($usage);
42 0 return 0;
43 }
44  
45 0 my $iterator = natatime 3, @filters;
46  
47 0 my $filter = {};
48 0 ▶ while ( my ( $field, $cmp, $value ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 0 0 0
49 0 ▶ $filter->{$field} ||= [];
Condition0%0/2
$filter->{$field} ||= []
l!l
00
MC/DC0%0/1
$filter->{$field} ||= []
$filter->{$field}
Truth table0%0/2
A
A= $filter->{$field}
Aresult
11
00
50 0 push @{ $filter->{$field} },
51 {
52 'modifier' => $cmp,
53 'value' => $value
54 };
55 }
56  
57 0 my $collection = eval { $client->query( $schema_name, $filter, \%uri_params ); };
58 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 0 ▶ if ( UNIVERSAL::isa( $@, 'WWW::GoDaddy::REST::Resource' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($@, 'WWW::GoDaddy::REST::Resource')) { } 0 0 0
60 0 $self->page( $@->to_string(1) . "\n" );
61 }
62 else {
63 0 carp($@);
64 }
65 0 return 0;
66 }
67  
68 0 $self->page( $collection->to_string(1) . "\n" );
69 0 return 1;
70 }
71  
72 sub smry_query {
73 0 ▶ return "search for items in a schema"
Subroutine smry_query: not called CC=1 SCAR=6.9
Pod undocumented
74 }
75  
76 sub help_query {
77 return <<HELP
78 Search for items in a collection of a given schema.
79  
80 Usage:
81 query [schema] [[field] [modifier] [value]] ... [[arbitrary=param] [arbitrary=param]] ...
82  
83 Example:
84 query user fname eq john
85 query pancakes limit=10
86 HELP
87 0 ▶}
Subroutine help_query: not called CC=1 SCAR=6.9
Pod undocumented
88  
89 sub comp_query {
90 0 ▶ my ( $self, $word, $line, $start ) = @_;
Subroutine comp_query: not called CC=13 SCAR=52.0
Pod undocumented
91  
92 0 my @words = $self->line_parsed($line);
93 0 my $client = $self->client();
94  
95 0 ▶ my $comp_schema = ( @words < 2 or ( @words == 2 and $start < length($line) ) );
Condition0%0/6
@words < 2 || @words == 2 && $start < length $line
l!l&&r!l&&!r
000
@words == 2 and $start < length $line
!ll&&!rl&&r
000
MC/DC0%0/3
@words < 2 || @words == 2 && $start < length $line
@words < 2 @words == 2 $start < length $line
Truth table0%0/4
A || B and C
A= @words < 2B= @words == 2C= $start < length $line
ABCresult
1XX1
0111
00X0
0100
96 0 ▶ if ($comp_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_schema) 0 0 0
97 0 return grep { index( $_, $word ) == 0 }
98 0 grep { $client->schema($_)->is_queryable } $self->schema_names();
99 }
100  
101 0 my $schema_name = $words[1];
102 0 my $schema = $client->schema($schema_name);
103 0 ▶ if ( !$schema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 0 0
104  
105 # bad schema name - bail
106 0 return ();
107 }
108  
109 0 my %filters = %{ $schema->f('collectionFilters') };
110  
111 0 ▶ my $comp_field = ( ( @words + 1 ) % 3 == 0 or ( @words % 3 == 0 and $start < length($line) ) );
Condition0%0/6
(@words + 1) % 3 == 0 || @words % 3 == 0 && $start < length $line
l!l&&r!l&&!r
000
@words % 3 == 0 and $start < length $line
!ll&&!rl&&r
000
MC/DC0%0/3
(@words + 1) % 3 == 0 || @words % 3 == 0 && $start < length $line
(@words + 1) % 3 == 0 @words % 3 == 0 $start < length $line
Truth table0%0/4
A || B and C
A= (@words + 1) % 3 == 0B= @words % 3 == 0C= $start < length $line
ABCresult
1XX1
0111
00X0
0100
112 0 ▶ if ($comp_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_field) 0 0 0
113 0 my @fields = sort keys %filters;
114 0 return grep { index( $_, $word ) == 0 } @fields;
115 }
116  
117 0 ▶ my $comp_modifier
Condition0%0/6
(@words + 1) % 3 == 1 || @words % 3 == 1 && $start < length $line
l!l&&r!l&&!r
000
@words % 3 == 1 and $start < length $line
!ll&&!rl&&r
000
MC/DC0%0/3
(@words + 1) % 3 == 1 || @words % 3 == 1 && $start < length $line
(@words + 1) % 3 == 1 @words % 3 == 1 $start < length $line
Truth table0%0/4
A || B and C
A= (@words + 1) % 3 == 1B= @words % 3 == 1C= $start < length $line
ABCresult
1XX1
0111
00X0
0100
118 = ( ( @words + 1 ) % 3 == 1 or ( @words % 3 == 1 and $start < length($line) ) );
119 0 ▶ if ($comp_modifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_modifier) 0 0 0
120 0 ▶ my $in_field_named = ( @words % 3 == 1 ) ? $words[-2] : $words[-1];
Branch: 1 branch
BranchTrueFalseTotal
@words % 3 == 1 ? : 0 0 0
121 0 ▶ if ( !$filters{$in_field_named} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filters{$in_field_named}) 0 0 0
122  
123 # bad field name - bail
124 0 return ();
125 }
126 0 my @modifiers = sort @{ $filters{$in_field_named}->{modifiers} };
127 0 return ( grep { index( $_, $word ) == 0 } @modifiers );
128 }
129  
130 0 return ();
131  
132 }
133  
134 1 1;
135  
136 =head1 AUTHOR
137  
138 David Bartle, C<< <davidb@mediatemple.net> >>
139  
140 =head1 COPYRIGHT & LICENSE
141  
142 Copyright (c) 2014 Go Daddy Operating Company, LLC
143  
144 Permission is hereby granted, free of charge, to any person obtaining a
145 copy of this software and associated documentation files (the "Software"),
146 to deal in the Software without restriction, including without limitation
147 the rights to use, copy, modify, merge, publish, distribute, sublicense,
148 and/or sell copies of the Software, and to permit persons to whom the
149 Software is furnished to do so, subject to the following conditions:
150  
151 The above copyright notice and this permission notice shall be included in
152 all copies or substantial portions of the Software.
153  
154 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
156 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
157 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
158 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
159 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
160 DEALINGS IN THE SOFTWARE.
161  
162 =cut
« blib/lib/WWW/GoDaddy/REST/Shell.pm ↑ Summary blib/lib/WWW/GoDaddy/REST/Shell/DocsCommand.pm »