Sloth/Resource.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 30.0% 3 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.2% 16 / 19 Podpod 100% 2 / 2 totaltotal 78.4% 80 / 102 CC 8 SCAR 14.6
CC
8
Cov
78%
_serializer
12.0
__ANON__
12.0
handle_request
5.7
CRAP 4.3
« Sloth/Method.pm ↑ Summary Sloth/RequestParser.pm »
1 package Sloth::Resource;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Sloth::Resource::VERSION = '0.05';
4 }
5 # ABSTRACT: A resource that exposed by the REST server
6  
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use HTTP::Throwable::Factory 'http_throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Moose::Util qw( does_role );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use REST::Utils qw( best_match );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use String::CamelCase 'decamelize';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has c => (
19 is => 'ro'
20 );
21  
22  
23 1 ▶sub resource_arguments {
Subroutine resource_arguments: called CC=1 SCAR=0.0
Pod documented
24 1 return ( c => shift->c );
25 }
26  
27  
28 1 has representations => (
29 required => 1,
30 isa => 'HashRef',
31 traits => [ 'Hash' ],
32 handles => {
33 representations => 'values',
34 accepts => 'keys',
35 representation => 'get'
36 }
37 );
38  
39  
40 has methods => (
41 isa => 'HashRef',
42 is => 'ro',
43 required => 1,
44 traits => [ 'Hash' ],
45 lazy => 1,
46 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 my $self = shift;
48 1 my $mpo = Module::Pluggable::Object->new(
49 search_path => $self->meta->name,
50 require => 1
51 );
52 return {
53 map {
54 1 my ($method) = $_ =~ /.*::([a-z]*)$/i;
55 1 uc($method) => $_->new($self->resource_arguments);
56 } grep {
57 1 $_->does('Sloth::Method')
58 } $mpo->plugins
59 }
60 },
61 1 handles => {
62 _method_handler => 'get',
63 _method_handlers => 'values',
64 supported_methods => 'keys'
65 }
66 );
67  
68 has router => (
69 is => 'ro',
70 lazy => 1,
71 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 my $self = shift;
73 0 my $router = Path::Router->new
74 }
75 1 );
76  
77 1 has path => (
78 is => 'ro',
79 isa => 'Str',
80 required => 1
81 );
82  
83 has _routes => (
84 is => 'ro',
85 lazy => 1,
86 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 my $self = shift;
88 return [
89 map {
90 1 my $router = Path::Router->new;
91 1 $router->add_route(
92 $_->path => (
93 defaults => {
94 resource => $self->name,
95 },
96 target => $self
97 )
98 );
99 1 $router;
100 } $self->_method_handlers
101 ];
102 }
103 1 );
104  
105 has name => (
106 is => 'ro',
107 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 1 my $self = shift;
109 1 my ($name) = $self->meta->name =~ /^.*::(.*)$/;
110 1 return decamelize($name);
111 }
112 1 );
113  
114 0 ▶sub _serializer {
Subroutine _serializer: not called CC=3 SCAR=24.8
115 0 my ($self, $type) = @_;
116 0 for my $rep ($self->representations) {
117 0 ▶ return $rep if $type =~ $rep->content_type;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ $rep->content_type 0 0 0
118 }
119 }
120  
121  
122 2 ▶sub handle_request {
Subroutine handle_request: called CC=4 SCAR=17.5
Pod documented
123 2 my ($self, $request) = @_;
124  
125 2 ▶ my $method = $self->_method_handler($request->method)
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->_method_handler($request->method) or return http_throw("MethodNotAllowed", {"allow", [$self->supported_methods]}) 1 1 2
126 or return http_throw('MethodNotAllowed' => {
127 allow => [ $self->supported_methods ]
128 });
129  
130 1 my $serializer;
131 1 ▶ if($self->accepts and my $best_match = best_match(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->accepts and my $best_match = best_match([$self->accepts], $request->header("Accept"))) 1 0 1
Condition33%1/3
$self->accepts and my $best_match = best_match([$self->accepts], $request->header("Accept"))
!ll&&!rl&&r
001
MC/DC0%0/2
$self->accepts and my $best_match = best_match([$self->accepts], $request->header("Accept"))
$self->accepts my $best_match = best_match([$self->accepts], $request->header("Accept"))
Truth table33%1/3
A and B
A= $self->acceptsB= my $best_match = best_match([$self->accepts], $request->header("Accept"))
ABresult
0X0
100
111
132 [ $self->accepts ],
133 $request->header('Accept')
134 )) {
135 1 $serializer = $self->representation($best_match);
136 }
137  
138 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 # This is done in 2 steps because we might not need to serialize if we
140 # throw a 2xx exception.
141 1 return $method->process_request($request, $serializer);
142 }
143 0 ▶ catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
144 0 ▶ if(does_role($_, 'HTTP::Throwable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (does_role($_, 'HTTP::Throwable')) { } 0 0 0
145 0 $_->throw;
146 }
147 else {
148 0 ▶ $_
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
149 ? http_throw(BadRequest => { message => "$_" })
150 : http_throw('BadRequest')
151 }
152 1 };
153 }
154  
155 1 1;
156  
157 __END__
« Sloth/Method.pm ↑ Summary Sloth/RequestParser.pm »