Spore/Meta/Method.pm

filter: Statementstmt 84.4% 87 / 103 Branchbran 63.3% 38 / 60 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 74.8% 146 / 195 CC 141 SCAR 30.5
CC
141
Cov
75%
__ANON__
22.4
add_email
22.4
add_user
22.4
CRAP 21.2
« Spore/Middleware/Auth/ApiKey.pm ↑ Summary Spore/Middleware/Auth/OAuth.pm »
1 package Net::HTTP::Spore::Meta::Method;
2 29 $Net::HTTP::Spore::Meta::Method::VERSION = '0.09';
3 # ABSTRACT: create api method
4  
5 29 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 29 ▶use MooseX::Types::Moose qw/Str Int ArrayRef HashRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use MooseX::Types::URI qw/Uri/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 29 extends 'Moose::Meta::Method';
12 29 ▶use Net::HTTP::Spore::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Net::HTTP::Spore::Meta::Types qw(UriPath HTTPMethod Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 29 has path => ( is => 'ro', isa => UriPath, required => 1 );
16 29 has method => ( is => 'ro', isa => HTTPMethod, required => 1 );
17 29 has description => ( is => 'ro', isa => Str, predicate => 'has_description' );
18  
19 29 has required_payload => (
20 is => 'ro',
21 isa => Boolean,
22 predicate => 'payload_is_required',
23 lazy => 1,
24 default => 0,
25 coerce => 1,
26 );
27 29 has authentication => (
28 is => 'ro',
29 isa => Boolean,
30 predicate => 'has_authentication',
31 default => 0,
32 lazy => 1,
33 coerce => 1,
34 );
35 29 has base_url => (
36 is => 'ro',
37 isa => Uri,
38 coerce => 1,
39 predicate => 'has_base_url',
40 );
41 29 has formats => (
42 is => 'ro',
43 isa => ArrayRef[Str],
44 predicate => 'has_formats',
45 );
46 29 has headers => (
47 is => 'ro',
48 isa => HashRef[Str],
49 predicate => 'has_headers',
50 );
51 29 has expected_status => (
52 traits => ['Array'],
53 is => 'ro',
54 isa => ArrayRef[Int],
55 auto_deref => 1,
56 predicate => 'has_expected_status',
57 handles => { find_expected_status => 'grep', },
58 );
59 29 has optional_params => (
60 traits => ['Array'],
61 is => 'ro',
62 isa => ArrayRef[Str],
63 predicate => 'has_optional_params',
64 auto_deref => 1,
65 );
66 29 has required_params => (
67 traits => ['Array'],
68 is => 'ro',
69 isa => ArrayRef[Str],
70 predicate => 'has_required_params',
71 auto_deref => 1,
72 );
73 29 has form_data => (
74 traits => ['Hash'],
75 is => 'ro',
76 isa => HashRef,
77 predicate => 'has_form_data',
78 auto_deref => 1,
79 );
80 has documentation => (
81 is => 'ro',
82 isa => Str,
83 lazy => 1,
84 0 ▶ default => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
85 0 my $self = shift;
86 0 my $doc;
87 0 $doc .= "name: " . $self->name . "\n";
88 0 ▶ $doc .= "description: " . $self->description . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_description 0 0 0
89 if $self->has_description;
90 0 $doc .= "method: " . $self->method . "\n";
91 0 $doc .= "path: " . $self->path . "\n";
92 0 ▶ $doc .= "optional params: " . join(', ', $self->optional_params) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_optional_params 0 0 0
93 if $self->has_optional_params;
94 0 ▶ $doc .= "required params: " . join(', ', $self->required_params) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_required_params 0 0 0
95 if $self->has_required_params;
96 0 $doc;
97 }
98 29 );
99  
100 130 ▶sub wrap {
Subroutine wrap: called CC=2 SCAR=7.1
Pod documented
101 130 my ( $class, %args ) = @_;
102  
103 130 my $name = $args{name};
104 7 ▶ my $code = sub {
Subroutine __ANON__: called CC=18 SCAR=31.1
Subroutine create_user: called CC=18 SCAR=31.1
105 7 my ( $self, %method_args ) = @_;
106  
107 7 my $method = $self->meta->find_spore_method_by_name( $name );
108  
109 my $payload =
110 ( defined $method_args{spore_payload} )
111 ? delete $method_args{spore_payload}
112 7 ▶ : delete $method_args{payload};
Branch: 2 branches
BranchTrueFalseTotal
defined $method_args{'spore_payload'} ? : 0 7 7
defined $method_args{'spore_payload'} ? : 0 20 20
113  
114 7 ▶ if ( $payload
Branch: 2 branches
BranchTrueFalseTotal
if ($payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i) 0 7 7
if ($payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i) 1 19 20
Condition66%4/6
$payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i
!ll&&!rl&&r
700
$payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i
!ll&&!rl&&r
1721
MC/DC0%0/2
$payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i
$payload not $method->method =~ /^(?:POST|PUT|PATCH)$/i
MC/DC100%2/2
$payload and not $method->method =~ /^(?:POST|PUT|PATCH)$/i
$payload not $method->method =~ /^(?:POST|PUT|PATCH)$/i
Truth table33%1/3
A and not(B)
A= $payloadB= not $method->method =~ /^(?:POST|PUT|PATCH)$/i
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= $payloadB= not $method->method =~ /^(?:POST|PUT|PATCH)$/i
ABresult
0X0
100
111
115 && ( $method->method !~ /^(?:POST|PUT|PATCH)$/i ) )
116 {
117 0 die Net::HTTP::Spore::Response->new( 599, [],
118 { error => "payload requires a PUT, PATCH or POST method" },
119 );
120 }
121  
122 7 ▶ if ( $method->payload_is_required && !$payload ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method->payload_is_required and not $payload) 0 7 7
if ($method->payload_is_required and not $payload) 2 17 19
Condition50%3/6
$method->payload_is_required and not $payload
!ll&&!rl&&r
700
$method->payload_is_required and not $payload
!ll&&!rl&&r
1702
MC/DC0%0/2
$method->payload_is_required and not $payload
$method->payload_is_required not $payload
MC/DC50%1/2
$method->payload_is_required and not $payload
$method->payload_is_required not $payload
Truth table33%1/3
A and not(B)
A= $method->payload_is_requiredB= not $payload
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= $method->payload_is_requiredB= not $payload
ABresult
0X0
100
111
123 0 die Net::HTTP::Spore::Response->new(
124 599,
125 [],
126 {
127 error => "this method require a payload, and no payload is provided",
128 }
129 );
130 }
131  
132 7 ▶ if ($method->has_required_params) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method->has_required_params) 2 5 7
if ($method->has_required_params) 0 17 17
133 0 foreach my $required ( $method->required_params ) {
134 0 ▶ if ( !grep { $required eq $_ } keys %method_args ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (grep {$required eq $_;} keys %method_args) 0 2 2
unless (grep {$required eq $_;} keys %method_args) 0 0 0
135 0 die Net::HTTP::Spore::Response->new(
136 599,
137 [],
138 {
139 error =>
140 "$required is marked as required but is missing",
141 }
142 );
143 }
144 }
145 }
146  
147 7 my $params;
148 7 foreach (keys %method_args) {
149 2 push @$params, $_, $method_args{$_};
150 }
151  
152 7 ▶ my $authentication =
Branch: 2 branches
BranchTrueFalseTotal
$method->has_authentication ? : 6 1 7
$method->has_authentication ? : 13 4 17
153 $method->has_authentication ? $method->authentication : $self->authentication;
154  
155 7 ▶ my $formats = $method->has_formats ? $method->formats : $self->formats;
Branch: 2 branches
BranchTrueFalseTotal
$method->has_formats ? : 0 7 7
$method->has_formats ? : 0 17 17
156  
157 7 ▶ my $base_url =
Branch: 2 branches
BranchTrueFalseTotal
$method->has_base_url ? : 0 7 7
$method->has_base_url ? : 0 17 17
158 $method->has_base_url
159 ? $method->base_url
160 : $self->base_url;
161  
162 7 ▶ my $env = {
Branch: 2 branches
BranchTrueFalseTotal
$base_url->path eq '/' ? : 1 6 7
$base_url->path eq '/' ? : 5 12 17
163 REQUEST_METHOD => $method->method,
164 SERVER_NAME => $base_url->host,
165 SERVER_PORT => $base_url->port,
166 SCRIPT_NAME => (
167 $base_url->path eq '/'
168 ? ''
169 : $base_url->path
170 ),
171 PATH_INFO => $method->path,
172 REQUEST_URI => '',
173 QUERY_STRING => '',
174 HTTP_USER_AGENT => $self->api_useragent->agent,
175 'spore.expected_status' => [ $method->expected_status ],
176 'spore.authentication' => $authentication,
177 'spore.params' => $params,
178 'spore.payload' => $payload,
179 'spore.errors' => *STDERR,
180 'spore.url_scheme' => $base_url->scheme,
181 'spore.userinfo' => $base_url->userinfo,
182 'spore.formats' => $formats,
183 };
184  
185 7 ▶ $env->{'spore.form_data'} = $method->form_data
Branch: 2 branches
BranchTrueFalseTotal
if $method->has_form_data 1 6 7
if $method->has_form_data 0 17 17
186 if $method->has_form_data;
187  
188 7 ▶ $env->{'spore.headers'} = $method->headers if $method->has_headers;
Branch: 2 branches
BranchTrueFalseTotal
if $method->has_headers 0 7 7
if $method->has_headers 1 16 17
189  
190 7 my $response = $self->http_request($env);
191 7 my $code = $response->status;
192  
193 my $ok = ($method->has_expected_status)
194 3 ▶ ? $method->find_expected_status( sub { $_ eq $code } )
Subroutine __ANON__: called CC=1 SCAR=0.0
195 7 ▶ : $response->is_success; # only 2xx is success
Branch: 2 branches
BranchTrueFalseTotal
$method->has_expected_status ? : 3 4 7
$method->has_expected_status ? : 0 17 17
196 7 ▶ die $response if not $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 0 7 7
unless $ok 4 13 17
197  
198 7 $response;
199 130 };
200 130 $args{body} = $code;
201  
202 130 ▶ if ($args{'form-data'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"form-data"}) 21 109 130
203 21 $args{'form_data'} = delete $args{'form-data'};
204 }
205  
206 130 $class->SUPER::wrap(%args);
207 }
208  
209 29 1;
210  
211 __END__
« Spore/Middleware/Auth/ApiKey.pm ↑ Summary Spore/Middleware/Auth/OAuth.pm »