line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::RESTful; |
2
|
3
|
|
|
3
|
|
10615
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
4142
|
|
|
3
|
|
|
|
|
29
|
|
3
|
3
|
|
|
3
|
|
3998
|
use Mojo::Exception; |
|
3
|
|
|
|
|
2130
|
|
|
3
|
|
|
|
|
27
|
|
4
|
3
|
|
|
3
|
|
1294
|
use Mojo::Util qw(decamelize); |
|
3
|
|
|
|
|
3308937
|
|
|
3
|
|
|
|
|
401
|
|
5
|
3
|
|
|
3
|
|
23
|
use List::Util qw(any); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
270
|
|
6
|
3
|
|
|
3
|
|
4235
|
use Lingua::EN::Inflect 'PL'; |
|
3
|
|
|
|
|
101190
|
|
|
3
|
|
|
|
|
3493
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.1.4'; # VERSION |
9
|
|
|
|
|
|
|
# ABSTRACT: A Mojolicious Plugin for RESTful HTTP Actions |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has crud2http => sub { |
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
collection => { |
16
|
|
|
|
|
|
|
list => 'get', |
17
|
|
|
|
|
|
|
create => 'post', |
18
|
|
|
|
|
|
|
options => 'options', |
19
|
|
|
|
|
|
|
}, |
20
|
|
|
|
|
|
|
resource => { |
21
|
|
|
|
|
|
|
retrieve => 'get', |
22
|
|
|
|
|
|
|
update => 'put', |
23
|
|
|
|
|
|
|
delete => 'delete', |
24
|
|
|
|
|
|
|
patch => 'patch', |
25
|
|
|
|
|
|
|
options => 'options', |
26
|
|
|
|
|
|
|
}, |
27
|
|
|
|
|
|
|
nonresource => { |
28
|
|
|
|
|
|
|
search => 'get', |
29
|
|
|
|
|
|
|
count => 'get', |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub register { |
36
|
2
|
|
|
2
|
1
|
124722
|
my ($self, $app) = @_; |
37
|
|
|
|
|
|
|
$app->routes->add_shortcut( |
38
|
|
|
|
|
|
|
restful => sub { |
39
|
11
|
|
|
11
|
|
29427
|
my $route = shift; |
40
|
11
|
100
|
|
|
|
48
|
my $prefix = $route->name ? $route->name.'_' : ''; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# params to hash |
43
|
11
|
100
|
|
|
|
161
|
my $params = { @_ ? ( ref $_[0] ? %{ $_[0] } : @_ eq 1 ? (name => $_[0]) : @_ ) : () }; |
|
0
|
50
|
|
|
|
0
|
|
|
|
50
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
11
|
50
|
|
|
|
46
|
Mojo::Exception->throw('Route name is required in rest') unless defined $params->{name}; |
46
|
|
|
|
|
|
|
# name setting |
47
|
11
|
|
|
|
|
31
|
my $route_name = lc $params->{name}; # must be lower case. |
48
|
11
|
|
|
|
|
43
|
my $route_plural = PL( $route_name ); |
49
|
11
|
|
100
|
|
|
10733
|
my $root_methods = $params->{root} // "lcrudpo"; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Controller |
52
|
11
|
|
66
|
|
|
75
|
my $controller = $params->{controller} // ucfirst($route_name); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Non-resource routes |
55
|
11
|
|
66
|
|
|
60
|
my $nonresource = $params->{nonresource} // $self->crud2http->{nonresource}; |
56
|
|
|
|
|
|
|
|
57
|
11
|
100
|
100
|
|
|
73
|
if (($route ne $route->root) and $root_methods =~/\A[lcrudpo]+\z/) { |
58
|
2
|
|
|
|
|
67
|
my %methods = map { lc($_) => 1 } split //, $root_methods; |
|
14
|
|
|
|
|
39
|
|
59
|
2
|
|
|
|
|
13
|
$route->root->restful( |
60
|
|
|
|
|
|
|
name => $route_name, |
61
|
|
|
|
|
|
|
methods => $root_methods, |
62
|
|
|
|
|
|
|
controller => $controller, |
63
|
|
|
|
|
|
|
nonresource => $nonresource, |
64
|
|
|
|
|
|
|
root => '' |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
11
|
|
100
|
|
|
639
|
my %methods = map { lc($_) => 1 } split //, ($params->{methods} // "lcrudpo"); |
|
72
|
|
|
|
|
180
|
|
69
|
|
|
|
|
|
|
|
70
|
11
|
|
|
|
|
33
|
foreach my $collection_method (sort keys %{$self->crud2http->{collection}}) { |
|
11
|
|
|
|
|
36
|
|
71
|
33
|
100
|
|
|
|
4997
|
next unless $methods{substr($collection_method, 0, 1)}; |
72
|
32
|
|
|
|
|
95
|
my $http = $self->crud2http->{collection}->{$collection_method}; |
73
|
32
|
|
|
|
|
210
|
my $url = "/$route_plural"; |
74
|
32
|
|
|
|
|
77
|
my $action = $prefix . $route_plural . '_' . $collection_method; |
75
|
32
|
50
|
|
|
|
106
|
next if $route->find($action); |
76
|
32
|
|
|
|
|
4272
|
$route->route($url)->via($http)->to( |
77
|
|
|
|
|
|
|
controller => $controller, action => $action |
78
|
|
|
|
|
|
|
)->name($action); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
|
|
|
1992
|
foreach my $nonresource_method (sort keys %{$nonresource}) { |
|
11
|
|
|
|
|
42
|
|
82
|
21
|
|
|
|
|
2656
|
my $http = $nonresource->{$nonresource_method}; |
83
|
21
|
|
|
|
|
49
|
my $url = "/$route_plural/$nonresource_method"; |
84
|
21
|
|
|
|
|
42
|
my $action = $prefix . $route_plural . '_' . $nonresource_method; |
85
|
21
|
50
|
|
|
|
62
|
next if $route->find($action); |
86
|
21
|
|
|
|
|
3169
|
$route->route($url)->via($http)->to( |
87
|
|
|
|
|
|
|
controller => $controller, action => $action |
88
|
|
|
|
|
|
|
)->name($action); |
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
|
91
|
11
|
|
|
|
|
2736
|
for my $resource_method (sort keys %{$self->crud2http->{resource}}) { |
|
11
|
|
|
|
|
34
|
|
92
|
55
|
100
|
|
|
|
9929
|
next unless $methods{substr($resource_method, 0, 1)}; |
93
|
50
|
|
|
|
|
193
|
my $http = $self->crud2http->{resource}->{$resource_method}; |
94
|
50
|
|
|
|
|
346
|
my $url = "/$route_plural/:$route_name"; |
95
|
50
|
|
|
|
|
114
|
my $action = $prefix . $route_name . '_' . $resource_method; |
96
|
50
|
50
|
|
|
|
143
|
next if $route->find($action); |
97
|
50
|
|
|
|
|
9441
|
$route->route($url)->via($http)->to( |
98
|
|
|
|
|
|
|
controller => $controller, action => $action |
99
|
|
|
|
|
|
|
)->name($action); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Return chained route. |
103
|
11
|
100
|
|
|
|
2496
|
if (any { /[rudp]/ } keys %methods) { |
|
13
|
|
|
|
|
61
|
|
104
|
10
|
|
|
|
|
84
|
return $route->route("/$route_plural/:$route_name")->name("$prefix$route_name"); |
105
|
|
|
|
|
|
|
} else { |
106
|
1
|
|
|
|
|
6
|
return $route->route("/$route_plural")->name("$prefix$route_plural"); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
2
|
|
|
|
|
12
|
); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__END__ |