| 1 | package OX::RouteBuilder::REST; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: OX::RouteBuilder which routes to an action method in a controller class based on HTTP verbs | ||||||||||
| 4 | 2 | our $VERSION = '0.004'; # VERSION | |||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | with 'OX::RouteBuilder'; | |||||||||
| 11 | |||||||||||
| 12 | sub import { | ||||||||||
| 13 | 1 | ▶ | my $caller = caller; | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | my $meta = Moose::Util::find_meta($caller); | |||||||||
| 15 | 1 | $meta->add_route_builder('OX::RouteBuilder::REST'); | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | sub compile_routes { | ||||||||||
| 19 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
compile_routes: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||
| 20 | 6 | my ($app) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 6 | my $spec = $self->route_spec; | |||||||||
| 23 | 6 | my $params = $self->params; | |||||||||
| 24 | 6 | my ( $defaults, $validations ) = | |||||||||
| 25 | $self->extract_defaults_and_validations($params); | ||||||||||
| 26 | 6 | $defaults = { %$spec, %$defaults }; | |||||||||
| 27 | |||||||||||
| 28 | my $target = sub { | ||||||||||
| 29 | 4 | ▶ | my ($req) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||
| 30 | |||||||||||
| 31 | 4 | my $match = $req->mapping; | |||||||||
| 32 | 4 | my $c = $match->{controller}; | |||||||||
| 33 | 4 | my $a = $match->{action}; | |||||||||
| 34 | |||||||||||
| 35 | 4 | my $err; | |||||||||
| 36 | 4 | ▶ | my $s = try { $app->fetch($c) } catch { ($err) = split "\n"; undef }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||
| 37 | return [ | ||||||||||
| 38 | 4 | ▶ | 500, [], [ "Cannot resolve $c in " . blessed($app) . ": $err" ] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | ] | ||||||||||
| 40 | unless $s; | ||||||||||
| 41 | |||||||||||
| 42 | 4 | my $component = $s->get; | |||||||||
| 43 | 4 | my $method = uc( $req->method ); | |||||||||
| 44 | 4 | my $action = $a . '_' . $method; | |||||||||
| 45 | |||||||||||
| 46 | 4 | ▶ | if ( $component->can($action) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 4 | return $component->$action(@_); | |||||||||
| 48 | } | ||||||||||
| 49 | else { | ||||||||||
| 50 | 0 | return [ 500, [], | |||||||||
| 51 | ["Component $component has no method $action"] ]; | ||||||||||
| 52 | } | ||||||||||
| 53 | 6 | }; | |||||||||
| 54 | |||||||||||
| 55 | return { | ||||||||||
| 56 | 6 | path => $self->path, | |||||||||
| 57 | defaults => $defaults, | ||||||||||
| 58 | target => $target, | ||||||||||
| 59 | validations => $validations, | ||||||||||
| 60 | }; | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | sub parse_action_spec { | ||||||||||
| 64 | 4 | ▶ | my $class = shift; | ||||||||
|
Subroutine
parse_action_spec: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 65 | 4 | my ($action_spec) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 4 | ▶ | return if ref($action_spec); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | ▶ | return unless $action_spec =~ /^REST\.(\w+)\.(\w+)$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | return { | ||||||||||
| 71 | 2 | controller => $1, | |||||||||
| 72 | action => $2, | ||||||||||
| 73 | name => $action_spec, | ||||||||||
| 74 | }; | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 78 | |||||||||||
| 79 | 2 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||
| 82 | |||||||||||
| 83 | =pod | ||||||||||
| 84 | |||||||||||
| 85 | =encoding UTF-8 | ||||||||||
| 86 | |||||||||||
| 87 | =head1 NAME | ||||||||||
| 88 | |||||||||||
| 89 | OX::RouteBuilder::REST - OX::RouteBuilder which routes to an action method in a controller class based on HTTP verbs | ||||||||||
| 90 | |||||||||||
| 91 | =head1 VERSION | ||||||||||
| 92 | |||||||||||
| 93 | version 0.004 | ||||||||||
| 94 | |||||||||||
| 95 | =head1 SYNOPSIS | ||||||||||
| 96 | |||||||||||
| 97 | package MyApp; | ||||||||||
| 98 | use OX; | ||||||||||
| 99 | use OX::RouteBuilder::REST; | ||||||||||
| 100 | |||||||||||
| 101 | has thing => ( | ||||||||||
| 102 | is => 'ro', | ||||||||||
| 103 | isa => 'MyApp::Controller::Thing', | ||||||||||
| 104 | ); | ||||||||||
| 105 | |||||||||||
| 106 | router as { | ||||||||||
| 107 | route '/thing' => 'REST.thing.root'; | ||||||||||
| 108 | route '/thing/:id' => 'REST.thing.item'; | ||||||||||
| 109 | }; | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | package MyApp::Controller::Thing; | ||||||||||
| 113 | use Moose; | ||||||||||
| 114 | |||||||||||
| 115 | sub root_GET { | ||||||||||
| 116 | my ($self, $req) = @_; | ||||||||||
| 117 | ... # return a list if things | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | sub root_PUT { | ||||||||||
| 121 | my ($self, $req) = @_; | ||||||||||
| 122 | ... # create a new thing | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | sub item_GET { | ||||||||||
| 126 | my ($self, $req, $id) = @_; | ||||||||||
| 127 | ... # view a thing | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | sub item_POST { | ||||||||||
| 131 | my ($self, $req, $id) = @_; | ||||||||||
| 132 | ... # update a thing | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||
| 136 | |||||||||||
| 137 | This is an L<OX::RouteBuilder> which routes to an action method in a | ||||||||||
| 138 | controller class based on HTTP verbs. It's a bit of a mixture between | ||||||||||
| 139 | L<OX::RouteBuilder::ControllerAction> and | ||||||||||
| 140 | L<OX::RouteBuilder::HTTPMethod>. | ||||||||||
| 141 | |||||||||||
| 142 | To enable this RouteBuilder, you need to C<use OX::RouteBuilder::REST> | ||||||||||
| 143 | in your main application class. | ||||||||||
| 144 | |||||||||||
| 145 | The C<action_spec> should be a string in the form | ||||||||||
| 146 | C<"REST.$controller.$action">, where C<$controller> is the name of a | ||||||||||
| 147 | service which provides a controller instance. For each HTTP verb you | ||||||||||
| 148 | want to support you will need to set up an action with the name | ||||||||||
| 149 | C<$action_$verb> (e.g. C<$action_GET>, C<$action_PUT>, etc). If no | ||||||||||
| 150 | matching action-verb-method is found, a 404 error will be returned. | ||||||||||
| 151 | |||||||||||
| 152 | C<controller> and C<action> will also be automatically added as | ||||||||||
| 153 | defaults for the route, as well as C<name> (which will be set to | ||||||||||
| 154 | C<"REST.$controller.$action">). | ||||||||||
| 155 | |||||||||||
| 156 | To generate a link to an action, use C<uri_for> with either the name | ||||||||||
| 157 | (eg C<"REST.$controller.$action">), or by passing a HashRef C<{ | ||||||||||
| 158 | controller => $controller, action => $action }>. See F<t/test.t> | ||||||||||
| 159 | for some examples. | ||||||||||
| 160 | |||||||||||
| 161 | =for Pod::Coverage import | ||||||||||
| 162 | compile_routes | ||||||||||
| 163 | parse_action_spec | ||||||||||
| 164 | |||||||||||
| 165 | =head1 AUTHORS | ||||||||||
| 166 | |||||||||||
| 167 | =over 4 | ||||||||||
| 168 | |||||||||||
| 169 | =item * | ||||||||||
| 170 | |||||||||||
| 171 | Thomas Klausner <domm@plix.at> | ||||||||||
| 172 | |||||||||||
| 173 | =item * | ||||||||||
| 174 | |||||||||||
| 175 | Validad GmbH http://validad.com | ||||||||||
| 176 | |||||||||||
| 177 | =back | ||||||||||
| 178 | |||||||||||
| 179 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 180 | |||||||||||
| 181 | This software is copyright (c) 2014 - 2021 by Thomas Klausner. | ||||||||||
| 182 | |||||||||||
| 183 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 184 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||