Edgecast.pm

filter: Statementstmt 36.3% 36 / 99 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.9% 9 / 17 Podpod n/a totaltotal 32.6% 45 / 138 CC 14 SCAR 28.4
CC
14
Cov
30%
_do_call
56.0
_parse_xml
12.0
_build_interfaces
12.0
CRAP 17.1
↑ Summary Edgecast/auto/RealTime/Interface/EdgeCastWebServices/EdgeCastWebServicesSoap.pm »
1 package WebService::Edgecast;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $WebService::Edgecast::VERSION = '0.01.00';
4 }
5  
6 # ABSTRACT: Perl interface to Edgecast's SOAP API
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # XXX Commened out becuase it breaks builds on some Debian lenny boxes
13  
14 # use metaclass (
15 # metaclass => 'Moose::Meta::Class',
16 # error_class => 'Moose::Error::Confess',
17 # );
18 }
19  
20 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Module::Pluggable search_path => [ __PACKAGE__ . '::auto' ];
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 has 'email' => ( is => 'ro', isa => 'Str', required => 1, );
26 1 has 'password' => ( is => 'ro', isa => 'Str', required => 1, );
27 1 has 'credentials' => ( is => 'ro', isa => 'Str', lazy_build => 1, );
28 1 has 'soap' => ( is => 'rw', isa => 'Any', lazy_build => 1, );
29  
30 1 has 'uri' => (
31 is => 'ro',
32 isa => 'Str',
33 default => 'EC:WebServices',
34 );
35  
36 1 has 'error' => (
37 is => 'rw',
38 isa => 'Str|Undef',
39 clearer => 'clear_error',
40 predicate => 'has_error',
41 );
42  
43 1 has 'interfaces' => (
44 is => 'ro',
45 isa => 'HashRef[Str]',
46 lazy_build => 1,
47 );
48  
49 1 has 'client_type' => (
50 is => 'rw',
51 isa => 'Str',
52 default => 'p',
53 );
54  
55 1 has 'security_type' => (
56 is => 'rw',
57 isa => 'Str',
58 default => 'bas',
59 );
60  
61 1 has 'api_type' => (
62 is => 'rw',
63 isa => 'Str',
64 writer => 'set_api_type',
65 trigger => \&_validate_api_type,
66 );
67  
68 has 'api_types' => (
69 is => 'ro',
70 isa => 'ArrayRef[Str]',
71 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 [
73 0 qw/
74 administration
75 realtime
76 mediamanager
77 reporting
78 /
79 ];
80 },
81 1 );
82  
83 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 1 our $AUTOLOAD;
86  
87 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
88 0 my $self = shift;
89  
90 0 ▶ unless ( $self->api_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->api_type) 0 0 0
91 0 croak 'To make API calls you must set an API type in the '
92 . 'constructor, or using the set_api_type() method once '
93 . 'the object has been instantiated.';
94 }
95  
96 0 ( my $method = $AUTOLOAD ) =~ s/.*\::(.+)$/$1/;
97  
98 0 $self->_do_call( $method, @_ );
99 }
100  
101 0 ▶sub _do_call {
Subroutine _do_call: not called CC=7 SCAR=40.3
102 0 my $self = shift;
103 0 my $method = shift;
104 0 my $args = shift;
105 0 my $raw = delete $args->{'raw'};
106 0 my $params = { strCredential => $self->credentials };
107 0 my $get = "get_$method".'Result';
108 0 my $fault = 'get_faultstring';
109  
110 0 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 0 0 0
111 0 foreach my $key ( keys %$args ) {
112 0 $params->{$key} = delete $args->{$key};
113 }
114 }
115  
116 0 ▶ unless ( $self->soap->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->soap->can($method)) 0 0 0
117 0 $self->error(
118 "Method '$method' is invalid. Please see the perldoc for package "
119 . ref($self->soap) . ' for a list of all valid methods'
120 );
121 0 return;
122 }
123  
124 0 my $result = $self->soap->$method($params);
125  
126 0 ▶ if ( $result->can($fault) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->can($fault)) { } 0 0 0
127 0 my $fault_str = $result->$fault;
128 0 $self->error("$fault_str");
129 0 return;
130 } else {
131 0 ▶ $self->clear_error if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
132 0 ▶ return $raw ? $result : $self->_parse_xml($result->$get);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 0 0 0
133 }
134 }
135  
136 0 ▶sub _validate_api_type {
Subroutine _validate_api_type: not called CC=2 SCAR=17.9
137 0 my $self = shift;
138 0 my $api_type = shift;
139 0 my $regexp = join '|', @{ $self->api_types };
140 0 my $fmt = join "\n", map { "\t" . ucfirst( lc $_ ) } @{ $self->api_types };
141  
142 0 ▶ unless ( $api_type =~ /^($regexp)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api_type =~ /^($regexp)$/i) 0 0 0
143 0 croak "API type '$api_type' is unsupported. The following are "
144 . "valid types:\n$fmt\n";
145 }
146  
147 # (re)build our soap object with the new api_type
148 0 $self->soap($self->_build_soap);
149 }
150  
151 0 ▶sub _parse_xml {
Subroutine _parse_xml: not called CC=3 SCAR=24.8
152 0 my $self = shift;
153 0 my $xml = shift;
154  
155 0 ▶ return unless defined $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml 0 0 0
156  
157 0 ▶ unless ($xml =~ /^\<opt\>.*\<\/opt\>$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml =~ m[^\<opt\>.*\</opt\>$]) 0 0 0
158 0 $xml = "<opt>$xml</opt>";
159 }
160  
161 0 return XMLin($xml);
162 }
163  
164 0 ▶sub _build_credentials {
Subroutine _build_credentials: not called CC=1 SCAR=6.9
165 0 my $self = shift;
166  
167 return join( ':',
168 0 map { $self->$_ } qw/ client_type security_type email password / );
169 }
170  
171 0 ▶sub _build_interfaces {
Subroutine _build_interfaces: not called CC=3 SCAR=24.8
172 0 my $self = shift;
173 0 my %out = ();
174  
175 0 foreach my $type ( @{ $self->api_types } ) {
176 0 my $module =
177 ( grep /$type/i, grep /::Interface::/, $self->plugins )[0];
178  
179 #XXX Probably want to warn here
180 0 ▶ next unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
181  
182 0 $out{ lc($type) } = $module;
183 }
184  
185 0 return {%out};
186 }
187  
188 0 ▶sub _build_soap {
Subroutine _build_soap: not called CC=2 SCAR=17.9
189 0 my $self = shift;
190 0 my $type = $self->api_type;
191 0 my $soap = $self->interfaces->{ lc($type) };
192 0 my $req = $soap;
193  
194 0 $req =~ s/::/\//g;
195 0 $req .= '.pm';
196  
197 0 eval { require "$req" };
198 0 ▶ croak "Failed to require auto-generated class '$soap': $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
199 if $@;
200  
201 0 return $soap->new
202 or croak "Failed to instantiate auto-generated class '$soap': $!";
203 }
204  
205 1 __PACKAGE__->meta->make_immutable;
206  
207 1 1;
208  
209 __END__
↑ Summary Edgecast/auto/RealTime/Interface/EdgeCastWebServices/EdgeCastWebServicesSoap.pm »