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         3  
4 1     1   28 use strict;
  1         4  
  1         28  
5 1     1   7 use warnings;
  1         3  
  1         25  
6 1     1   683 use WebService::Hexonet::Connector::Column;
  1         3  
  1         29  
7 1     1   669 use WebService::Hexonet::Connector::Record;
  1         2  
  1         34  
8 1     1   451 use parent qw(WebService::Hexonet::Connector::ResponseTemplate);
  1         309  
  1         5  
9 1     1   47 use POSIX qw(ceil floor);
  1         2  
  1         6  
10 1     1   686 use List::MoreUtils qw(first_index);
  1         13045  
  1         6  
11 1     1   1122 use Readonly;
  1         2  
  1         71  
12             Readonly my $INDEX_NOT_FOUND => -1;
13              
14 1     1   7 use version 0.9917; our $VERSION = version->declare('v2.9.1');
  1         17  
  1         7  
15              
16              
17             sub new {
18 50     50 1 34210 my ( $class, $raw, $cmd, $ph ) = @_;
19 50         298 my $self = WebService::Hexonet::Connector::ResponseTemplate->new($raw);
20             # care about getting placeholder variables replaced
21 50 100       262 if ( $self->{raw} =~ /[{][[:upper:]_]+[}]/gsmx ) {
22 4 100       13 if ( !defined $ph ) {
23 3         7 $ph = {};
24             }
25 4         8 foreach my $key ( keys %{$ph} ) {
  4         13  
26 1         3 my $find = "[{]${key}[}]";
27 1         3 my $replace = $ph->{$key};
28 1         22 $self->{raw} =~ s/$find/$replace/gsmx;
29             }
30 4         20 $self->{raw} =~ s/[{][[:upper:]_]+[}]//gsmx;
31 4         14 $self = WebService::Hexonet::Connector::ResponseTemplate->new( $self->{raw} );
32             }
33 50         148 $self = bless $self, $class;
34 50         174 $self->{command} = $cmd;
35 50 100       178 if ( defined $self->{command}->{PASSWORD} ) {
36             # make password no longer accessible
37 1         3 $self->{command}->{PASSWORD} = '***';
38             }
39 50         157 $self->{columnkeys} = [];
40 50         159 $self->{columns} = [];
41 50         130 $self->{records} = [];
42 50         131 $self->{recordIndex} = 0;
43              
44 50         196 my $h = $self->getHash();
45 50 100       155 if ( defined $h->{PROPERTY} ) {
46 33         86 my @keys = keys %{ $h->{PROPERTY} };
  33         178  
47 33         96 my $count = 0;
48 33         95 foreach my $key (@keys) {
49 155         244 my @d = @{ $h->{PROPERTY}->{$key} };
  155         737  
50 155         487 $self->addColumn( $key, @d );
51 155         272 my $len = scalar @d;
52 155 100       431 if ( $len > $count ) {
53 54         203 $count = $len;
54             }
55             }
56 33         101 $count--;
57 33         115 for my $i ( 0 .. $count ) {
58 3153         5054 my %d = ();
59 3153         5295 foreach my $colkey (@keys) {
60 18856         32840 my $col = $self->getColumn($colkey);
61 18856 50       39885 if ( defined $col ) {
62 18856         40808 my $v = $col->getDataByIndex($i);
63 18856 100       39228 if ( defined $v ) {
64 3280         8141 $d{$colkey} = $v;
65             }
66             }
67             }
68 3153         6013 $self->addRecord( \%d );
69             }
70             }
71 50         725 return $self;
72             }
73              
74              
75             sub addColumn {
76 155     155 1 1519 my ( $self, $key, @data ) = @_;
77 155         239 push @{ $self->{columns} }, WebService::Hexonet::Connector::Column->new( $key, @data );
  155         645  
78 155         273 push @{ $self->{columnkeys} }, $key;
  155         372  
79 155         379 return $self;
80             }
81              
82              
83             sub addRecord {
84 3153     3153 1 5792 my ( $self, $h ) = @_;
85 3153         4819 push @{ $self->{records} }, WebService::Hexonet::Connector::Record->new($h);
  3153         7975  
86 3153         7039 return $self;
87             }
88              
89              
90             sub getColumn {
91 18958     18958 1 33654 my ( $self, $key ) = @_;
92 18958 100       33333 if ( $self->_hasColumn($key) ) {
93 18941     66248   119386 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  66248         99343  
  18941         43457  
94 18941         52796 return $self->{columns}[ $idx ];
95             }
96 17         109 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       8 return $col->getDataByIndex($idx) if defined $col;
104 1         3 return;
105             }
106              
107              
108             sub getColumnKeys {
109 3     3 1 580 my $self = shift;
110 3         8 return \@{ $self->{columnkeys} };
  3         12  
111             }
112              
113              
114             sub getColumns {
115 1     1 0 7 my $self = shift;
116 1         2 return \@{ $self->{columns} };
  1         4  
117             }
118              
119              
120             sub getCommand {
121 8     8 1 61 my $self = shift;
122 8         35 return $self->{command};
123             }
124              
125              
126             sub getCommandPlain {
127 4     4 1 21 my $self = shift;
128 4         13 my $str = q{};
129 4         43 foreach my $key ( sort keys %{ $self->{command} } ) {
  4         32  
130 10         22 my $val = $self->{command}->{$key};
131 10         34 $str .= "${key} = ${val}\n";
132             }
133 4         20 return $str;
134             }
135              
136              
137             sub getCurrentPageNumber {
138 19     19 1 37 my $self = shift;
139 19         43 my $first = $self->getFirstRecordIndex();
140 19         43 my $limit = $self->getRecordsLimitation();
141 19 100 66     97 if ( defined $first && $limit > 0 ) {
142 14         73 return floor( $first / $limit ) + 1;
143             }
144 5         11 return $INDEX_NOT_FOUND;
145             }
146              
147              
148             sub getCurrentRecord {
149 2     2 1 11 my $self = shift;
150 2 100       7 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       96 if ( defined $col ) {
160 21         63 my $f = $col->getDataByIndex(0);
161 21 50       56 if ( defined $f ) {
162 21         64 return int $f;
163             }
164             }
165 7         13 my $len = scalar @{ $self->{records} };
  7         16  
166 7 100       29 return 0 if ( $len > 0 );
167 6         14 return;
168             }
169              
170              
171             sub getLastRecordIndex {
172 9     9 1 27 my $self = shift;
173 9         22 my $col = $self->getColumn('LAST');
174 9 100       29 if ( defined $col ) {
175 7         25 my $l = $col->getDataByIndex(0);
176 7 50       20 if ( defined $l ) {
177 7         36 return int $l;
178             }
179             }
180 2         5 my $len = $self->getRecordsCount();
181 2 100       7 if ( $len > 0 ) {
182 1         12 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         6 push @lh, $rec->getData();
193             }
194 1         5 my $r = {
195             LIST => \@lh,
196             meta => {
197             columns => $self->getColumnKeys(),
198             pg => $self->getPagination()
199             }
200             };
201 1         7 return $r;
202             }
203              
204              
205             sub getNextRecord {
206 5     5 1 19 my $self = shift;
207 5 100       14 return $self->{records}[ ++$self->{recordIndex} ]
208             if ( $self->_hasNextRecord() );
209 2         9 return;
210             }
211              
212              
213             sub getNextPageNumber {
214 5     5 1 16 my $self = shift;
215 5         13 my $cp = $self->getCurrentPageNumber();
216 5 100       17 if ( $cp < 0 ) {
217 1         4 return $INDEX_NOT_FOUND;
218             }
219 4         11 my $page = $cp + 1;
220 4         10 my $pages = $self->getNumberOfPages();
221 4 50       17 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         26 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 553 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         25 return $r;
251             }
252              
253              
254             sub getPreviousPageNumber {
255 5     5 1 18 my $self = shift;
256 5         10 my $cp = $self->getCurrentPageNumber();
257 5 100       19 if ( $cp < 0 ) {
258 1         3 return $INDEX_NOT_FOUND;
259             }
260 4         22 my $np = $cp - 1;
261 4 50       10 return $np if ( $np > 0 );
262 4         14 return $INDEX_NOT_FOUND;
263             }
264              
265              
266             sub getPreviousRecord {
267 4     4 1 19 my $self = shift;
268 4 100       11 return $self->{records}[ --$self->{recordIndex} ]
269             if ( $self->_hasPreviousRecord() );
270 3         14 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         20 return $self->{records}[ $idx ];
278             }
279 0         0 return;
280             }
281              
282              
283             sub getRecords {
284 1     1 1 2 my $self = shift;
285 1         3 return \@{ $self->{records} };
  1         5  
286             }
287              
288              
289             sub getRecordsCount {
290 19     19 1 40 my $self = shift;
291 19         34 my $len = scalar @{ $self->{records} };
  19         47  
292 19         66 return $len;
293             }
294              
295              
296             sub getRecordsTotalCount {
297 18     18 1 52 my $self = shift;
298 18         1052 my $col = $self->getColumn('TOTAL');
299 18 100       68 if ( defined $col ) {
300 17         60 my $t = $col->getDataByIndex(0);
301 17 50       54 if ( defined $t ) {
302 17         77 return int $t;
303             }
304             }
305 1         3 return $self->getRecordsCount();
306             }
307              
308              
309             sub getRecordsLimitation {
310 41     41 1 76 my $self = shift;
311 41         88 my $col = $self->getColumn('LIMIT');
312 41 100       102 if ( defined $col ) {
313 35         96 my $l = $col->getDataByIndex(0);
314 35 50       98 if ( defined $l ) {
315 35         104 return int $l;
316             }
317             }
318 6         14 return $self->getRecordsCount();
319             }
320              
321              
322             sub hasNextPage {
323 2     2 1 11 my $self = shift;
324 2         5 my $cp = $self->getCurrentPageNumber();
325 2 100       11 if ( $cp < 0 ) {
326 1         6 return 0;
327             }
328 1         3 my $np = $cp + 1;
329 1 50       4 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 21 my $self = shift;
338 2         6 my $cp = $self->getCurrentPageNumber();
339 2 100       15 if ( $cp < 0 ) {
340 1         5 return 0;
341             }
342 1         11 my $pp = $cp - 1;
343 1 50       4 if ( $pp > 0 ) {
344 0         0 return 1;
345             }
346 1         6 return 0;
347             }
348              
349              
350             sub rewindRecordList {
351 1     1 1 2 my $self = shift;
352 1         3 $self->{recordIndex} = 0;
353 1         3 return $self;
354             }
355              
356              
357             sub _hasColumn {
358 18958     18958   31553 my ( $self, $key ) = @_;
359 18958     66256   46182 my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} };
  66256         98958  
  18958         43489  
360 18958         58790 return ( $idx > $INDEX_NOT_FOUND );
361             }
362              
363              
364             sub _hasCurrentRecord {
365 8     8   15 my $self = shift;
366 8         24 my $len = scalar @{ $self->{records} };
  8         20  
367 8   66     81 return ( $len > 0 && $self->{recordIndex} >= 0 && $self->{recordIndex} < $len );
368             }
369              
370              
371             sub _hasNextRecord {
372 5     5   8 my $self = shift;
373 5         10 my $next = $self->{recordIndex} + 1;
374 5         9 my $len = scalar @{ $self->{records} };
  5         11  
375 5   66     11 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__