| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::FreeIPA::Response; | 
| 2 |  |  |  |  |  |  | $Net::FreeIPA::Response::VERSION = '3.0.0'; | 
| 3 | 8 |  |  | 8 |  | 372 | use strict; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 167 |  | 
| 4 | 8 |  |  | 8 |  | 22 | use warnings; | 
|  | 8 |  |  |  |  | 6 |  | 
|  | 8 |  |  |  |  | 152 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 21 | use base qw(Exporter); | 
|  | 8 |  |  |  |  | 6 |  | 
|  | 8 |  |  |  |  | 422 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 331 | use Net::FreeIPA::Error; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 386 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @EXPORT = qw(mkresponse); | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 8 |  |  | 8 |  | 27 | use overload bool => '_boolean'; | 
|  | 8 |  |  |  |  | 7 |  | 
|  | 8 |  |  |  |  | 34 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 8 |  |  | 8 |  | 342 | use Readonly; | 
|  | 8 |  |  |  |  | 7 |  | 
|  | 8 |  |  |  |  | 2203 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Readonly my $RESULT_PATH => 'result/result'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 NAME | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | Net::FreeIPA::Response is an response class for Net::FreeIPA. | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Boolean logic is overloaded using C<_boolean> method (as inverse of C). | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head2 Public methods | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =over | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =item mkresponse | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | A C factory | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =cut | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub mkresponse | 
| 35 |  |  |  |  |  |  | { | 
| 36 | 4 |  |  | 4 | 1 | 332 | return Net::FreeIPA::Response->new(@_); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =item new | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | Create new response instance. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Options | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =over | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =item answer: complete answer hashref | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =item error: an error (passed to C). | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =item result_path: passed to C to set the result attribute. | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =back | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub new | 
| 59 |  |  |  |  |  |  | { | 
| 60 | 5 |  |  | 5 | 1 | 15 | my ($this, %opts) = @_; | 
| 61 | 5 |  | 33 |  |  | 19 | my $class = ref($this) || $this; | 
| 62 |  |  |  |  |  |  | my $self = { | 
| 63 |  |  |  |  |  |  | answer => $opts{answer} || {}, | 
| 64 | 5 |  | 100 |  |  | 17 | }; | 
| 65 | 5 |  |  |  |  | 5 | bless $self, $class; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # First error | 
| 68 | 5 |  |  |  |  | 11 | $self->set_error($opts{error}); | 
| 69 |  |  |  |  |  |  | # Then result | 
| 70 | 5 |  |  |  |  | 14 | $self->set_result($opts{result_path}); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 5 |  |  |  |  | 10 | return $self; | 
| 73 |  |  |  |  |  |  | }; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =item set_error | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Set and return the error attribute using C. | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =cut | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub set_error | 
| 82 |  |  |  |  |  |  | { | 
| 83 | 7 |  |  | 7 | 1 | 6 | my $self = shift; | 
| 84 | 7 |  |  |  |  | 16 | $self->{error} = mkerror(@_); | 
| 85 | 7 |  |  |  |  | 8 | return $self->{error}; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =item set_result | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | Set and return the result attribute based on the C. | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | The C is path-like string, indicating which subtree of the answer | 
| 93 |  |  |  |  |  |  | should be set as result attribute (default C). | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =cut | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub set_result | 
| 98 |  |  |  |  |  |  | { | 
| 99 | 9 |  |  | 9 | 1 | 12 | my ($self, $result_path) = @_; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 9 |  |  |  |  | 8 | my $res; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 9 | 100 |  |  |  | 10 | if (! $self->is_error()) { | 
| 104 | 6 | 100 |  |  |  | 17 | $result_path = $RESULT_PATH if ! defined($result_path); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 6 |  |  |  |  | 16 | $res = $self->{answer}; | 
| 107 |  |  |  |  |  |  | # remove any "empty" paths | 
| 108 | 6 |  |  |  |  | 31 | foreach my $subpath (grep {$_} split('/', $result_path)) { | 
|  | 14 |  |  |  |  | 17 |  | 
| 109 | 14 | 100 |  |  |  | 24 | $res = $res->{$subpath} if (defined($res)); | 
| 110 |  |  |  |  |  |  | }; | 
| 111 |  |  |  |  |  |  | }; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 9 |  |  |  |  | 11 | $self->{result} = $res; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 9 |  |  |  |  | 17 | return $self->{result}; | 
| 116 |  |  |  |  |  |  | }; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =item is_error | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Test if this is an error or not (based on error attribute). | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =cut | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub is_error | 
| 125 |  |  |  |  |  |  | { | 
| 126 | 18 |  |  | 18 | 1 | 394 | my $self = shift; | 
| 127 | 18 | 100 |  |  |  | 42 | return $self->{error} ? 1 : 0; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | # Overloaded boolean, inverse of is_error | 
| 131 |  |  |  |  |  |  | sub _boolean | 
| 132 |  |  |  |  |  |  | { | 
| 133 | 4 |  |  | 4 |  | 22 | my $self = shift; | 
| 134 | 4 |  |  |  |  | 5 | return ! $self->is_error(); | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =pod | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =back | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =cut | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | 1; |