File Coverage

blib/lib/Mojo/Redfish/Client/Result.pm
Criterion Covered Total %
statement 55 60 91.6
branch 19 30 63.3
condition 2 9 22.2
subroutine 14 16 87.5
pod 4 4 100.0
total 94 119 78.9


line stmt bran cond sub pod time code
1             package Mojo::Redfish::Client::Result;
2              
3 3     3   21 use Mojo::Base -base;
  3         6  
  3         22  
4              
5 3     3   471 use Carp ();
  3         11  
  3         57  
6 3     3   14 use Mojo::Collection;
  3         14  
  3         191  
7 3     3   21 use Mojo::Promise;
  3         23  
  3         21  
8 3     3   563 use Mojo::JSON::Pointer;
  3         586  
  3         30  
9 3     3   122 use Scalar::Util ();
  3         6  
  3         3001  
10              
11             my $isa = sub { Scalar::Util::blessed($_[0]) && $_[0]->isa($_[1]) };
12              
13             has client => sub { Carp::croak 'client is required' }, weak => 1;
14             has data => sub { Carp::croak 'data is required' };
15              
16             sub get {
17 2     2 1 981 my ($self, $path) = @_;
18 2 50       7 return $self unless defined $path;
19 2         7 my $target = Mojo::JSON::Pointer->new($self->data)->get($path);
20 2         84 return $self->_get($target);
21             }
22              
23             sub get_p {
24 2     2 1 975 my ($self, $path) = @_;
25 2 50       8 return $self unless defined $path;
26 2         7 my $target = Mojo::JSON::Pointer->new($self->data)->get($path);
27 2         88 return $self->_get_p($target);
28             }
29              
30             sub value {
31 14     14 1 9148 my ($self, $path) = @_;
32 14 50       45 return $self->data unless defined $path;
33              
34 14         45 my $target = Mojo::JSON::Pointer->new($self->data)->get($path);
35              
36 14 100       700 $target = Mojo::Collection->new(@$target)
37             if ref $target eq 'ARRAY';
38              
39 14         81 return $target;
40             }
41              
42 0     0 1 0 sub TO_JSON { shift->data }
43              
44             sub _get {
45 4     4   11 my ($self, $target) = @_;
46 4 50       12 return $target
47             if $target->$isa('Mojo::Redfish::Client::Result');
48              
49 4 100       15 $target = Mojo::Collection->new(@$target)
50             if ref $target eq 'ARRAY';
51              
52 2     2   45 return $target->map(sub{ $self->_get($_) })
53 4 100       13 if $target->$isa('Mojo::Collection');
54              
55 3 50       8 if (ref $target eq 'HASH') {
56 3 50 33     19 if (keys %$target == 1 && exists $target->{'@odata.id'}) {
57 3         7 $target = $target->{'@odata.id'};
58             } else {
59 0         0 return $self->_clone($target);
60             }
61             }
62              
63 3         9 return $self->client->get($target);
64             }
65              
66             sub _get_p {
67 4     4   11 my ($self, $target) = @_;
68 4 50       11 return Mojo::Promise->resolve($target)
69             if $target->$isa('Mojo::Redfish::Client::Result');
70              
71 4 50       10 $target = $target->to_array
72             if $target->$isa('Mojo::Collection');
73              
74 4 100       14 if (ref $target eq 'ARRAY') {
75 1         3 my %opt;
76 1 50       4 if (my $c = $self->client->concurrency) { $opt{concurrency} = $c }
  1         13  
77 2     2   1385 return Mojo::Promise->map(\%opt, sub{ $self->_get_p($_) }, @$target)
78 1     1   12 ->then(sub{ Mojo::Collection->new(map { $_->[0] } @_) });
  1         489  
  2         10  
79             }
80              
81 3 50       19 if (ref $target eq 'HASH') {
82 3 50 33     21 if (keys %$target == 1 && exists $target->{'@odata.id'}) {
83 3         7 $target = $target->{'@odata.id'};
84             } else {
85 0         0 return $self->_clone($target);
86             }
87             }
88              
89 3         11 return $self->client->get_p($target);
90             }
91              
92             sub _clone {
93 0     0     my ($self, $data) = @_;
94 0   0       $self->new(
95             client => $self->client,
96             data => ($data // $self->data),
97             );
98             }
99              
100             1;
101              
102             =head1 NAME
103              
104             Mojo::Redfish::Client::Result
105              
106             =head1 DESCRIPTION
107              
108             A class to get represent the result of a request from L.
109             It encapsulates the returned data and facilitates walking further out in the tree by following links.
110              
111             =head1 ATTRIBUTES
112              
113             L inherits all of the attributes from L and implements the following new ones.
114              
115             =head2 client
116              
117             An instance of L, usually the one that created this object.
118             Required and weakened.
119              
120             =head2 data
121              
122             The payload result from the Redfish request.
123              
124             =head1 METHODS
125              
126             L inherits all of the methods from L and implements the following new ones.
127              
128             =head2 get
129              
130             $result = $result->get;
131             my $deeper = $result->get('/deeper/key');
132              
133             Get the value of the L for the given JSON Pointer, making additional requests to follow links if needed.
134             Array values are upgraded to L objects and all (directly) contained values are fetched (if needed).
135             The result is always either a result object or a collection; use L to get a simple value out of the data by pointer.
136              
137             =head2 get_p
138              
139             Same as L but returns a L that resolves to the result.
140              
141             =head2 value
142              
143             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.
144             Arrays are still upgraded to L objects.
145              
146             =head2 TO_JSON
147              
148             Alias for L as a getter only.
149              
150             =head1 SEE ALSO
151              
152             =over
153              
154             =item L
155              
156             =back
157              
158              
159