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   19 use 5.026_000;
  1         4  
4 1     1   31 use strict;
  1         3  
  1         27  
5 1     1   5 use warnings;
  1         2  
  1         27  
6 1     1   676 use WebService::Hexonet::Connector::Column;
  1         2  
  1         31  
7 1     1   655 use WebService::Hexonet::Connector::Record;
  1         3  
  1         34  
8 1     1   468 use parent qw(WebService::Hexonet::Connector::ResponseTemplate);
  1         313  
  1         6  
9 1     1   46 use POSIX qw(ceil floor);
  1         2  
  1         7  
10 1     1   720 use List::MoreUtils qw(first_index);
  1         13007  
  1         7  
11 1     1   1070 use Readonly;
  1         2  
  1         74  
12             Readonly my $INDEX_NOT_FOUND => -1;
13              
14 1     1   7 use version 0.9917; our $VERSION = version->declare('v2.9.2');
  1         18  
  1         6  
15              
16              
17             sub new {
18 49     49 1 28299 my ( $class, $raw, $cmd, $ph ) = @_;
19 49         274 my $self = WebService::Hexonet::Connector::ResponseTemplate->new($raw);
20             # care about getting placeholder variables replaced
21 49 100       264 if ( $self->{raw} =~ /[{][[:upper:]_]+[}]/gsmx ) {
22 4 100       16 if ( !defined $ph ) {
23 3         7 $ph = {};
24             }
25 4         6 foreach my $key ( keys %{$ph} ) {
  4         16  
26 1         4 my $find = "[{]${key}[}]";
27 1         4 my $replace = $ph->{$key};
28 1         31 $self->{raw} =~ s/$find/$replace/gsmx;
29             }
30 4         22 $self->{raw} =~ s/[{][[:upper:]_]+[}]//gsmx;
31 4         16 $self = WebService::Hexonet::Connector::ResponseTemplate->new( $self->{raw} );
32             }
33 49         118 $self = bless $self, $class;
34 49         130 $self->{command} = $cmd;
35 49 100       181 if ( defined $self->{command}->{PASSWORD} ) {
36             # make password no longer accessible
37 1         5 $self->{command}->{PASSWORD} = '***';
38             }
39 49         122 $self->{columnkeys} = [];
40 49         155 $self->{columns} = [];
41 49         145 $self->{records} = [];
42 49         123 $self->{recordIndex} = 0;
43              
44 49         193 my $h = $self->getHash();
45 49 100       161 if ( defined $h->{PROPERTY} ) {
46 32         75 my @keys = keys %{ $h->{PROPERTY} };
  32         173  
47 32         88 my $count = 0;
48 32         93 foreach my $key (@keys) {
49 154         234 my @d = @{ $h->{PROPERTY}->{$key} };
  154         699  
50 154         444 $self->addColumn( $key, @d );
51 154         254 my $len = scalar @d;
52 154 100       403 if ( $len > $count ) {
53 56         210 $count = $len;
54             }
55             }
56 32         63 $count--;
57 32         135 for my $i ( 0 .. $count ) {
58 3245         4999 my %d = ();
59 3245         5604 foreach my $colkey (@keys) {
60 19413         35801 my $col = $self->getColumn($colkey);
61 19413 50       39959 if ( defined $col ) {
62 19413         42008 my $v = $col->getDataByIndex($i);
63 19413 100       42694 if ( defined $v ) {
64 3372         8588 $d{$colkey} = $v;
65             }
66             }
67             }
68 3245         6805 $self->addRecord( \%d );
69             }
70             }
71 49         702 return $self;
72             }
73              
74              
75             sub addColumn {
76 154     154 1 691 my ( $self, $key, @data ) = @_;
77 154         226 push @{ $self->{columns} }, WebService::Hexonet::Connector::Column->new( $key, @data );
  154         582  
78 154         271 push @{ $self->{columnkeys} }, $key;
  154         324  
79 154         365 return $self;
80             }
81              
82              
83             sub addRecord {
84 3245     3245 1 5701 my ( $self, $h ) = @_;
85 3245         4840 push @{ $self->{records} }, WebService::Hexonet::Connector::Record->new($h);
  3245         8611  
86 3245         7210 return $self;
87             }
88              
89              
90             sub getColumn {
91 19514     19514 1 34227 my ( $self, $key ) = @_;
92 19514 100       35987 if ( $self->_hasColumn($key) ) {
93 19497     68176   120581 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  68176         103741  
  19497         44234  
94 19497         53909 return $self->{columns}[ $idx ];
95             }
96 17         125 return;
97             }
98              
99              
100             sub getColumnIndex {
101 2     2 1 12 my ( $self, $key, $idx ) = @_;
102 2         6 my $col = $self->getColumn($key);
103 2 100       12 return $col->getDataByIndex($idx) if defined $col;
104 1         5 return;
105             }
106              
107              
108             sub getColumnKeys {
109 3     3 1 593 my $self = shift;
110 3         7 return \@{ $self->{columnkeys} };
  3         14  
111             }
112              
113              
114             sub getColumns {
115 1     1 0 6 my $self = shift;
116 1         7 return \@{ $self->{columns} };
  1         3  
117             }
118              
119              
120             sub getCommand {
121 8     8 1 51 my $self = shift;
122 8         33 return $self->{command};
123             }
124              
125              
126             sub getCommandPlain {
127 4     4 1 33 my $self = shift;
128 4         13 my $str = q{};
129 4         50 foreach my $key ( sort keys %{ $self->{command} } ) {
  4         36  
130 10         23 my $val = $self->{command}->{$key};
131 10         32 $str .= "${key} = ${val}\n";
132             }
133 4         21 return $str;
134             }
135              
136              
137             sub getCurrentPageNumber {
138 19     19 1 58 my $self = shift;
139 19         42 my $first = $self->getFirstRecordIndex();
140 19         47 my $limit = $self->getRecordsLimitation();
141 19 100 66     64 if ( defined $first && $limit > 0 ) {
142 14         82 return floor( $first / $limit ) + 1;
143             }
144 5         10 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 62 my $self = shift;
158 28         64 my $col = $self->getColumn('FIRST');
159 28 100       79 if ( defined $col ) {
160 21         54 my $f = $col->getDataByIndex(0);
161 21 50       49 if ( defined $f ) {
162 21         68 return int $f;
163             }
164             }
165 7         11 my $len = scalar @{ $self->{records} };
  7         17  
166 7 100       23 return 0 if ( $len > 0 );
167 6         12 return;
168             }
169              
170              
171             sub getLastRecordIndex {
172 9     9 1 26 my $self = shift;
173 9         22 my $col = $self->getColumn('LAST');
174 9 100       28 if ( defined $col ) {
175 7         26 my $l = $col->getDataByIndex(0);
176 7 50       21 if ( defined $l ) {
177 7         47 return int $l;
178             }
179             }
180 2         5 my $len = $self->getRecordsCount();
181 2 100       7 if ( $len > 0 ) {
182 1         6 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         3 foreach my $rec ( @{ $self->getRecords() } ) {
  1         4  
192 2         7 push @lh, $rec->getData();
193             }
194 1         6 my $r = {
195             LIST => \@lh,
196             meta => {
197             columns => $self->getColumnKeys(),
198             pg => $self->getPagination()
199             }
200             };
201 1         6 return $r;
202             }
203              
204              
205             sub getNextRecord {
206 5     5 1 20 my $self = shift;
207 5 100       15 return $self->{records}[ ++$self->{recordIndex} ]
208             if ( $self->_hasNextRecord() );
209 2         8 return;
210             }
211              
212              
213             sub getNextPageNumber {
214 5     5 1 24 my $self = shift;
215 5         13 my $cp = $self->getCurrentPageNumber();
216 5 100       18 if ( $cp < 0 ) {
217 1         4 return $INDEX_NOT_FOUND;
218             }
219 4         8 my $page = $cp + 1;
220 4         9 my $pages = $self->getNumberOfPages();
221 4 50       24 return $page if ( $page <= $pages );
222 0         0 return $pages;
223             }
224              
225              
226             sub getNumberOfPages {
227 9     9 1 20 my $self = shift;
228 9         19 my $t = $self->getRecordsTotalCount();
229 9         20 my $limit = $self->getRecordsLimitation();
230 9 100 66     35 if ( $t > 0 && $limit > 0 ) {
231 8         33 return ceil( $t / $limit );
232             }
233 1         5 return 0;
234             }
235              
236              
237             sub getPagination {
238 3     3 1 548 my $self = shift;
239 3         9 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 15 my $self = shift;
256 5         11 my $cp = $self->getCurrentPageNumber();
257 5 100       18 if ( $cp < 0 ) {
258 1         3 return $INDEX_NOT_FOUND;
259             }
260 4         8 my $np = $cp - 1;
261 4 50       9 return $np if ( $np > 0 );
262 4         10 return $INDEX_NOT_FOUND;
263             }
264              
265              
266             sub getPreviousRecord {
267 4     4 1 16 my $self = shift;
268 4 100       10 return $self->{records}[ --$self->{recordIndex} ]
269             if ( $self->_hasPreviousRecord() );
270 3         16 return;
271             }
272              
273              
274             sub getRecord {
275 2     2 1 8 my ( $self, $idx ) = @_;
276 2 50 33     17 if ( $idx >= 0 && $self->getRecordsCount() > $idx ) {
277 2         15 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         3 return \@{ $self->{records} };
  1         4  
286             }
287              
288              
289             sub getRecordsCount {
290 18     18 1 40 my $self = shift;
291 18         28 my $len = scalar @{ $self->{records} };
  18         42  
292 18         69 return $len;
293             }
294              
295              
296             sub getRecordsTotalCount {
297 18     18 1 47 my $self = shift;
298 18         1161 my $col = $self->getColumn('TOTAL');
299 18 100       49 if ( defined $col ) {
300 17         47 my $t = $col->getDataByIndex(0);
301 17 50       49 if ( defined $t ) {
302 17         82 return int $t;
303             }
304             }
305 1         9 return $self->getRecordsCount();
306             }
307              
308              
309             sub getRecordsLimitation {
310 41     41 1 71 my $self = shift;
311 41         87 my $col = $self->getColumn('LIMIT');
312 41 100       117 if ( defined $col ) {
313 35         93 my $l = $col->getDataByIndex(0);
314 35 50       82 if ( defined $l ) {
315 35         101 return int $l;
316             }
317             }
318 6         21 return $self->getRecordsCount();
319             }
320              
321              
322             sub hasNextPage {
323 2     2 1 13 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         7 my $np = $cp + 1;
329 1 50       3 if ( $np <= $self->getNumberOfPages() ) {
330 1         5 return 1;
331             }
332 0         0 return 0;
333             }
334              
335              
336             sub hasPreviousPage {
337 2     2 1 20 my $self = shift;
338 2         6 my $cp = $self->getCurrentPageNumber();
339 2 100       11 if ( $cp < 0 ) {
340 1         5 return 0;
341             }
342 1         3 my $pp = $cp - 1;
343 1 50       5 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         4 return $self;
354             }
355              
356              
357             sub _hasColumn {
358 19514     19514   32468 my ( $self, $key ) = @_;
359 19514     68184   49178 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  68184         102541  
  19514         45023  
360 19514         61305 return ( $idx > $INDEX_NOT_FOUND );
361             }
362              
363              
364             sub _hasCurrentRecord {
365 8     8   12 my $self = shift;
366 8         15 my $len = scalar @{ $self->{records} };
  8         14  
367 8   66     84 return ( $len > 0 && $self->{recordIndex} >= 0 && $self->{recordIndex} < $len );
368             }
369              
370              
371             sub _hasNextRecord {
372 5     5   8 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   7 my $self = shift;
381 4   66     23 return ( $self->{recordIndex} > 0 && $self->_hasCurrentRecord() );
382             }
383              
384             1;
385              
386             __END__