Broadworks/OCIP/Response.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 118 CC 3 SCAR 14.6
CC
3
Cov
0%
_list
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 4.3
1 package Broadworks::OCIP::Response;
2  
3 # ABSTRACT: A Broadworks OCI-P Response Message
4  
5 use strict;
6 use warnings;
7 use utf8;
8 use feature 'unicode_strings';
9  
10 our $VERSION = '0.08'; # VERSION
11 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
12  
13 use Broadworks::OCIP::Throwable;
14 use Moose;
15 use Method::Signatures;
16 use MooseX::StrictConstructor;
17 use Try::Tiny;
18 use XML::Fast;
19  
20  
21 # ------------------------------------------------------------------------
22 sub _list {
23 return () unless ( defined( $_[0] ) );
24 return @{ $_[0] } if ( ref( $_[0] ) eq 'ARRAY' );
25 return $_[0];
26 }
27  
28 # ------------------------------------------------------------------------
29  
30  
31 has xml => (
32 is => 'ro',
33 isa => 'Str',
34 required => 1,
35 );
36  
37 # ------------------------------------------------------------------------
38  
39  
40 has expected => (
41 is => 'ro',
42 isa => 'Str',
43 required => 1,
44 );
45  
46 # ------------------------------------------------------------------------
47  
48  
49 has die_on_error => (
50 is => 'ro',
51 isa => 'Bool',
52 required => 1,
53 default => 1,
54 );
55  
56 # ------------------------------------------------------------------------
57  
58  
59 has hash => (
60 is => 'ro',
61 isa => 'HashRef',
62 lazy => 1,
63 builder => '_build_hash'
64 );
65  
66 method _build_hash () {
67 my $hash;
68 try { $hash = XML::Fast::xml2hash( $self->xml ); }
69 catch {
70 Broadworks::OCIP::Throwable->throw(
71 message => sprintf( "Error on XML unpack %s with %s", $_, $self->xml ),
72 execution_phase => 'response',
73 error_code => 'xml_unpack'
74 );
75 };
76  
77 Broadworks::OCIP::Throwable->throw(
78 message => sprintf( "Nothing returned from parse - received XML was [%s]", $self->xml ),
79 execution_phase => 'response',
80 error_code => 'xml_null'
81 ) unless ( $hash and $hash->{BroadsoftDocument} and ( $hash->{BroadsoftDocument}{-protocol} eq 'OCI' ) );
82  
83 return $hash->{BroadsoftDocument};
84 }
85  
86 # ------------------------------------------------------------------------
87  
88  
89 has type => (
90 is => 'ro',
91 isa => 'Str',
92 lazy => 1,
93 builder => '_build_type',
94 weak_ref => 1
95 );
96 method _build_type () { return ( $self->payload->{'-xsi:type'} ); }
97  
98 # ------------------------------------------------------------------------
99  
100  
101 has status_ok => (
102 is => 'ro',
103 isa => 'Bool',
104 lazy => 1,
105 builder => '_build_status_ok'
106 );
107 method _build_status_ok () { return ( ( $self->type eq $self->expected ) ? 1 : 0 ); }
108  
109 # ------------------------------------------------------------------------
110  
111  
112 has payload => (
113 is => 'ro',
114 isa => 'HashRef',
115 lazy => 1,
116 builder => '_build_payload',
117 weak_ref => 1
118 );
119 method _build_payload () { return $self->hash->{command}; }
120  
121 # ------------------------------------------------------------------------
122  
123  
124 has tables => (
125 is => 'ro',
126 isa => 'HashRef',
127 lazy => 1,
128 builder => '_build_tables',
129 );
130  
131 method _build_tables () {
132 my $tables = {};
133 while ( my ( $k, $v ) = each %{ $self->payload } ) {
134 if ( $k =~ /Table$/ ) {
135 my $table = [];
136 $tables->{$k} = $table;
137 my @cols = _list( $v->{colHeading} );
138 foreach my $row ( _list( $v->{row} ) ) {
139 push( @{$table}, { map { $cols[$_] => $row->{col}->[$_] } 0 .. $#cols } );
140 }
141 }
142 }
143 return $tables;
144 }
145  
146 # ------------------------------------------------------------------------
147  
148  
149 method table ($table_name) { return ( @{ $self->tables->{$table_name} || [] } ); }
150  
151 # ------------------------------------------------------------------------
152 method BUILD ($args) {
153  
154 # check this object is valid and the right type
155 Broadworks::OCIP::Throwable->throw(
156 message =>
157 sprintf( "Expecting a response of type %s but got %s\n%s\n", $self->expected, $self->type, $self->xml ),
158 execution_phase => 'response',
159 error_code => 'ocip_unexpected'
160 ) if ( $self->die_on_error and not $self->status_ok );
161 }
162  
163 # ------------------------------------------------------------------------
164 method list ($key) {
165 my $val = $self->payload->{$key};
166 return () unless ( defined($val) );
167 return @{$val} if ( ref($val) eq 'ARRAY' );
168 return $val;
169 }
170  
171 # ------------------------------------------------------------------------
172  
173 no Moose;
174 __PACKAGE__->meta->make_immutable;
175 1;
176  
177 __END__
178  
179 =pod
180  
181 =encoding UTF-8
182  
183 =head1 NAME
184  
185 Broadworks::OCIP::Response - A Broadworks OCI-P Response Message
186  
187 =head1 VERSION
188  
189 version 0.08
190  
191 =for test_synopsis 1;
192 __END__
193  
194 =for stopwords NIGELM
195  
196 =for Pod::Coverage mvp_multivalue_args
197  
198 =head1 SYNOPSIS
199  
200 use Broadworks::OCIP::Response;
201  
202 my $res = Broadworks::OCIP::Response->new(
203 xml => string,
204 expected => 'SuccessResponse' );
205 $res->status_ok;
206  
207 =head1 DESCRIPTION
208  
209 Unpacks an XML document sent as a reply and allows its manipulation.
210  
211 =head2 Required Parameters
212  
213 =head3 xml
214  
215 The XML document returned. This should be a perl string - not encoded.
216  
217 =head3 expected
218  
219 The response type we are expecting. This is used to populate C<status_ok>. If
220 C<die_on_error> is set an exception will be thrown if the received document is
221 not of this type.
222  
223 =head3 die_on_error
224  
225 Whether we throw toys out of pram on unexpected input.
226  
227 =head3 hash
228  
229 A perl hash representation of the XML.
230  
231 =head3 type
232  
233 The type of the returned XML.
234  
235 =head3 status_ok
236  
237 Is this the expected return type.
238  
239 =head3 payload
240  
241 The hash payload of the XML document.
242  
243 =head3 tables
244  
245 Any tables that are in the returned data. This returns a hash of tables named
246 by the table name.
247  
248 Each table is an array of rows, each of which is a hash.
249  
250 =head3 table
251  
252 Returns the content of a single named table, as a list
253  
254 =head1 AUTHOR
255  
256 Nigel Metheringham <Nigel.Metheringham@redcentricplc.com>
257  
258 =head1 COPYRIGHT
259  
260 Copyright 2014 Recentric Solutions Limited. All rights reserved.
261  
262 =head1 AUTHOR
263  
264 Nigel Metheringham <nigelm@cpan.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2018 by Nigel Metheringham.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut