Sloth.pm

filter: Statementstmt 96.0% 72 / 75 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.4% 21 / 22 Podpod 100% 4 / 4 totaltotal 94.2% 99 / 105 CC 5 SCAR 5.3
CC
5
Cov
94%
mock
6.0
__ANON__
3.0
_request
2.0
CRAP 1.7
« Sloth/RequestParser/Form.pm ↑ Summary Sloth/Representation.pm »
1 package Sloth;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Sloth::VERSION = '0.05';
4 }
5 # ABSTRACT: A PSGI compatible REST framework
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use aliased 'Sloth::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use HTTP::Message::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Throwable::Factory qw(http_throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moose::Util qw( does_role );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 extends 'Plack::Component';
22  
23 1 has c => (
24 is => 'ro'
25 );
26  
27  
28 1 ▶sub resource_arguments {
Subroutine resource_arguments: called CC=1 SCAR=0.0
Pod documented
29 1 return ( c => shift->c );
30 }
31  
32  
33 has representations => (
34 is => 'ro',
35 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 my $self = shift;
37 1 my $prefix = $self->meta->name . '::Representation';
38 return {
39 map {
40 1 $_->content_type => $_->new
41 } grep {
42 1 does_role($_ => 'Sloth::Representation');
43 } Module::Pluggable::Object->new(
44 search_path => $prefix,
45 require => 1
46 )->plugins
47 };
48 },
49 1 lazy => 1
50 );
51  
52  
53 has resources => (
54 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 1 my $self = shift;
56 1 my $prefix = $self->meta->name . '::Resource';
57 return {
58 map {
59 1 my ($name) = $_ =~ /${prefix}::(.*)$/;
60 1 $name => $_->new(
61 representations => $self->representations,
62 $self->resource_arguments
63 )
64 } grep {
65 1 does_role($_ => 'Sloth::Resource');
66 } Module::Pluggable::Object->new(
67 search_path => $prefix,
68 require => 1
69 )->plugins
70 };
71 },
72 1 traits => [ 'Hash' ],
73 handles => {
74 resource => 'get',
75 resources => 'values'
76 },
77 lazy => 1,
78 );
79  
80  
81 has router => (
82 is => 'ro',
83 1 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
84 1 my $self = shift;
85 1 my $router = Path::Router->new;
86 1 for my $resource ($self->resources) {
87 1 for my $route (@{ $resource->_routes }) {
88 1 $router->include_router(
89 $resource->path => $route
90 )
91 }
92 }
93 1 return $router;
94 },
95 1 lazy => 1
96 );
97  
98  
99 0 ▶sub mock {
Subroutine mock: not called CC=2 SCAR=17.9
Pod documented
100 0 my ($self, $request) = @_;
101 0 ▶ $request->header(Accept => 'mock/ref') unless $request->header('Accept');
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header("Accept") 0 0 0
102 0 $self->_request($request->to_psgi);
103 }
104  
105  
106 3 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
107 3 my ($self, $env) = @_;
108 3 ▶ my $ret = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 3 return $self->_request($env);
110 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 2 $_->as_psgi;
112 3 };
113  
114 3 return $ret;
115 };
116  
117 3 ▶sub _request {
Subroutine _request: called CC=2 SCAR=6.9
118 3 my ($self, $env) = @_;
119 3 my $request = Plack::Request->new($env);
120  
121 3 ▶ if(my $route = $self->router->match($request->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $route = $self->router->match($request->path)) { } 2 1 3
122 2 $route->target->handle_request(
123 Request->new(
124 plack_request => $request,
125 path_components => $route->mapping,
126 router => $self->router
127 )
128 )->finalize;
129 }
130 else {
131 1 http_throw('NotFound');
132 }
133 }
134  
135 3 ▶sub prepare_app {
Subroutine prepare_app: called CC=1 SCAR=0.0
Pod documented
136 3 my $self = shift;
137  
138 # Force creation of lazy attributes
139 3 $self->resources;
140 3 $self->representations;
141 }
142  
143 1 1;
144  
145  
146 __END__
« Sloth/RequestParser/Form.pm ↑ Summary Sloth/Representation.pm »