| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::GoDaddy::REST::Schema; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 1323 | use Moose; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | use WWW::GoDaddy::REST::Util qw( abs_url build_complex_query_url ); | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | extends 'WWW::GoDaddy::REST::Resource'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub resource_field_names { | 
| 9 |  |  |  |  |  |  | my $self       = shift; | 
| 10 |  |  |  |  |  |  | my %res_fields = %{ $self->f('resourceFields') }; | 
| 11 |  |  |  |  |  |  | return keys %res_fields; | 
| 12 |  |  |  |  |  |  | } | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub resource_field { | 
| 15 |  |  |  |  |  |  | my $self       = shift; | 
| 16 |  |  |  |  |  |  | my $name       = shift; | 
| 17 |  |  |  |  |  |  | my $res_fields = $self->f('resourceFields'); | 
| 18 |  |  |  |  |  |  | if ( !exists $res_fields->{$name} ) { | 
| 19 |  |  |  |  |  |  | return undef; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  | return $res_fields->{$name}; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub resource_field_type { | 
| 25 |  |  |  |  |  |  | my $self = shift; | 
| 26 |  |  |  |  |  |  | my $name = shift; | 
| 27 |  |  |  |  |  |  | my $opts = shift || {}; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my $type                     = $self->resource_field($name)->{'type'} || 'string'; | 
| 30 |  |  |  |  |  |  | my $want_array               = wantarray; | 
| 31 |  |  |  |  |  |  | my $auto_upconvert_reference = $opts->{auto_upconvert_reference}; | 
| 32 |  |  |  |  |  |  | my $qualify_schema_types     = $opts->{qualify_schema_types}; | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | if ( $want_array or $auto_upconvert_reference or $qualify_schema_types ) { | 
| 35 |  |  |  |  |  |  | my ( $a_type, $b_type ) = split( /[\[\]]/, $type ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | my $compound_type = $b_type ? 1 : 0; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | if ($auto_upconvert_reference) { | 
| 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 |  |  |  |  |  |  | if ( !$compound_type && $name =~ /^(.*)(_id|Id)$/ ) { | 
| 47 |  |  |  |  |  |  | my $possible_schema = $self->client->schema($1); | 
| 48 |  |  |  |  |  |  | if ($possible_schema) { | 
| 49 |  |  |  |  |  |  | $compound_type = 1; | 
| 50 |  |  |  |  |  |  | $a_type        = 'reference'; | 
| 51 |  |  |  |  |  |  | $b_type        = $possible_schema->id; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | if ($qualify_schema_types) { | 
| 57 |  |  |  |  |  |  | if ($compound_type) { | 
| 58 |  |  |  |  |  |  | my $possible_schema = $self->client->schema($b_type); | 
| 59 |  |  |  |  |  |  | if ($possible_schema) { | 
| 60 |  |  |  |  |  |  | $b_type = $possible_schema->link('self'); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | else { | 
| 64 |  |  |  |  |  |  | my $possible_schema = $self->client->schema($a_type); | 
| 65 |  |  |  |  |  |  | if ($possible_schema) { | 
| 66 |  |  |  |  |  |  | $a_type = $possible_schema->link('self'); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | if ($want_array) { | 
| 72 |  |  |  |  |  |  | if ($compound_type) { | 
| 73 |  |  |  |  |  |  | return ( $a_type, $b_type ); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | else { | 
| 76 |  |  |  |  |  |  | return ( undef, $a_type ); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  | else { | 
| 80 |  |  |  |  |  |  | if ($compound_type) { | 
| 81 |  |  |  |  |  |  | $type = sprintf( '%s[%s]', $a_type, $b_type ); | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  | else { | 
| 84 |  |  |  |  |  |  | $type = $a_type; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | return $type; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub query { | 
| 92 |  |  |  |  |  |  | my $self = shift; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | if ( !ref( $_[0] ) ) { | 
| 95 |  |  |  |  |  |  | return $self->query_by_id(@_); | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | else { | 
| 98 |  |  |  |  |  |  | return $self->query_complex(@_); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub query_complex { | 
| 104 |  |  |  |  |  |  | my $self = shift; | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | my $url = build_complex_query_url( $self->query_url, @_ ); | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | my $resource = $self->client->http_request_as_resource( 'GET', $url ); | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | return wantarray ? $resource->items() : $resource; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub query_by_id { | 
| 114 |  |  |  |  |  |  | my $self = shift; | 
| 115 |  |  |  |  |  |  | my $id   = shift; | 
| 116 |  |  |  |  |  |  | my $opts = shift || {}; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | my $client = $self->client; | 
| 119 |  |  |  |  |  |  | my $url    = build_complex_query_url( $self->query_url($id), $opts ); | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | return $client->http_request_as_resource( 'GET', $url ); | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub create { | 
| 125 |  |  |  |  |  |  | my $self = shift; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $client = $self->client; | 
| 128 |  |  |  |  |  |  | my $url    = $self->query_url; | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | return $client->http_request_as_resource( 'POST', $url, @_ ); | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | sub is_queryable { | 
| 134 |  |  |  |  |  |  | my $self = shift; | 
| 135 |  |  |  |  |  |  | if ( $self->id eq 'schema' ) { | 
| 136 |  |  |  |  |  |  | return 1; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  | if ( $self->link('collection') ) { | 
| 139 |  |  |  |  |  |  | return 1; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub query_url { | 
| 144 |  |  |  |  |  |  | my $self = shift; | 
| 145 |  |  |  |  |  |  | my ($id) = @_; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | my $base_url = $self->id eq 'schema' ? $self->link('schemas') : $self->link('collection'); | 
| 148 |  |  |  |  |  |  | if ( !defined $base_url ) { | 
| 149 |  |  |  |  |  |  | die( "Unable to build a query url: schema '" . $self->id . "' has no 'collection' link" ); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  | if ( defined $id ) { | 
| 152 |  |  |  |  |  |  | return abs_url( $base_url, $id ); | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  | return $base_url; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | our %REGISTRY; | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | sub registry_add { | 
| 160 |  |  |  |  |  |  | my $class   = shift; | 
| 161 |  |  |  |  |  |  | my %schemas = @_; | 
| 162 |  |  |  |  |  |  | while ( my ( $key, $object ) = each(%schemas) ) { | 
| 163 |  |  |  |  |  |  | $REGISTRY{$key} = $object; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | sub registry_lookup { | 
| 168 |  |  |  |  |  |  | my $class     = shift; | 
| 169 |  |  |  |  |  |  | my @possibles = @_; | 
| 170 |  |  |  |  |  |  | foreach my $find (@possibles) { | 
| 171 |  |  |  |  |  |  | next unless defined $find; | 
| 172 |  |  |  |  |  |  | if ( exists $REGISTRY{$find} and defined $REGISTRY{$find} ) { | 
| 173 |  |  |  |  |  |  | return $REGISTRY{$find}; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  | while ( my ( $key, $schema ) = each %REGISTRY ) { | 
| 176 |  |  |  |  |  |  | if ( lc($find) eq lc($key) ) { | 
| 177 |  |  |  |  |  |  | return $schema; | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  | return; | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | sub registry_list { | 
| 186 |  |  |  |  |  |  | my $class = shift; | 
| 187 |  |  |  |  |  |  | my %seen; | 
| 188 |  |  |  |  |  |  | return grep { not $seen{ $_->id }++ } values %REGISTRY; | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | 1; | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =head1 NAME | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | WWW::GoDaddy::REST::Schema - schema specific resource class | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | $schema  = $client->schema('the_name'); | 
| 200 |  |  |  |  |  |  | $scehama = $resource->schema(); | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | This is used to represent a 'schema' which is a very common resource in the | 
| 205 |  |  |  |  |  |  | Go Daddy(r) API specification. | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | It is a sub class of L<WWW::GoDaddy::REST::Resource>. | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | =head1 METHODS | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =over 4 | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =item query | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | This is a helper method that, based on the parameters | 
| 216 |  |  |  |  |  |  | will choose to call C<query_by_id> or C<query_complex>. | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | You probably should not be calling this directly. | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | See the C<query> method on L<WWW::GoDaddy::REST|WWW::GoDaddy::REST>. | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | Example: | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | $schema->query('1234'); # query_by_id('1234') | 
| 225 |  |  |  |  |  |  | $schema->query('1234', { opt => '1' } ); # query_by_id('1234', { opt => '1' } ) | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =item query_by_id | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | Returns a L<Resource|WWW::GoDaddy::REST::Resource> given | 
| 230 |  |  |  |  |  |  | an C<id> parameter and an optional hash reference with additional key value pairs. | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | You probably should not be calling this directly. | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | See the C<query> method on the L<client instance|WWW::GoDaddy::REST>. | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | Example: | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | $resource = $schema->query_by_id('1234'); | 
| 239 |  |  |  |  |  |  | $resource = $schema->query_by_id('1234', { opt => '1' } ); | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =item query_complex | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | Search against the collection defined by this resource. | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | Returns a L<Collection|WWW::GoDaddy::REST::Collection> given a hash ref with | 
| 246 |  |  |  |  |  |  | key value pairs. | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | Example: | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | $resource = $schema->query_complex( { opt => '1' } ); | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | =back | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | =head1 AUTHOR | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | David Bartle, C<< <davidb@mediatemple.net> >> | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | Copyright (c) 2014 Go Daddy Operating Company, LLC | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | Permission is hereby granted, free of charge, to any person obtaining a | 
| 263 |  |  |  |  |  |  | copy of this software and associated documentation files (the "Software"), | 
| 264 |  |  |  |  |  |  | to deal in the Software without restriction, including without limitation | 
| 265 |  |  |  |  |  |  | the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
| 266 |  |  |  |  |  |  | and/or sell copies of the Software, and to permit persons to whom the | 
| 267 |  |  |  |  |  |  | Software is furnished to do so, subject to the following conditions: | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | The above copyright notice and this permission notice shall be included in | 
| 270 |  |  |  |  |  |  | all copies or substantial portions of the Software. | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| 273 |  |  |  |  |  |  | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| 274 |  |  |  |  |  |  | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 
| 275 |  |  |  |  |  |  | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| 276 |  |  |  |  |  |  | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
| 277 |  |  |  |  |  |  | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
| 278 |  |  |  |  |  |  | DEALINGS IN THE SOFTWARE. | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | =cut |