| 1 | package Dancer::Plugin::REST; | ||||||||||||||||||||||||||||
| 2 | 9 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: A plugin for writing RESTful apps with Dancer | ||||||||||||||||||||||||||||
| 4 | 9 | $Dancer::Plugin::REST::VERSION = '0.12'; | |||||||||||||||||||||||||||
| 5 | 9 | ▶ | use 5.24.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Dancer::HTTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 9 | my $content_types = { | |||||||||||||||||||||||||||
| 16 | json => 'application/json', | ||||||||||||||||||||||||||||
| 17 | yml => 'text/x-yaml', | ||||||||||||||||||||||||||||
| 18 | xml => 'application/xml', | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 9 | our $default_serializer; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | register prepare_serializer_for_format => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.5
| |||||||||||||||||||||||||||||
| 24 | 4 | my $conf = plugin_setting; | |||||||||||||||||||||||||||
| 25 | my $serializers = ( | ||||||||||||||||||||||||||||
| 26 | ($conf && exists $conf->{serializers}) | ||||||||||||||||||||||||||||
| 27 | ? $conf->{serializers} | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | : { 'json' => 'JSON', | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$conf and exists $conf->{"serializers"}
MC/DC0%0/2
$conf and exists $conf->{"serializers"}
$conf
exists $conf->{"serializers"}
Truth table33%1/3
A and B
A= $confB= exists $conf->{"serializers"}
| |||||||||||||||||||||||||||||
| 29 | 'yml' => 'YAML', | ||||||||||||||||||||||||||||
| 30 | 'xml' => 'XML', | ||||||||||||||||||||||||||||
| 31 | 'dump' => 'Dumper', | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 15 | ▶ | hook 'before' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||
| 36 | # remember what was there before | ||||||||||||||||||||||||||||
| 37 | 15 | ▶ | $default_serializer ||= setting 'serializer'; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Dancer::Plugin::REST::default_serializer ||= setting("serializer")
MC/DC0%0/2
$Dancer::Plugin::REST::default_serializer ||= setting("serializer")
$Dancer::Plugin::REST::default_serializer
setting("serializer")
Truth table33%1/3
A ||= B
A= $Dancer::Plugin::REST::default_serializerB= setting("serializer")
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 15 | ▶ | my $format = params->{'format'} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | ▶ | my $serializer = $serializers->{$format} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | or return halt( | ||||||||||||||||||||||||||||
| 43 | Dancer::Error->new( | ||||||||||||||||||||||||||||
| 44 | code => 404, | ||||||||||||||||||||||||||||
| 45 | title => "unsupported format requested", | ||||||||||||||||||||||||||||
| 46 | message => "unsupported format requested: " . $format | ||||||||||||||||||||||||||||
| 47 | )->render | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 10 | set serializer => $serializer; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # check if we were supposed to deserialize the request | ||||||||||||||||||||||||||||
| 53 | 10 | Dancer::Serializer->process_request( | |||||||||||||||||||||||||||
| 54 | Dancer::SharedData->request | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | content_type $content_types->{$format} || setting('content_type'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$content_types->{$format} || setting('content_type')
MC/DC0%0/2
$content_types->{$format} || setting('content_type')
$content_types->{$format}
setting('content_type')
Truth table33%1/3
A || B
A= $content_types->{$format}B= setting('content_type')
| |||||||||||||||||||||||||||||
| 58 | 4 | }; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 14 | ▶ | hook after => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | # put it back the way it was | ||||||||||||||||||||||||||||
| 62 | 14 | set serializer => $default_serializer; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 4 | }; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 9 | my %triggers_map = ( | |||||||||||||||||||||||||||
| 67 | get => \&get, | ||||||||||||||||||||||||||||
| 68 | update => \&put, | ||||||||||||||||||||||||||||
| 69 | create => \&post, | ||||||||||||||||||||||||||||
| 70 | delete => \&del, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | register resource => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 74 | 4 | ▶ | croak "resource invoked without arguments" unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | my ($resource, %triggers) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | while( my( $t, $sub ) = each %triggers ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 13 | ▶ | my $method = $triggers_map{$t} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | or croak "action '$t' not recognized"; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 12 | ▶ | if ( $t eq 'create' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 3 | $method->( "/${resource}" => $triggers{$t} ); | |||||||||||||||||||||||||||
| 84 | 3 | $method->( "/${resource}.:format" => $triggers{$t} ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||
| 87 | $method->( "/${resource}/:id$_" => $triggers{$t} ) | ||||||||||||||||||||||||||||
| 88 | 9 | for '.:format', ''; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 9 | }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 8 | ▶ | register send_entity => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 95 | # entity, status_code | ||||||||||||||||||||||||||||
| 96 | 8 | ▶ | status($_[1] || 200); | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] || 200
MC/DC0%0/1
$_[1] || 200
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 97 | 8 | $_[0]; | |||||||||||||||||||||||||||
| 98 | 9 | }; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 9 | my %http_codes = Dancer::HTTP->codes->%*; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 9 | for my $code (keys %http_codes) { | |||||||||||||||||||||||||||
| 103 | 306 | my $helper_name = lc($http_codes{$code}); | |||||||||||||||||||||||||||
| 104 | 306 | $helper_name =~ s/[^\w]+/_/gms; | |||||||||||||||||||||||||||
| 105 | 306 | $helper_name = "status_${helper_name}"; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 8 | ▶ | register $helper_name => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | my $entity = ($code >= 400 ) ? { error => $_[0]} : $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 8 | send_entity($entity, $code); | |||||||||||||||||||||||||||
| 110 | 306 | }; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 9 | register_plugin; | |||||||||||||||||||||||||||
| 114 | 9 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||