File Coverage

blib/lib/Mojo/Redfish/Client/Result.pm
Criterion Covered Total %
statement 33 37 89.1
branch 11 16 68.7
condition 1 6 16.6
subroutine 9 11 81.8
pod 3 3 100.0
total 57 73 78.0


line stmt bran cond sub pod time code
1             package Mojo::Redfish::Client::Result;
2              
3 2     2   12 use Mojo::Base -base;
  2         4  
  2         13  
4              
5 2     2   266 use Carp ();
  2         4  
  2         27  
6 2     2   8 use Mojo::Collection;
  2         4  
  2         54  
7 2     2   375 use Mojo::JSON::Pointer;
  2         509  
  2         16  
8 2     2   59 use Scalar::Util ();
  2         16  
  2         4046  
9              
10             my $isa = sub { Scalar::Util::blessed($_[0]) && $_[0]->isa($_[1]) };
11              
12             has client => sub { Carp::croak 'client is required' }, weak => 1;
13             has data => sub { Carp::croak 'data is required' };
14              
15             sub get {
16 2     2 1 2875 my ($self, $path) = @_;
17 2 50       7 return $self unless defined $path;
18 2         6 my $target = Mojo::JSON::Pointer->new($self->data)->get($path);
19 2         72 return $self->_get($target);
20             }
21              
22             sub value {
23 7     7 1 8210 my ($self, $path) = @_;
24 7 50       21 return $self->data unless defined $path;
25              
26 7         20 my $target = Mojo::JSON::Pointer->new($self->data)->get($path);
27              
28 7 100       296 $target = Mojo::Collection->new(@$target)
29             if ref $target eq 'ARRAY';
30              
31 7         35 return $target;
32             }
33              
34 0     0 1 0 sub TO_JSON { shift->data }
35              
36             sub _get {
37 4     4   9 my ($self, $target) = @_;
38 4 50       11 return $target
39             if $target->$isa('Mojo::Redfish::Client::Result');
40              
41 4 100       14 $target = Mojo::Collection->new(@$target)
42             if ref $target eq 'ARRAY';
43              
44 2     2   44 return $target->map(sub{ $self->_get($_) })
45 4 100       13 if $target->$isa('Mojo::Collection');
46              
47 3 50       8 if (ref $target eq 'HASH') {
48 3 50 33     20 if (keys %$target == 1 && exists $target->{'@odata.id'}) {
49 3         6 $target = $target->{'@odata.id'};
50             } else {
51 0         0 return $self->_clone($target);
52             }
53             }
54              
55 3         9 return $self->client->get($target);
56             }
57              
58             sub _clone {
59 0     0     my ($self, $data) = @_;
60 0   0       $self->new(
61             client => $self->client,
62             data => ($data // $self->data),
63             );
64             }
65              
66             1;
67              
68             =head1 NAME
69              
70             Mojo::Redfish::Client::Result
71              
72             =head1 DESCRIPTION
73              
74             A class to get represent the result of a request from L.
75             It encapsulates the returned data and facilitates walking further out in the tree by following links.
76              
77             =head1 ATTRIBUTES
78              
79             L inherits all of the attributes from L and implements the following new ones.
80              
81             =head2 client
82              
83             An instance of L, usually the one that created this object.
84             Required and weakened.
85              
86             =head2 data
87              
88             The payload result from the Redfish request.
89              
90             =head1 METHODS
91              
92             L inherits all of the methods from L and implements the following new ones.
93              
94             =head2 get
95              
96             $result = $result->get;
97             my $deeper = $result->get('/deeper/key');
98              
99             Get the value of the L for the given JSON Pointer, making additional requests to follow links if needed.
100             Array values are upgraded to L objects and all (directly) contained values are fetched (if needed).
101             The result is always either a result object or a collection; use L to get a simple value out of the data by pointer.
102              
103             =head2 value
104              
105             Similar to L this method takes a pointer to dive into the L however the result is never fetched from the Redfish server and the value is not upgraded to a results object.
106             Arrays are still upgraded to L objects.
107              
108             =head2 TO_JSON
109              
110             Alias for L as a getter only.
111              
112             =head1 SEE ALSO
113              
114             =over
115              
116             =item L
117              
118             =back
119              
120              
121