| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->accepts and my $best_match = best_match([$self->accepts], $request->header("Accept"))
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"))
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 145 | 0 | $_->throw; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | ? http_throw(BadRequest => { message => "$_" }) | ||||||||||||||||||||||||||||
| 150 | : http_throw('BadRequest') | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 1 | }; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||