| 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.006'; # 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 | 1 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | my $caller = caller; | |||||||||||||||||||
| 14 | 1 | my $meta = Moose::Util::find_meta($caller); | |||||||||||||||||||
| 15 | 1 | $meta->add_route_builder('OX::RouteBuilder::REST'); | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 6 | ▶ | sub compile_routes { | ||||||||||||||||||
|
Subroutine
compile_routes: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||
| 19 | 6 | my $self = shift; | |||||||||||||||||||
| 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 | 6 | ▶ | my $target = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.6
| |||||||||||||||||||||
| 29 | 6 | my ($req) = @_; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 6 | my $match = $req->mapping; | |||||||||||||||||||
| 32 | 6 | my $c = $match->{controller}; | |||||||||||||||||||
| 33 | 6 | my $a = $match->{action}; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 6 | my $err; | |||||||||||||||||||
| 36 | 0 | ▶ | my $s = try { $app->fetch($c) } catch { ($err) = split "\n"; undef }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 37 | return [ | ||||||||||||||||||||
| 38 | 6 | ▶ | 500, [], [ "Cannot resolve $c in " . blessed($app) . ": $err" ] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | ] | ||||||||||||||||||||
| 40 | unless $s; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 6 | my $component = $s->get; | |||||||||||||||||||
| 43 | 6 | my $method = uc( $req->method ); | |||||||||||||||||||
| 44 | 6 | my $action = $a . '_' . $method; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 6 | ▶ | if ( $component->can($action) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 47 | 4 | return $component->$action(@_); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | ▶ | elsif ($method eq 'HEAD' && $component->can($a . '_GET')) { | |||||||||||||||||||
|
Condition66%2/3
$method eq "HEAD" and $component->can($a . '_GET')
MC/DC50%1/2
$method eq "HEAD" and $component->can($a . '_GET')
$method eq "HEAD"
$component->can($a . '_GET')
Truth table66%2/3
A and B
A= $method eq "HEAD"B= $component->can($a . '_GET')
| |||||||||||||||||||||
| 50 | 1 | $action = $a . '_GET'; | |||||||||||||||||||
| 51 | 1 | my $res = $component->$action(@_); | |||||||||||||||||||
| 52 | 1 | ▶ | if (ref($res) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 53 | 1 | $res->[2]=['']; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | ▶ | elsif (blessed($res) && $res->can('content')) { | |||||||||||||||||||
|
Condition0%0/3
builtin::blessed($res) and $res->can("content")
MC/DC0%0/2
builtin::blessed($res) and $res->can("content")
builtin::blessed($res)
$res->can("content")
Truth table0%0/3
A and B
A= builtin::blessed($res)B= $res->can("content")
| |||||||||||||||||||||
| 56 | 0 | $res->content(''); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | # else don't mess with content.. | ||||||||||||||||||||
| 59 | 1 | return $res; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||
| 62 | 1 | return [ 501, [], | |||||||||||||||||||
| 63 | ["Controller '$c' (".blessed($component).") has no method '$action'"] ]; | ||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | 6 | }; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | return { | ||||||||||||||||||||
| 68 | 6 | path => $self->path, | |||||||||||||||||||
| 69 | defaults => $defaults, | ||||||||||||||||||||
| 70 | target => $target, | ||||||||||||||||||||
| 71 | validations => $validations, | ||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 4 | ▶ | sub parse_action_spec { | ||||||||||||||||||
|
Subroutine
parse_action_spec: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 76 | 4 | my $class = shift; | |||||||||||||||||||
| 77 | 4 | my ($action_spec) = @_; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 4 | ▶ | return if ref($action_spec); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 4 | ▶ | return unless $action_spec =~ /^REST\.(\w+)\.(\w+)$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | return { | ||||||||||||||||||||
| 83 | 2 | controller => $1, | |||||||||||||||||||
| 84 | action => $2, | ||||||||||||||||||||
| 85 | name => $action_spec, | ||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | 1; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||