File Coverage

blib/lib/WebService/Hexonet/Connector/Response.pm
Criterion Covered Total %
statement 230 234 98.2
branch 56 66 84.8
condition 11 18 61.1
subroutine 44 44 100.0
pod 27 28 96.4
total 368 390 94.3


line stmt bran cond sub pod time code
1             package WebService::Hexonet::Connector::Response;
2              
3 1     1   18 use 5.026_000;
  1         3  
4 1     1   26 use strict;
  1         3  
  1         25  
5 1     1   6 use warnings;
  1         2  
  1         35  
6 1     1   709 use WebService::Hexonet::Connector::Column;
  1         3  
  1         30  
7 1     1   664 use WebService::Hexonet::Connector::Record;
  1         3  
  1         32  
8 1     1   479 use parent qw(WebService::Hexonet::Connector::ResponseTemplate);
  1         306  
  1         5  
9 1     1   45 use POSIX qw(ceil floor);
  1         2  
  1         7  
10 1     1   724 use List::MoreUtils qw(first_index);
  1         12769  
  1         6  
11 1     1   1089 use Readonly;
  1         3  
  1         70  
12             Readonly my $INDEX_NOT_FOUND => -1;
13              
14 1     1   7 use version 0.9917; our $VERSION = version->declare('v2.9.0');
  1         16  
  1         7  
