REST/Resource.pm

filter: Statementstmt 97.7% 176 / 180 Branchbran 88.0% 44 / 50 Conditioncond 58.1% 25 / 43 MC/DCmcdc 23.0% 6 / 26 Subroutinesub 97.1% 34 / 35 Podpod 100% 25 / 25 totaltotal 86.3% 310 / 359 CC 45 SCAR 15.4
CC
45
Cov
90%
f_as_resources
13.4
do_action
7.1
new_subclassed
5.0
CRAP 4.7
« REST/Shell/GetCommand.pm ↑ Summary REST/Util.pm »
1 package WWW::GoDaddy::REST::Resource;
2  
3 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use List::MoreUtils qw( natatime );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use WWW::GoDaddy::REST::Util qw( abs_url json_instance json_encode json_decode is_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use constant DEFAULT_IMPL_CLASS => 'WWW::GoDaddy::REST::Resource';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use overload '""' => \&to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 has 'client' => (
14 is => 'rw',
15 isa => 'WWW::GoDaddy::REST',
16 required => 1
17 );
18  
19 4 has 'fields' => (
20 is => 'ro',
21 isa => 'HashRef',
22 required => 1,
23 );
24  
25 4 has 'http_response' => (
26 is => 'ro',
27 isa => 'Maybe[HTTP::Response]',
28 required => 0
29 );
30  
31 2 ▶sub save {
Subroutine save: called CC=2 SCAR=7.4
Pod documented
32 2 my $self = shift;
33 2 my $http_opts = shift;
34 2 ▶ my $url = $self->link('self') || $self->client->schema( $self->type )->query_url( $self->id );
Condition33%1/3
$self->link('self') || $self->client->schema($self->type)->query_url($self->id)
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->link('self') || $self->client->schema($self->type)->query_url($self->id)
$self->link('self') $self->client->schema($self->type)->query_url($self->id)
Truth table33%1/3
A || B
A= $self->link('self')B= $self->client->schema($self->type)->query_url($self->id)
ABresult
1X1
011
000
35 2 return $self->client->http_request_as_resource( 'PUT', $url, $self, $http_opts );
36 }
37  
38 2 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.4
Pod documented
39 2 my $self = shift;
40 2 my $http_opts = shift;
41 2 ▶ my $url = $self->link('self') || $self->client->schema( $self->type )->query_url( $self->id );
Condition33%1/3
$self->link('self') || $self->client->schema($self->type)->query_url($self->id)
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->link('self') || $self->client->schema($self->type)->query_url($self->id)
$self->link('self') $self->client->schema($self->type)->query_url($self->id)
Truth table33%1/3
A || B
A= $self->link('self')B= $self->client->schema($self->type)->query_url($self->id)
ABresult
1X1
011
000
42 2 return $self->client->http_request_as_resource( 'DELETE', $url, $self, $http_opts );
43 }
44  
45 2 ▶sub follow_link {
Subroutine follow_link: called CC=2 SCAR=6.9
Pod documented
46 2 my $self = shift;
47 2 my $link_name = shift;
48 2 my $http_opts = shift;
49  
50 2 my $link_url = $self->link($link_name);
51 2 ▶ if ( !$link_url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link_url) 1 1 2
52 1 my @valid_links = keys %{ $self->links() };
53 1 croak("$link_name is not a valid link name. Did you mean one of these? @valid_links");
54 }
55  
56 1 return $self->client->http_request_as_resource( 'GET', $link_url, undef, $http_opts );
57 }
58  
59 5 ▶sub do_action {
Subroutine do_action: called CC=7 SCAR=19.6
Pod documented
60 5 my $self = shift;
61 5 my $action = shift;
62 5 my $params = shift;
63 5 my $http_opts = shift;
64  
65 5 my $action_url = $self->action($action);
66 5 ▶ if ( !$action_url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_url) 2 3 5
67 2 ▶ if ( $self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 2 0 2
68  
69 # try and find an action in the schema as fallback
70 2 my $schema = $self->schema();
71 2 ▶ my $resource_actions = $schema->f('resourceActions') || {};
Condition50%1/2
$schema->f('resourceActions') || {}
l!l
20
MC/DC0%0/1
$schema->f('resourceActions') || {}
$schema->f('resourceActions')
Truth table50%1/2
A
A= $schema->f('resourceActions')
Aresult
11
00
72 2 ▶ if ( exists $resource_actions->{$action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $resource_actions->{$action}) 1 1 2
73 1 ▶ my $self_uri = URI->new( $self->link('self') || $schema->query_url( $self->id ) );
Condition33%1/3
$self->link('self') || $schema->query_url($self->id)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->link('self') || $schema->query_url($self->id)
$self->link('self') $schema->query_url($self->id)
Truth table33%1/3
A || B
A= $self->link('self')B= $schema->query_url($self->id)
ABresult
1X1
011
000
74 1 $self_uri->query("$action");
75 1 $action_url = "$self_uri";
76 }
77 }
78 2 ▶ if ( !$action_url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_url) 1 1 2
79 1 croak("$action is not a valid action name.");
80 }
81 }
82  
83 4 return $self->client->http_request_as_resource( 'POST', $action_url, $params, $http_opts );
84  
85 }
86  
87 0 ▶sub items {
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
88 0 my $self = shift;
89 0 return ($self);
90 }
91  
92 165 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
93 165 return shift->f('id');
94 }
95  
96 10 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
97 10 return shift->f('type');
98 }
99  
100 10 ▶sub resource_type {
Subroutine resource_type: called CC=1 SCAR=0.0
Pod documented
101 10 return shift->f('resourceType');
102 }
103  
104 4 ▶sub type_fq {
Subroutine type_fq: called CC=1 SCAR=0.0
Pod documented
105 4 my $self = shift;
106  
107 4 my $base_url = $self->schemas_url;
108  
109 4 return abs_url( $base_url, $self->type );
110 }
111  
112 5 ▶sub resource_type_fq {
Subroutine resource_type_fq: called CC=2 SCAR=6.9
Pod documented
113 5 my $self = shift;
114  
115 5 ▶ return unless $self->resource_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_type 4 1 5
116  
117 1 return abs_url( $self->schemas_url, $self->resource_type );
118 }
119  
120 5 ▶sub schema {
Subroutine schema: called CC=3 SCAR=12.0
Pod documented
121 5 my $self = shift;
122 5 ▶ my $schema
Condition50%3/6
$self->client->schema($self->resource_type_fq) || $self->client->schema($self->type_fq) || $self->client->schema($self->type)
l!l&&r!l&&!r
500
$self->client->schema($self->resource_type_fq) or $self->client->schema($self->type_fq)
l!l&&r!l&&!r
104
MC/DC0%0/3
$self->client->schema($self->resource_type_fq) || $self->client->schema($self->type_fq) || $self->client->schema($self->type)
$self->client->schema($self->resource_type_fq) $self->client->schema($self->type_fq) $self->client->schema($self->type)
Truth table25%1/4
A or B || C
A= $self->client->schema($self->resource_type_fq)B= $self->client->schema($self->type_fq)C= $self->client->schema($self->type)
ABCresult
1XX1
01X1
0011
0000
123 = $self->client->schema( $self->resource_type_fq )
124 || $self->client->schema( $self->type_fq )
125 || $self->client->schema( $self->type );
126 5 return $schema;
127 }
128  
129 83 ▶sub link {
Subroutine link: called CC=2 SCAR=6.9
Pod documented
130 83 my $self = shift;
131 83 my $name = shift;
132  
133 83 my $links = $self->links();
134 83 ▶ if ( exists $links->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $links->{$name}) 79 4 83
135 79 return $links->{$name};
136 }
137 4 return undef;
138 }
139  
140 84 ▶sub links {
Subroutine links: called CC=1 SCAR=0.0
Pod documented
141 84 return shift->f('links');
142 }
143  
144 5 ▶sub action {
Subroutine action: called CC=2 SCAR=6.9
Pod documented
145 5 my $self = shift;
146 5 my $name = shift;
147  
148 5 my $actions = $self->actions();
149 5 ▶ if ( exists $actions->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $actions->{$name}) 3 2 5
150 3 return $actions->{$name};
151 }
152 2 return undef;
153 }
154  
155 5 ▶sub actions {
Subroutine actions: called CC=2 SCAR=6.9
Pod documented
156 5 ▶ return shift->f('actions') || {};
Condition100%2/2
(shift())->f('actions') || {}
l!l
41
MC/DC100%1/1
(shift())->f('actions') || {}
(shift())->f('actions')
Truth table100%2/2
A
A= (shift())->f('actions')
Aresult
11
00
157 }
158  
159 348 ▶sub f {
Subroutine f: called CC=1 SCAR=0.0
Pod documented
160 348 return shift->field(@_);
161 }
162  
163 7 ▶sub f_as_resources {
Subroutine f_as_resources: called CC=13 SCAR=25.9
Pod documented
164 7 my $self = shift;
165 7 my $field = shift;
166 7 my $raw_data = $self->f($field);
167  
168 7 my ( $container, $type );
169  
170 # if the 'field' is data, skip detection and use the resource type
171 7 ▶ if ( $field eq 'data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'data') { } 4 3 7
172 4 ( $container, $type ) = ( 'array', $self->resource_type );
173 }
174 else {
175 3 ( $container, $type ) = $self->schema->resource_field_type($field);
176 }
177 7 my %defaults = (
178 client => $self->client,
179 http_response => $self->http_response
180 );
181  
182 7 my $type_schema = $self->client->schema($type);
183 7 ▶ if ($type_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_schema) 6 1 7
184 6 ▶ if ( $container && $container eq 'map' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($container and $container eq 'map') { } 1 5 6
elsif ($container and $container eq 'array') { } 4 1 5
elsif (ref $raw_data eq 'HASH') { } 1 0 1
Condition100%3/3
$container and $container eq "map"
!ll&&!rl&&r
141
MC/DC100%2/2
$container and $container eq "map"
$container $container eq "map"
Truth table100%3/3
A and B
A= $containerB= $container eq "map"
ABresult
0X0
100
111
185 1 my %ret;
186 1 foreach ( my ( $k, $v ) = each %{$raw_data} ) {
187 2 ▶ $v->{type} ||= $type_schema->id;
Condition66%2/3
$v->{"type"} ||= $type_schema->id
l!l&&r!l&&!r
110
MC/DC0%0/2
$v->{"type"} ||= $type_schema->id
$v->{"type"} $type_schema->id
Truth table66%2/3
A ||= B
A= $v->{"type"}B= $type_schema->id
ABresult
1X1
011
000
188 2 $ret{$k} = $self->new_subclassed( { %defaults, fields => $v } );
189 }
190 1 return \%ret;
191 }
192 ▶ elsif ( $container && $container eq 'array' ) {
Condition66%2/3
$container and $container eq "array"
!ll&&!rl&&r
104
MC/DC50%1/2
$container and $container eq "array"
$container $container eq "array"
Truth table66%2/3
A and B
A= $containerB= $container eq "array"
ABresult
0X0
100
111
193 4 my @ret;
194 4 foreach my $v ( @{$raw_data} ) {
195 23 ▶ $v->{type} ||= $type_schema->id;
Condition33%1/3
$v->{"type"} ||= $type_schema->id
l!l&&r!l&&!r
2300
MC/DC0%0/2
$v->{"type"} ||= $type_schema->id
$v->{"type"} $type_schema->id
Truth table33%1/3
A ||= B
A= $v->{"type"}B= $type_schema->id
ABresult
1X1
011
000
196 23 push @ret, $self->new_subclassed( { %defaults, fields => $v } );
197 }
198 4 return \@ret;
199 }
200 elsif ( ref($raw_data) eq 'HASH' ) {
201 1 my %ret = %{$raw_data};
202 1 ▶ $ret{type} ||= $type_schema->id;
Condition33%1/3
$ret{"type"} ||= $type_schema->id
l!l&&r!l&&!r
010
MC/DC0%0/2
$ret{"type"} ||= $type_schema->id
$ret{"type"} $type_schema->id
Truth table33%1/3
A ||= B
A= $ret{"type"}B= $type_schema->id
ABresult
1X1
011
000
203 1 return $self->new_subclassed( { %defaults, fields => \%ret } );
204 }
205 }
206  
207 # just return the raw data if not returned otherwise
208 1 return $raw_data;
209  
210 }
211  
212 351 ▶sub field {
Subroutine field: called CC=2 SCAR=6.9
Pod documented
213 351 my $self = shift;
214 351 ▶ if ( @_ <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ <= 1) { } 346 5 351
215 346 return $self->_get_field(@_);
216 }
217 else {
218 5 return $self->_set_field(@_);
219 }
220 }
221  
222 5 ▶sub schemas_url {
Subroutine schemas_url: called CC=3 SCAR=11.4
Pod documented
223 5 my $self = shift;
224  
225 5 my $found;
226  
227 5 my $http_resp = $self->http_response;
228 5 my $link = $self->link('schemas');
229  
230 5 ▶ if ( $http_resp && $http_resp->header('X-API-Schemas') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_resp and $http_resp->header("X-API-Schemas")) 0 5 5
Condition66%2/3
$http_resp and $http_resp->header("X-API-Schemas")
!ll&&!rl&&r
230
MC/DC0%0/2
$http_resp and $http_resp->header("X-API-Schemas")
$http_resp $http_resp->header("X-API-Schemas")
Truth table66%2/3
A and B
A= $http_respB= $http_resp->header("X-API-Schemas")
ABresult
0X0
100
111
231 0 return $http_resp->header('X-API-Schemas');
232 }
233  
234 5 return $self->client->schemas_url;
235 }
236  
237 346 ▶sub _get_field {
Subroutine _get_field: called CC=2 SCAR=6.9
238 346 my $self = shift;
239 346 my $name = shift;
240  
241 346 ▶ if ( !exists $self->fields->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->fields->{$name}) 8 338 346
242 8 return undef;
243 }
244 338 return $self->fields->{$name};
245  
246 }
247  
248 5 ▶sub _set_field {
Subroutine _set_field: called CC=1 SCAR=0.0
249 5 my $self = shift;
250 5 my $name = shift;
251 5 my $value = shift;
252 5 $self->fields->{$name} = $value;
253 5 return $self->fields->{$name};
254 }
255  
256 98 ▶sub new_subclassed {
Subroutine new_subclassed: called CC=5 SCAR=16.2
Pod documented
257 98 my $class = shift;
258 98 my $params = shift;
259  
260 98 my $data = $params->{fields};
261  
262 98 my $impl = DEFAULT_IMPL_CLASS;
263 98 ▶ if ( ref($data) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 94 4 98
264 94 ▶ my $type_short = $params->{fields}->{type} || '';
Condition50%1/2
$params->{'fields'}{'type'} || ''
l!l
940
MC/DC0%0/1
$params->{'fields'}{'type'} || ''
$params->{'fields'}{'type'}
Truth table50%1/2
A
A= $params->{'fields'}{'type'}
Aresult
11
00
265 94 ▶ my $type_long = $type_short ? $params->{client}->schemas_url($type_short) : '';
Branch: 1 branch
BranchTrueFalseTotal
$type_short ? : 94 0 94
266  
267 94 ▶ $impl = $class->find_implementation( ( $type_long, $type_short ) ) || DEFAULT_IMPL_CLASS;
Condition100%2/2
$class->find_implementation($type_long, $type_short) || 'WWW::GoDaddy::REST::Resource'
l!l
5836
MC/DC100%1/1
$class->find_implementation($type_long, $type_short) || 'WWW::GoDaddy::REST::Resource'
$class->find_implementation($type_long, $type_short)
Truth table100%2/2
A
A= $class->find_implementation($type_long, $type_short)
Aresult
11
00
268 94 eval "require $impl;";
269 }
270 else {
271  
272 # hmm, the json response didn't seem to be a hashref...
273 # well in this case, there are not really any fields
274 # so the caller will have to check the http_response
275 # content
276 4 $params->{fields} = {};
277 }
278  
279 98 return $impl->new($params);
280  
281 }
282  
283 17 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
284 17 my $self = shift;
285 17 return $self->data;
286 }
287  
288 20 ▶sub data {
Subroutine data: called CC=5 SCAR=16.1
Pod documented
289 20 my $self = shift;
290  
291 20 ▶ my %fields = %{ $self->fields || {} };
Condition100%2/2
$self->fields || {}
l!l
191
MC/DC100%1/1
$self->fields || {}
$self->fields
Truth table100%2/2
A
A= $self->fields
Aresult
11
00
292 20 ▶ if (%fields) {
Branch: 2 branches
BranchTrueFalseTotal
if (%fields) { } 16 4 20
elsif ($self->http_response) { } 3 1 4
293 16 return \%fields;
294 }
295 elsif ( $self->http_response ) {
296 3 my $content = $self->http_response->decoded_content;
297 3 ▶ return is_json($content) ? json_decode($content) : $content;
Branch: 1 branch
BranchTrueFalseTotal
is_json($content) ? : 3 0 3
298 }
299 1 return {};
300 }
301  
302 13 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=7.2
Pod documented
303 13 my $self = shift;
304 13 my $pretty = shift;
305  
306 13 my $JSON = json_instance();
307 13 ▶ if ($pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 0 13 13
308 0 $JSON->pretty(1);
309 }
310 13 return json_encode( $self, $JSON );
311 }
312  
313 4 my %SCHEMA_TO_IMPL = (
314 'collection' => 'WWW::GoDaddy::REST::Collection',
315 'schema' => 'WWW::GoDaddy::REST::Schema',
316 );
317  
318 100 ▶sub find_implementation {
Subroutine find_implementation: called CC=3 SCAR=11.0
Pod documented
319 100 my $class = shift;
320 100 my @look_for = @_;
321  
322 100 foreach (@look_for) {
323 194 ▶ if ( exists $SCHEMA_TO_IMPL{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SCHEMA_TO_IMPL{$_}) 63 131 194
324 63 return $SCHEMA_TO_IMPL{$_};
325 }
326 }
327 37 return;
328 }
329  
330 3 ▶sub register_implementation {
Subroutine register_implementation: called CC=3 SCAR=11.0
Pod documented
331 3 my $class = shift;
332  
333 3 ▶ if ( @_ % 2 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 1 2 3
334 1 croak("Expecting even number of parameters");
335 }
336  
337 2 my $iterator = natatime 2, @_;
338 2 ▶ while ( my ( $schema, $subclass ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 3 2 5
339 3 $SCHEMA_TO_IMPL{$schema} = $subclass;
340 }
341 2 return;
342 }
343  
344 4 1;
345  
346 =head1 NAME
347  
348 WWW::GoDaddy::REST::Resource - Represent a REST resource
349  
350 =head1 SYNOPSIS
351  
352 $client = WWW::GoDaddy::REST->new(...);
353  
354 $resource = WWW::GoDaddy::REST::Resource->new({
355 client => $client,
356 fields => {
357 'type' => 'automobile',
358 'id' => '1001',
359 'make' => 'Tesla',
360 'model' => 'S'
361 'links' => {
362 'self' => 'https://example.com/v1/automobiles/1001',
363 'schemas' => 'https://example.com/v1/schemas'
364 },
365 'actions' => {
366 'charge' => 'https://example.com/v1/automobiles/1001?charge'
367 }
368 # ...
369 # see: https://github.com/godaddy/gdapi/blob/master/specification.md
370 },
371 });
372  
373 $resource->f('id'); # get 1001
374 $resource->f('id','2000'); # set to 2000 and return 2000
375  
376 # follow a link in links section
377 $schemas_resource = $resource->follow_link('schemas');
378  
379 # perform an action in the actions section
380 $result_resource = $resource->do_action('charge',{ 'with' => 'quick_charger' });
381  
382 =head1 DESCRIPTION
383  
384 Base class used to represent a REST resource.
385  
386 =head1 CLASS METHODS
387  
388 =over 4
389  
390 =item new
391  
392 Given a hash reference of L<"ATTRIBUTES"> and values, return a new instance
393 of this object.
394  
395 It is likely more important that you use the C<new_subclassed> class method.
396  
397 Example:
398  
399 my $resource = WWW::GoDaddy::REST::Resource->new({
400 client => WWW::GoDaddy::REST->new(...),
401 fields => {
402 id => '...',
403 ...
404 },
405 });
406  
407 =item new_subclassed
408  
409 This takes the same paramegers as C<new> and is the preferred construction
410 method. This tries to find the appropriate subclass of
411 C<WWW::GoDaddy::REST::Resource> and passes along the paramegers to the C<new>
412 method of that subclass instead.
413  
414 See also: C<new>
415  
416 =item find_implementation
417  
418 Given a list of schema type names, find the best implementation sub class.
419  
420 Returns the string of the class name. If no good subclass candidate is found,
421 returns undef.
422  
423 Example:
424  
425 find_implementation( 'schema' );
426 # WWW::GoDaddy::REST::Schema
427  
428 =item register_implementation
429  
430 Register a subclass handler for a schema type given a schema name and the
431 name of a L<WWW::GoDaddy::REST::Resource> subclass.
432  
433 This can take as many schema => resource class pairs as you want.
434  
435 Example:
436  
437 WWW::GoDaddy::REST::Resource->register_subclass( 'account' => 'My::AccountRes' );
438 WWW::GoDaddy::REST::Resource->register_subclass( 'foo' => 'Bar', 'baz' => 'Buzz' );
439  
440 =back
441  
442 =head1 ATTRIBUTES
443  
444 =over 4
445  
446 =item client
447  
448 Instance of L<WWW::GoDaddy::REST> associated with the resource.
449  
450 =item fields
451  
452 Hash reference containing the raw data for the underlying resource.
453  
454 Several methods delegate to this underlying structure such as C<f>,
455 and C<field>.
456  
457 =item http_response
458  
459 Optionally present instance of an L<HTTP::Response> object so that
460 you can inspect the HTTP information related to the resource.
461  
462 =back
463  
464 =head1 METHODS
465  
466 =over 4
467  
468 =item f
469  
470 Get or set a field by name. You may also use the longer name C<field>.
471  
472 When performing a set, it also returns the new value that was set.
473  
474 Example:
475  
476 $res->f('field_name'); # get
477 $res->f('field_name','new'); # set
478  
479 =item f_as_resources
480  
481 Get a field by name. If it is a resource, this will turn it into an
482 object instead of giving you the raw hash reference as the return value.
483  
484 Note, if the field is a 'map' or 'array' of resources, every item in
485 those lists will be 'resourcified'.
486  
487 If this is not a resource, then it does return the raw value.
488  
489 Example:
490  
491 # return value is a WWW::GoDaddy::REST::Resource, not a hash ref
492 $driver = $car->f('driver');
493  
494 See C<f> if you want the raw value. This will return the raw value, if
495 the value does not look like a resource.
496  
497 =item field
498  
499 Get or set a field by name. You may also use the shorter name C<f>.
500  
501 When performing a set, it also returns the new value that was set.
502  
503 Example:
504  
505 $res->field('field_name'); # get
506 $res->fieldf('field_name','new'); # set
507  
508 =item save
509  
510 Does a PUT at this resources URI. Returns a new resource object.
511  
512 Example:
513  
514 $r2 = $r1->save();
515  
516 =item delete
517  
518 Does a DELETE on this resource. Returns a new resource object. This
519 return value likely is only useful to get at the C<http_response> attribute.
520  
521 =item do_action
522  
523 Does a POST with the supplied data on the action URL with the given name.
524  
525 If the action with the provided name does not exist, this method will
526 die. See also: C<action> and C<actions>
527  
528 Example:
529  
530 $r2 = $r1->do_action('some_action',{ a => 'a_v' });
531  
532 =item follow_link
533  
534 Gets the resource by following the link URL with the provided name.
535  
536 If the link with the provided name does not exist, this method will
537 die. See also: C<link> and C<link>
538  
539 Example:
540  
541 $r2 = $r1->follow_link('some_link');
542  
543 =item id
544  
545 Return the id of this instance
546  
547 =item type
548  
549 Return the name of the schema type that this object belongs to.
550  
551 =item type_fq
552  
553 Return the full URI to the schema type that this object belongs to.
554  
555 =item resource_type
556  
557 Return the name of the schema type that this collection's objects belong to.
558  
559 =item resource_type_fq
560  
561 Return the full URI to the schema type that this collection's objects belong to.
562  
563 =item schemas_url
564  
565 Returns the URL for the schema collection. This differs from the
566 client C<schemas_url> method since it has more places to look for hints
567 of the schemas collection url (headers, json response etc).
568  
569 Example:
570  
571 $r->schemas_url();
572  
573 =item schema
574  
575 Find and return the L<WWW::GoDaddy::REST::Schema> object that this is.
576  
577 =item link
578  
579 Return the link URL for the given name or undef if it does not exist.
580  
581 Example:
582  
583 # https://example.com/v1/thing/...
584 $r->link('self');
585 # 'https://example.com/v1/me/1'
586  
587 =item links
588  
589 Return the hashref that contains the link => url information
590  
591 Example:
592  
593 $r->links();
594 # {
595 # 'self' => 'https://example.com/v1/me/1',
596 # 'some_link' => 'https://example.com/v1/me/1/some_link'
597 # }
598  
599 =item action
600  
601 Return the action URL for the given name.
602  
603 Example:
604  
605 $r->action('custom_action');
606 # https://example.com/v1/thing/1001?some_action
607  
608 =item actions
609  
610 Return the hashref that contains the action => url information
611  
612 Example:
613  
614 $r->actions();
615 # {
616 # 'custom_action' => 'https://example.com/v1/thing/1001?some_action'
617 # }
618  
619 =item items
620  
621 Returns a list of resources that this resource contains. This implementation
622 simply returns a list of 'self'. It is here to be consistent with the
623 implementation found in L<WWW::GoDaddy::REST::Collection>.
624  
625 Example:
626  
627 @items = $resource->items();
628  
629 =item TO_JSON
630  
631 Returns a hashref that represents this object. This exists to make using the
632 L<JSON> module more convenient. This does NOT return a JSON STRING, just a
633 perl data structure.
634  
635 See C<to_string>.
636  
637 =item to_string
638  
639 Returns a JSON string that represents this object. This takes an optional
640 parameter, "pretty". If true, the json output will be prettified. This defaults
641 to false.
642  
643 =item data
644  
645 The resource is returned as a perl data structure. Note, if there are no
646 C<fields>, then the http_respons is consulted, if json data is found in
647 the content, that is returned (for instance, a plane old string or integer).
648  
649 =back
650  
651 =head1 AUTHOR
652  
653 David Bartle, C<< <davidb@mediatemple.net> >>
654  
655 =head1 COPYRIGHT & LICENSE
656  
657 Copyright (c) 2014 Go Daddy Operating Company, LLC
658  
659 Permission is hereby granted, free of charge, to any person obtaining a
660 copy of this software and associated documentation files (the "Software"),
661 to deal in the Software without restriction, including without limitation
662 the rights to use, copy, modify, merge, publish, distribute, sublicense,
663 and/or sell copies of the Software, and to permit persons to whom the
664 Software is furnished to do so, subject to the following conditions:
665  
666 The above copyright notice and this permission notice shall be included in
667 all copies or substantial portions of the Software.
668  
669 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
670 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
671 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
672 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
673 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
674 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
675 DEALINGS IN THE SOFTWARE.
676  
677  
678 =cut
« REST/Shell/GetCommand.pm ↑ Summary REST/Util.pm »