| 1 | package OX::RouteBuilder::HTTPMethod; | ||||||||||||||
| 2 | 34 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 34 | $OX::RouteBuilder::HTTPMethod::AUTHORITY = 'cpan:STEVAN'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | 34 | $OX::RouteBuilder::HTTPMethod::VERSION = '0.14'; | |||||||||||||
| 6 | 34 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 34 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | # ABSTRACT: OX::RouteBuilder which routes to a method in a controller based on the HTTP method | ||||||||||||||
| 9 | |||||||||||||||
| 10 | 34 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 34 | with 'OX::RouteBuilder'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | 6 | ▶ | sub compile_routes { | ||||||||||||
|
Subroutine
compile_routes: called CC=1 SCAR=0.5
Pod
undocumented
| |||||||||||||||
| 16 | 6 | my $self = shift; | |||||||||||||
| 17 | 6 | my ($app) = @_; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 6 | my $spec = $self->route_spec; | |||||||||||||
| 20 | 6 | my $params = $self->params; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 6 | my ($defaults, $validations) = $self->extract_defaults_and_validations($params); | |||||||||||||
| 23 | 6 | $defaults = { %$spec, %$defaults }; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | ▶ | my $target = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=18.4
| |||||||||||||||
| 26 | 1 | my ($req) = @_; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 1 | my $match = $req->mapping; | |||||||||||||
| 29 | 1 | my $a = $match->{action}; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | my $err; | |||||||||||||
| 32 | 1 | ▶ | my $s = try { $app->fetch($a) } catch { ($err) = split "\n"; undef }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | return [ | ||||||||||||||
| 34 | 1 | ▶ | 500, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | [], | ||||||||||||||
| 36 | ["Cannot resolve $a in " . blessed($app) . ": $err"] | ||||||||||||||
| 37 | ] unless $s; | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | my $component = $s->get; | |||||||||||||
| 40 | 0 | my $method = lc($req->method); | |||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | ▶ | if ($component->can($method)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 43 | 0 | return $component->$method(@_); | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | elsif ($component->can('any')) { | ||||||||||||||
| 46 | 0 | return $component->any(@_); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | else { | ||||||||||||||
| 49 | return [ | ||||||||||||||
| 50 | 0 | 500, | |||||||||||||
| 51 | [], | ||||||||||||||
| 52 | ["Component $component has no method $method"] | ||||||||||||||
| 53 | ]; | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | 6 | }; | |||||||||||||
| 56 | |||||||||||||||
| 57 | return { | ||||||||||||||
| 58 | 6 | path => $self->path, | |||||||||||||
| 59 | defaults => $defaults, | ||||||||||||||
| 60 | target => $target, | ||||||||||||||
| 61 | validations => $validations, | ||||||||||||||
| 62 | }; | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 167 | ▶ | sub parse_action_spec { | ||||||||||||
|
Subroutine
parse_action_spec: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 66 | 167 | my $class = shift; | |||||||||||||
| 67 | 167 | my ($action_spec) = @_; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 167 | ▶ | return if ref($action_spec); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 91 | ▶ | return unless $action_spec =~ /^(\w+)$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | return { | ||||||||||||||
| 73 | 5 | action => $1, | |||||||||||||
| 74 | name => $action_spec, | ||||||||||||||
| 75 | }; | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 34 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 79 | |||||||||||||||
| 80 | |||||||||||||||
| 81 | 34 | 1; | |||||||||||||
| 82 | |||||||||||||||
| 83 | __END__ | ||||||||||||||