15              
16              
17             sub new {
18 51     51 1 31070 my ( $class, $raw, $cmd, $ph ) = @_;
19 51         283 my $self = WebService::Hexonet::Connector::ResponseTemplate->new($raw);
20             # care about getting placeholder variables replaced
21 51 100       257 if ( $self->{raw} =~ /[{][[:upper:]_]+[}]/gsmx ) {
22 4 100       14 if ( !defined $ph ) {
23 3         6 $ph = {};
24             }
25 4         6 foreach my $key ( keys %{$ph} ) {
  4         14  
26 1         9 my $find = "[{]${key}[}]";
27 1         3 my $replace = $ph->{$key};
28 1         23 $self->{raw} =~ s/$find/$replace/gsmx;
29             }
30 4         19 $self->{raw} =~ s/[{][[:upper:]_]+[}]//gsmx;
31 4         16 $self = WebService::Hexonet::Connector::ResponseTemplate->new( $self->{raw} );
32             }
33 51         149 $self = bless $self, $class;
34 51         155 $self->{command} = $cmd;
35 51 100       185 if ( defined $self->{command}->{PASSWORD} ) {
36             # make password no longer accessible
37 1         4 $self->{command}->{PASSWORD} = '***';
38             }
39 51         137 $self->{columnkeys} = [];
40 51         154 $self->{columns} = [];
41 51         136 $self->{records} = [];
42 51         138 $self->{recordIndex} = 0;
43              
44 51         208 my $h = $self->getHash();
45 51 100       145 if ( defined $h->{PROPERTY} ) {
46 33         78 my @keys = keys %{ $h->{PROPERTY} };
  33         177  
47 33         89 my $count = 0;
48 33         101 foreach my $key (@keys) {
49 155         244 my @d = @{ $h->{PROPERTY}->{$key} };
  155         681  
50 155         467 $self->addColumn( $key, @d );
51 155         246 my $len = scalar @d;
52 155 100       420 if ( $len > $count ) {
53 50         203 $count = $len;
54             }
55             }
56 33         81 $count--;
57 33         135 for my $i ( 0 .. $count ) {
58 3124         5170 my %d = ();
59 3124         5383 foreach my $colkey (@keys) {
60 18682         32780 my $col = $self->getColumn($colkey);
61 18682 50       38999 if ( defined $col ) {
62 18682         40004 my $v = $col->getDataByIndex($i);
63 18682 100       39724 if ( defined $v ) {
64 3251         8452 $d{$colkey} = $v;
65             }
66             }
67             }
68 3124         6254 $self->addRecord( \%d );
69             }
70             }
71 51         703 return $self;
72             }
73              
74              
75             sub addColumn {
76 155     155 1 663 my ( $self, $key, @data ) = @_;
77 155         229 push @{ $self->{columns} }, WebService::Hexonet::Connector::Column->new( $key, @data );
  155         606  
78 155         274 push @{ $self->{columnkeys} }, $key;
  155         311  
79 155         369 return $self;
80             }
81              
82              
83             sub addRecord {
84 3124     3124 1 5597 my ( $self, $h ) = @_;
85 3124         4866 push @{ $self->{records} }, WebService::Hexonet::Connector::Record->new($h);
  3124         8148  
86 3124         6782 return $self;
87             }
88              
89              
90             sub getColumn {
91 18784     18784 1 34792 my ( $self, $key ) = @_;
92 18784 100       33037 if ( $self->_hasColumn($key) ) {
93 18767     65603   117106 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  65603         97717  
  18767         41989  
94 18767         53812 return $self->{columns}[ $idx ];
95             }
96 17         113 return;
97             }
98              
99              
100             sub getColumnIndex {
101 2     2 1 13 my ( $self, $key, $idx ) = @_;
102 2         5 my $col = $self->getColumn($key);
103 2 100       8 return $col->getDataByIndex($idx) if defined $col;
104 1         4 return;
105             }
106              
107              
108             sub getColumnKeys {
109 3     3 1 578 my $self = shift;
110 3         6 return \@{ $self->{columnkeys} };
  3         12  
111             }
112              
113              
114             sub getColumns {
115 1     1 0 6 my $self = shift;
116 1         3 return \@{ $self->{columns} };
  1         5  
117             }
118              
119              
120             sub getCommand {
121 8     8 1 53 my $self = shift;
122 8         34 return $self->{command};
123             }
124              
125              
126             sub getCommandPlain {
127 4     4 1 23 my $self = shift;
128 4         11 my $str = q{};
129 4         31 foreach my $key ( sort keys %{ $self->{command} } ) {
  4         27  
130 10         24 my $val = $self->{command}->{$key};
131 10         32 $str .= "${key} = ${val}\n";
132             }
133 4         20 return $str;
134             }
135              
136              
137             sub getCurrentPageNumber {
138 19     19 1 39 my $self = shift;
139 19         41 my $first = $self->getFirstRecordIndex();
140 19         45 my $limit = $self->getRecordsLimitation();
141 19 100 66     66 if ( defined $first && $limit > 0 ) {
142 14         64 return floor( $first / $limit ) + 1;
143             }
144 5         13 return $INDEX_NOT_FOUND;
145             }
146              
147              
148             sub getCurrentRecord {
149 2     2 1 12 my $self = shift;
150 2 100       6 return $self->{records}[ $self->{recordIndex} ]
151             if $self->_hasCurrentRecord();
152 1         5 return;
153             }
154              
155              
156             sub getFirstRecordIndex {
157 28     28 1 57 my $self = shift;
158 28         57 my $col = $self->getColumn('FIRST');
159 28 100       79 if ( defined $col ) {
160 21         60 my $f = $col->getDataByIndex(0);
161 21 50       59 if ( defined $f ) {
162 21         62 return int $f;
163             }
164             }
165 7         9 my $len = scalar @{ $self->{records} };
  7         15  
166 7 100       21 return 0 if ( $len > 0 );
167 6         14 return;
168             }
169              
170              
171             sub getLastRecordIndex {
172 9     9 1 46 my $self = shift;
173 9         25 my $col = $self->getColumn('LAST');
174 9 100       26 if ( defined $col ) {
175 7         24 my $l = $col->getDataByIndex(0);
176 7 50       20 if ( defined $l ) {
177 7         30 return int $l;
178             }
179             }
180 2         5 my $len = $self->getRecordsCount();
181 2 100       7 if ( $len > 0 ) {
182 1         7 return ( $len - 1 );
183             }
184 1         5 return;
185             }
186              
187              
188             sub getListHash {
189 1     1 1 7 my $self = shift;
190 1         3 my @lh = ();
191 1         2 foreach my $rec ( @{ $self->getRecords() } ) {
  1         4  
192 2         6 push @lh, $rec->getData();
193             }
194 1         7 my $r = {
195             LIST => \@lh,
196             meta => {
197             columns => $self->getColumnKeys(),
198             pg => $self->getPagination()
199             }
200             };
201 1         5 return $r;
202             }
203              
204              
205             sub getNextRecord {
206 5     5 1 17 my $self = shift;
207 5 100       13 return $self->{records}[ ++$self->{recordIndex} ]
208             if ( $self->_hasNextRecord() );
209 2         10 return;
210             }
211              
212              
213             sub getNextPageNumber {
214 5     5 1 17 my $self = shift;
215 5         13 my $cp = $self->getCurrentPageNumber();
216 5 100       18 if ( $cp < 0 ) {
217 1         3 return $INDEX_NOT_FOUND;
218             }
219 4         9 my $page = $cp + 1;
220 4         12 my $pages = $self->getNumberOfPages();
221 4 50       18 return $page if ( $page <= $pages );
222 0         0 return $pages;
223             }
224              
225              
226             sub getNumberOfPages {
227 9     9 1 19 my $self = shift;
228 9         18 my $t = $self->getRecordsTotalCount();
229 9         20 my $limit = $self->getRecordsLimitation();
230 9 100 66     32 if ( $t > 0 && $limit > 0 ) {
231 8         36 return ceil( $t / $limit );
232             }
233 1         5 return 0;
234             }
235              
236              
237             sub getPagination {
238 3     3 1 550 my $self = shift;
239 3         8 my $r = {
240             COUNT => $self->getRecordsCount(),
241             CURRENTPAGE => $self->getCurrentPageNumber(),
242             FIRST => $self->getFirstRecordIndex(),
243             LAST => $self->getLastRecordIndex(),
244             LIMIT => $self->getRecordsLimitation(),
245             NEXTPAGE => $self->getNextPageNumber(),
246             PAGES => $self->getNumberOfPages(),
247             PREVIOUSPAGE => $self->getPreviousPageNumber(),
248             TOTAL => $self->getRecordsTotalCount()
249             };
250 3         26 return $r;
251             }
252              
253              
254             sub getPreviousPageNumber {
255 5     5 1 11 my $self = shift;
256 5         12 my $cp = $self->getCurrentPageNumber();
257 5 100       15 if ( $cp < 0 ) {
258 1         3 return $INDEX_NOT_FOUND;
259             }
260 4         7 my $np = $cp - 1;
261 4 50       8 return $np if ( $np > 0 );
262 4         11 return $INDEX_NOT_FOUND;
263             }
264              
265              
266             sub getPreviousRecord {
267 4     4 1 16 my $self = shift;
268 4 100       9 return $self->{records}[ --$self->{recordIndex} ]
269             if ( $self->_hasPreviousRecord() );
270 3         16 return;
271             }
272              
273              
274             sub getRecord {
275 3     3 1 14 my ( $self, $idx ) = @_;
276 3 50 33     24 if ( $idx >= 0 && $self->getRecordsCount() > $idx ) {
277 3         24 return $self->{records}[ $idx ];
278             }
279 0         0 return;
280             }
281              
282              
283             sub getRecords {
284 1     1 1 3 my $self = shift;
285 1         2 return \@{ $self->{records} };
  1         5  
286             }
287              
288              
289             sub getRecordsCount {
290 19     19 1 43 my $self = shift;
291 19         41 my $len = scalar @{ $self->{records} };
  19         44  
292 19         84 return $len;
293             }
294              
295              
296             sub getRecordsTotalCount {
297 18     18 1 44 my $self = shift;
298 18         1003 my $col = $self->getColumn('TOTAL');
299 18 100       79 if ( defined $col ) {
300 17         54 my $t = $col->getDataByIndex(0);
301 17 50       52 if ( defined $t ) {
302 17         77 return int $t;
303             }
304             }
305 1         4 return $self->getRecordsCount();
306             }
307              
308              
309             sub getRecordsLimitation {
310 41     41 1 80 my $self = shift;
311 41         86 my $col = $self->getColumn('LIMIT');
312 41 100       104 if ( defined $col ) {
313 35         92 my $l = $col->getDataByIndex(0);
314 35 50       88 if ( defined $l ) {
315 35         102 return int $l;
316             }
317             }
318 6         15 return $self->getRecordsCount();
319             }
320              
321              
322             sub hasNextPage {
323 2     2 1 10 my $self = shift;
324 2         5 my $cp = $self->getCurrentPageNumber();
325 2 100       12 if ( $cp < 0 ) {
326 1         5 return 0;
327             }
328 1         3 my $np = $cp + 1;
329 1 50       3 if ( $np <= $self->getNumberOfPages() ) {
330 1         6 return 1;
331             }
332 0         0 return 0;
333             }
334              
335              
336             sub hasPreviousPage {
337 2     2 1 11 my $self = shift;
338 2         5 my $cp = $self->getCurrentPageNumber();
339 2 100       11 if ( $cp < 0 ) {
340 1         5 return 0;
341             }
342 1         2 my $pp = $cp - 1;
343 1 50       4 if ( $pp > 0 ) {
344 0         0 return 1;
345             }
346 1         5 return 0;
347             }
348              
349              
350             sub rewindRecordList {
351 1     1 1 3 my $self = shift;
352 1         2 $self->{recordIndex} = 0;
353 1         3 return $self;
354             }
355              
356              
357             sub _hasColumn {
358 18784     18784   31262 my ( $self, $key ) = @_;
359 18784     65611   45981 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  65611         97937  
  18784         42956  
360 18784         58691 return ( $idx > $INDEX_NOT_FOUND );
361             }
362              
363              
364             sub _hasCurrentRecord {
365 8     8   13 my $self = shift;
366 8         14 my $len = scalar @{ $self->{records} };
  8         14  
367 8   66     81 return ( $len > 0 && $self->{recordIndex} >= 0 && $self->{recordIndex} < $len );
368             }
369              
370              
371             sub _hasNextRecord {
372 5     5   10 my $self = shift;
373 5         11 my $next = $self->{recordIndex} + 1;
374 5         8 my $len = scalar @{ $self->{records} };
  5         10  
375 5   66     12 return ( $self->_hasCurrentRecord() && $next < $len );
376             }
377              
378              
379             sub _hasPreviousRecord {
380 4     4   6 my $self = shift;
381 4   66     24 return ( $self->{recordIndex} > 0 && $self->_hasCurrentRecord() );
382             }
383              
384             1;
385              
386             __END__