REST/Schema.pm

filter: Statementstmt 72.4% 84 / 116 Branchbran 51.8% 28 / 54 Conditioncond 46.1% 12 / 26 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 87.5% 14 / 16 Podpod 25.0% 3 / 12 totaltotal 60.2% 144 / 239 CC 39 SCAR 34.7
CC
39
Cov
63%
resource_field_type
88.0
is_queryable
12.0
registry_lookup
7.1
CRAP 32.3
« REST/Shell/DocsCommand.pm ↑ Summary REST/Shell/Util.pm »
1 package WWW::GoDaddy::REST::Schema;
2  
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use WWW::GoDaddy::REST::Util qw( abs_url build_complex_query_url );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 extends 'WWW::GoDaddy::REST::Resource';
7  
8 0 ▶sub resource_field_names {
Subroutine resource_field_names: not called CC=1 SCAR=6.9
Pod undocumented
9 0 my $self = shift;
10 0 my %res_fields = %{ $self->f('resourceFields') };
11 0 return keys %res_fields;
12 }
13  
14 3 ▶sub resource_field {
Subroutine resource_field: called CC=2 SCAR=7.2
Pod undocumented
15 3 my $self = shift;
16 3 my $name = shift;
17 3 my $res_fields = $self->f('resourceFields');
18 3 ▶ if ( !exists $res_fields->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $res_fields->{$name}) 0 3 3
19 0 return undef;
20 }
21 3 return $res_fields->{$name};
22 }
23  
24 3 ▶sub resource_field_type {
Subroutine resource_field_type: called CC=18 SCAR=44.8
Pod undocumented
25 3 my $self = shift;
26 3 my $name = shift;
27 3 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
03
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
28  
29 3 ▶ my $type = $self->resource_field($name)->{'type'} || 'string';
Condition50%1/2
$self->resource_field($name)->{'type'} || 'string'
l!l
30
MC/DC0%0/1
$self->resource_field($name)->{'type'} || 'string'
$self->resource_field($name)->{'type'}
Truth table50%1/2
A
A= $self->resource_field($name)->{'type'}
Aresult
11
00
30 3 my $want_array = wantarray;
31 3 my $auto_upconvert_reference = $opts->{auto_upconvert_reference};
32 3 my $qualify_schema_types = $opts->{qualify_schema_types};
33  
34 3 ▶ if ( $want_array or $auto_upconvert_reference or $qualify_schema_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_array or $auto_upconvert_reference or $qualify_schema_types) 3 0 3
Condition33%2/6
$want_array or $auto_upconvert_reference or $qualify_schema_types
l!l&&r!l&&!r
300
$want_array or $auto_upconvert_reference
l!l&&r!l&&!r
300
MC/DC0%0/3
$want_array or $auto_upconvert_reference or $qualify_schema_types
$want_array $auto_upconvert_reference $qualify_schema_types
Truth table25%1/4
A or B or C
A= $want_arrayB= $auto_upconvert_referenceC= $qualify_schema_types
ABCresult
1XX1
01X1
0011
0000
35 3 my ( $a_type, $b_type ) = split( /[\[\]]/, $type );
36  
37 3 ▶ my $compound_type = $b_type ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b_type ? : 1 2 3
38  
39 3 ▶ if ($auto_upconvert_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_upconvert_reference) 0 3 3
40  
41 # some schemas don't properly use the 'reference' type to indicate
42 # the complex relationship. Convention seems to favor two sorts
43 # of field names that link to complex types:
44 # mySchemaId
45 # my_schema_id
46 0 ▶ if ( !$compound_type && $name =~ /^(.*)(_id|Id)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $compound_type and $name =~ /^(.*)(_id|Id)$/) 0 0 0
Condition0%0/3
not $compound_type and $name =~ /^(.*)(_id|Id)$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $compound_type and $name =~ /^(.*)(_id|Id)$/
not $compound_type $name =~ /^(.*)(_id|Id)$/
Truth table0%0/3
not(A) and B
A= not $compound_typeB= $name =~ /^(.*)(_id|Id)$/
ABresult
0X0
100
111
47 0 my $possible_schema = $self->client->schema($1);
48 0 ▶ if ($possible_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_schema) 0 0 0
49 0 $compound_type = 1;
50 0 $a_type = 'reference';
51 0 $b_type = $possible_schema->id;
52 }
53 }
54 }
55  
56 3 ▶ if ($qualify_schema_types) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qualify_schema_types) 0 3 3
57 0 ▶ if ($compound_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compound_type) { } 0 0 0
58 0 my $possible_schema = $self->client->schema($b_type);
59 0 ▶ if ($possible_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_schema) 0 0 0
60 0 $b_type = $possible_schema->link('self');
61 }
62 }
63 else {
64 0 my $possible_schema = $self->client->schema($a_type);
65 0 ▶ if ($possible_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_schema) 0 0 0
66 0 $a_type = $possible_schema->link('self');
67 }
68 }
69 }
70  
71 3 ▶ if ($want_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_array) { } 3 0 3
72 3 ▶ if ($compound_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compound_type) { } 1 2 3
73 1 return ( $a_type, $b_type );
74 }
75 else {
76 2 return ( undef, $a_type );
77 }
78 }
79 else {
80 0 ▶ if ($compound_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compound_type) { } 0 0 0
81 0 $type = sprintf( '%s[%s]', $a_type, $b_type );
82 }
83 else {
84 0 $type = $a_type;
85 }
86 }
87 }
88 0 return $type;
89 }
90  
91 8 ▶sub query {
Subroutine query: called CC=2 SCAR=6.9
Pod documented
92 8 my $self = shift;
93  
94 8 ▶ if ( !ref( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0]) { } 5 3 8
95 5 return $self->query_by_id(@_);
96 }
97 else {
98 3 return $self->query_complex(@_);
99 }
100  
101 }
102  
103 3 ▶sub query_complex {
Subroutine query_complex: called CC=2 SCAR=6.9
Pod documented
104 3 my $self = shift;
105  
106 3 my ( $query_params, $http_opts ) = _separate_query_and_http_params(@_);
107  
108 3 my $url = build_complex_query_url( $self->query_url, @{$query_params} );
109  
110 3 my $resource = $self->client->http_request_as_resource( 'GET', $url, undef, $http_opts );
111  
112 3 ▶ return wantarray ? $resource->items() : $resource;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
113 }
114  
115 20 ▶sub query_by_id {
Subroutine query_by_id: called CC=3 SCAR=11.0
Pod documented
116 20 my $self = shift;
117 20 my $id = shift;
118 20 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
713
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
119 20 ▶ my $http_opts = shift || {};
Condition100%2/2
shift() || {}
l!l
614
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
120  
121 20 my $client = $self->client;
122 20 my $url = build_complex_query_url( $self->query_url($id), $opts );
123  
124 20 return $client->http_request_as_resource( 'GET', $url, undef, $http_opts );
125 }
126  
127 3 ▶sub _separate_query_and_http_params {
Subroutine _separate_query_and_http_params: called CC=4 SCAR=16.3
128 3 my @all = @_;
129  
130 3 ▶ if ( !@all ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@all) 0 3 3
131 0 return ();
132 }
133  
134 3 my ( $query_opts, $http_opts );
135  
136 3 ▶ if ( @all && _looks_like_http_options( $all[-1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@all and _looks_like_http_options($all[-1])) { } 0 3 3
Condition33%1/3
@all and _looks_like_http_options($all[-1])
!ll&&!rl&&r
030
MC/DC0%0/2
@all and _looks_like_http_options($all[-1])
@all _looks_like_http_options($all[-1])
Truth table33%1/3
A and B
A= @allB= _looks_like_http_options($all[-1])
ABresult
0X0
100
111
137 0 $http_opts = pop @all;
138 0 $query_opts = \@all;
139 }
140 else {
141 3 $http_opts = undef;
142 3 $query_opts = \@all;
143 }
144 3 return ( $query_opts, $http_opts );
145 }
146  
147 3 ▶sub _looks_like_http_options {
Subroutine _looks_like_http_options: called CC=2 SCAR=8.1
148 3 my $item = shift;
149 3 ▶ return ref($item) eq 'HASH' && exists $item->{timeout};
Condition33%1/3
ref $item eq 'HASH' && exists $item->{'timeout'}
!ll&&!rl&&r
030
MC/DC0%0/2
ref $item eq 'HASH' && exists $item->{'timeout'}
ref $item eq 'HASH' exists $item->{'timeout'}
Truth table33%1/3
A && B
A= ref $item eq 'HASH'B= exists $item->{'timeout'}
ABresult
0X0
100
111
150 }
151  
152 3 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod undocumented
153 3 my $self = shift;
154  
155 3 my $client = $self->client;
156 3 my $url = $self->query_url;
157  
158 3 return $client->http_request_as_resource( 'POST', $url, @_ );
159 }
160  
161 0 ▶sub is_queryable {
Subroutine is_queryable: not called CC=3 SCAR=24.8
Pod undocumented
162 0 my $self = shift;
163 0 ▶ if ( $self->id eq 'schema' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id eq "schema") 0 0 0
164 0 return 1;
165 }
166 0 ▶ if ( $self->link('collection') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->link("collection")) 0 0 0
167 0 return 1;
168 }
169 }
170  
171 27 ▶sub query_url {
Subroutine query_url: called CC=4 SCAR=14.2
Pod undocumented
172 27 my $self = shift;
173 27 my ($id) = @_;
174  
175 27 ▶ my $base_url = $self->id eq 'schema' ? $self->link('schemas') : $self->link('collection');
Branch: 1 branch
BranchTrueFalseTotal
$self->id eq 'schema' ? : 0 27 27
176 27 ▶ if ( !defined $base_url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base_url) 0 27 27
177 0 die( "Unable to build a query url: schema '" . $self->id . "' has no 'collection' link" );
178 }
179 27 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 21 6 27
180 21 return abs_url( $base_url, $id );
181 }
182 6 return $base_url;
183 }
184  
185 4 our %REGISTRY;
186  
187 88 ▶sub registry_add {
Subroutine registry_add: called CC=2 SCAR=6.9
Pod undocumented
188 88 my $class = shift;
189 88 my %schemas = @_;
190 88 ▶ while ( my ( $key, $object ) = each(%schemas) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %schemas) 88 88 176
191 88 $REGISTRY{$key} = $object;
192 }
193 }
194  
195 42 ▶sub registry_lookup {
Subroutine registry_lookup: called CC=7 SCAR=19.7
Pod undocumented
196 42 my $class = shift;
197 42 my @possibles = @_;
198 42 foreach my $find (@possibles) {
199 42 ▶ next unless defined $find;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $find 4 38 42
200 38 ▶ if ( exists $REGISTRY{$find} and defined $REGISTRY{$find} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $WWW::GoDaddy::REST::Schema::REGISTRY{$find} and defined $WWW::GoDaddy::REST::Schema::REGISTRY{$find}) 37 1 38
Condition66%2/3
exists $WWW::GoDaddy::REST::Schema::REGISTRY{$find} and defined $WWW::GoDaddy::REST::Schema::REGISTRY{$find}
!ll&&!rl&&r
1037
MC/DC50%1/2
exists $WWW::GoDaddy::REST::Schema::REGISTRY{$find} and defined $WWW::GoDaddy::REST::Schema::REGISTRY{$find}
exists $WWW::GoDaddy::REST::Schema::REGISTRY{$find} defined $WWW::GoDaddy::REST::Schema::REGISTRY{$find}
Truth table66%2/3
A and B
A= exists $WWW::GoDaddy::REST::Schema::REGISTRY{$find}B= defined $WWW::GoDaddy::REST::Schema::REGISTRY{$find}
ABresult
0X0
100
111
201 37 return $REGISTRY{$find};
202 }
203 1 ▶ while ( my ( $key, $schema ) = each %REGISTRY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %WWW::GoDaddy::REST::Schema::REGISTRY) 22 1 23
204 22 ▶ if ( lc($find) eq lc($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $find eq lc $key) 0 22 22
205 0 return $schema;
206 }
207 }
208 }
209 5 return;
210  
211 }
212  
213 4 ▶sub registry_list {
Subroutine registry_list: called CC=1 SCAR=0.0
Pod undocumented
214 4 my $class = shift;
215 4 my %seen;
216 4 return grep { not $seen{ $_->id }++ } values %REGISTRY;
217 }
218  
219 4 1;
220  
221 =head1 NAME
222  
223 WWW::GoDaddy::REST::Schema - schema specific resource class
224  
225 =head1 SYNOPSIS
226  
227 $schema = $client->schema('the_name');
228 $scehama = $resource->schema();
229  
230 =head1 DESCRIPTION
231  
232 This is used to represent a 'schema' which is a very common resource in the
233 Go Daddy(r) API specification.
234  
235 It is a sub class of L<WWW::GoDaddy::REST::Resource>.
236  
237 =head1 METHODS
238  
239 =over 4
240  
241 =item query
242  
243 This is a helper method that, based on the parameters
244 will choose to call C<query_by_id> or C<query_complex>.
245  
246 You probably should not be calling this directly.
247  
248 See the C<query> method on L<WWW::GoDaddy::REST|WWW::GoDaddy::REST>.
249  
250 Example:
251  
252 $schema->query('1234'); # query_by_id('1234')
253 $schema->query('1234', { opt => '1' } ); # query_by_id('1234', { opt => '1' } )
254  
255 =item query_by_id
256  
257 Returns a L<Resource|WWW::GoDaddy::REST::Resource> given
258 an C<id> parameter and an optional hash reference with additional key value pairs.
259  
260 You probably should not be calling this directly.
261  
262 See the C<query> method on the L<client instance|WWW::GoDaddy::REST>.
263  
264 Example:
265  
266 $resource = $schema->query_by_id('1234');
267 $resource = $schema->query_by_id('1234', { opt => '1' } );
268  
269 =item query_complex
270  
271 Search against the collection defined by this resource.
272  
273 Returns a L<Collection|WWW::GoDaddy::REST::Collection> given a hash ref with
274 key value pairs.
275  
276 Example:
277  
278 $resource = $schema->query_complex( { opt => '1' } );
279  
280 =back
281  
282 =head1 AUTHOR
283  
284 David Bartle, C<< <davidb@mediatemple.net> >>
285  
286 =head1 COPYRIGHT & LICENSE
287  
288 Copyright (c) 2014 Go Daddy Operating Company, LLC
289  
290 Permission is hereby granted, free of charge, to any person obtaining a
291 copy of this software and associated documentation files (the "Software"),
292 to deal in the Software without restriction, including without limitation
293 the rights to use, copy, modify, merge, publish, distribute, sublicense,
294 and/or sell copies of the Software, and to permit persons to whom the
295 Software is furnished to do so, subject to the following conditions:
296  
297 The above copyright notice and this permission notice shall be included in
298 all copies or substantial portions of the Software.
299  
300 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
301 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
302 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
303 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
304 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
305 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
306 DEALINGS IN THE SOFTWARE.
307  
308 =cut
« REST/Shell/DocsCommand.pm ↑ Summary REST/Shell/Util.pm »