MetaAPI/API/Service.pm

filter: Statementstmt 100% 70 / 70 Branchbran 80.7% 21 / 26 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 87.9% 109 / 124 CC 16 SCAR 14.2
CC
16
Cov
93%
root_uri
7.0
setup_method
6.3
can_method
3.0
CRAP 4.1
« MetaAPI/API/Images.pm ↑ Summary MetaAPI/API.pm »
1 package OpenStack::MetaAPI::API::Service;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use OpenStack::MetaAPI::API::Specs::Default ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 has 'auth' => (is => 'ro', required => 1);
10 17 has 'name' => (is => 'ro', required => 1);
11 17 has 'region' => (is => 'ro', required => 1);
12  
13 17 has 'interface' => (is => 'ro', default => 'public'); # admin internal or public
14  
15 has 'client' => (
16 is => 'ro',
17 lazy => 1,
18 34 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 34 my ($self) = @_;
20  
21 34 return $self->auth->service(
22 $self->name,
23 region => $self->region,
24 interface => $self->interface);
25 },
26 17 handles => [qw/endpoint get put post delete/]
27  
28 );
29  
30 17 has 'api' => (is => 'ro', required => 1)
31 ; # this is backreference to the mainapi so we can call other services
32  
33 17 has 'api_specs' => (is => 'ro', lazy => 1, default => \&BUILD_api_specs);
34  
35 ## FIXME: this needs a refactor...
36 # idea always strip the version from endpoint so we can add it to the uri later..
37 # this would make uri consistent... and improve root_uri
38 17 has 'version' => ('is' => 'ro', lazy => 1, default => \&BUILD_version);
39 17 has 'version_prefix' => ('is' => 'ro'); # added to very routes [optional]
40  
41 43 ▶has 'methods' => (is => 'ro', default => sub { return {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 34 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
44 34 my ($self, $args) = @_;
45  
46 34 $self->api_specs->setup_api_methods_for_service($self);
47  
48 34 return;
49 }
50  
51 26 ▶sub BUILD_version {
Subroutine BUILD_version: called CC=2 SCAR=6.9
Pod undocumented
52 26 my ($self) = @_;
53  
54 26 my $url = $self->client->endpoint;
55 26 ▶ if ($url =~ m{/(v[0-9\.]+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[/(v[0-9\.]+)]) 21 5 26
56 21 return $1;
57 }
58 5 return 'default';
59 }
60  
61 34 ▶sub BUILD_api_specs { # load specs
Subroutine BUILD_api_specs: called CC=2 SCAR=6.9
Pod undocumented
62 34 my ($self) = @_;
63  
64 34 my $v = $self->version;
65 34 $v =~ s{\.}{_};
66 34 my $pkg =
67 'OpenStack::MetaAPI::API::Specs::' . ucfirst($self->name) . '::' . $v;
68  
69 34 (my $file = $pkg) =~ s{::}{/}g;
70 34 $file .= '.pm';
71 34 my $load = eval { require $file; 1 };
72 34 ▶ if ($load) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load) 28 6 34
73 28 return $pkg->new();
74 }
75  
76 # default void specs [ undefined ]
77 # we do not have to define all specs for now
78 6 return OpenStack::MetaAPI::API::Specs::Default->new();
79 }
80  
81 106 ▶sub root_uri {
Subroutine root_uri: called CC=7 SCAR=19.5
Pod undocumented
82 106 my ($self, $uri) = @_;
83  
84 106 ▶ return unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 2 104 106
85  
86 104 ▶ return $uri if $uri =~ m{^v}; # already contains a version
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /^v/ 2 102 104
87  
88 # endpoint already contains a version
89 102 ▶ return $uri if $self->endpoint && $self->endpoint =~ m{/v\d}a;
Branch: 1 branch
BranchTrueFalseTotal
if $self->endpoint and $self->endpoint =~ m[/v\d]a 71 31 102
Condition66%2/3
$self->endpoint and $self->endpoint =~ m[/v\d]a
!ll&&!rl&&r
03171
MC/DC50%1/2
$self->endpoint and $self->endpoint =~ m[/v\d]a
$self->endpoint $self->endpoint =~ m[/v\d]a
Truth table66%2/3
A and B
A= $self->endpointB= $self->endpoint =~ m[/v\d]a
ABresult
0X0
100
111
90  
91 # append our prefix to the endpoint
92 31 ▶ if ($self->version_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version_prefix) 30 1 31
93 30 my $base = $self->version_prefix;
94 30 ▶ $base .= '/' unless $uri =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/] 0 30 30
95 30 $base .= $uri;
96 30 return $base;
97 }
98  
99 1 return $uri;
100 }
101  
102 121 ▶sub setup_method {
Subroutine setup_method: called CC=6 SCAR=18.5
Pod undocumented
103 121 my ($self, $name, $sub) = @_;
104  
105 121 ▶ die "setup_method must be called as an instance method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 121 121
106 121 ▶ die "setup_method: method name is required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 121 121
107 121 ▶ die "setup_method: second argument must be a CODE ref" unless ref $sub eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 121 121
108  
109 121 my $methods = $self->methods();
110 121 ▶ die "Method '$name' already exists" if defined $methods->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $methods->{$name} 1 120 121
111 120 ▶ die "Function '$name' already exists for " . ref($self)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($name) 0 120 120
112 if $self->can($name);
113  
114 120 $methods->{$name} = $sub;
115  
116 120 return 1;
117 }
118  
119 ### not sure how to overwrite 'can' method to let Moo/Class::XSAccessor see the methods
120 172 ▶sub can_method {
Subroutine can_method: called CC=3 SCAR=11.0
Pod undocumented
121 172 my ($self, $method) = @_; # Not shift, using goto.
122  
123 #my $sub = UNIVERSAL::can($self, $method);
124 172 my $sub = $self->can($method);
125  
126 172 ▶ if (not defined $sub && ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub and ref $self) 110 62 172
Condition66%2/3
defined $sub and ref $self
!ll&&!rl&&r
110062
MC/DC50%1/2
defined $sub and ref $self
defined $sub ref $self
Truth table66%2/3
A and B
A= defined $subB= ref $self
ABresult
0X0
100
111
127 110 $sub = $self->methods()->{$method};
128 }
129  
130 172 return $sub; # May be undefined.
131 }
132  
133 17 1;
134  
135 __END__
« MetaAPI/API/Images.pm ↑ Summary MetaAPI/API.pm »