| 1 | # | ||||||||||
| 2 | # This file is part of Jedi | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2013 by celogeek <me@celogeek.com>. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | package Jedi::Role::App; | ||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: imported role for Jedi::App | ||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | our $VERSION = '1.006'; # VERSION | |||||||||
| 15 | 6 | ▶ | use Jedi::Helpers::Scalar; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 6 | ▶ | use CHI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 6 | ▶ | use Carp qw/carp croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | # REQUIRED | ||||||||||
| 20 | |||||||||||
| 21 | 6 | requires 'jedi_app'; | |||||||||
| 22 | |||||||||||
| 23 | # INTERNAL ATTRIBUTES | ||||||||||
| 24 | |||||||||||
| 25 | 20 | ▶ | has '_jedi_routes' => ( is => 'ro', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 20 | ▶ | has '_jedi_missing' => ( is => 'ro', default => sub { [] } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 6 | has '_jedi_routes_cache' => ( is => 'lazy', clearer => 1 ); | |||||||||
| 28 | |||||||||||
| 29 | sub _build__jedi_routes_cache { | ||||||||||
| 30 | 16 | ▶ | return CHI->new( | ||||||||
|
Subroutine
_build__jedi_routes_cache: called CC=1 SCAR=0.0
| |||||||||||
| 31 | driver => 'RawMemory', | ||||||||||
| 32 | datastore => {}, | ||||||||||
| 33 | max_size => 10_000 | ||||||||||
| 34 | ); | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | sub _jedi_routes_push { | ||||||||||
| 38 | 37 | ▶ | my ( $self, $which, $path, $sub ) = @_; | ||||||||
|
Subroutine
_jedi_routes_push: called CC=5 SCAR=16.1
| |||||||||||
| 39 | 37 | ▶ | croak "method invalid : only support GET/POST/PUT/DELETE !" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | if $which !~ /^(?:GET|POST|PUT|DELETE)/x; | ||||||||||
| 41 | 36 | ▶ | croak "path invalid !" if !defined $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 35 | ▶ | croak "sub invalid !" if ref $sub ne 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 34 | ▶ | $path = $path->full_path if ref $path ne 'Regexp'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 34 | push @{ $self->_jedi_routes->{$which} }, [ $path, $sub ]; | |||||||||
| 45 | 34 | $self->_clear_jedi_routes_cache; | |||||||||
| 46 | 34 | return; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # ATTRIBUTES | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | has 'jedi_config' => ( is => 'ro', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 52 | 6 | has 'jedi_base_route' => ( is => 'ro' ); | |||||||||
| 53 | 6 | has 'jedi_host_ip' => ( is => 'ro' ); | |||||||||
| 54 | |||||||||||
| 55 | # PUBLIC METHODS | ||||||||||
| 56 | |||||||||||
| 57 | sub get { | ||||||||||
| 58 | 25 | ▶ | my ( $self, $path, $sub ) = @_; | ||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 59 | 25 | return $self->_jedi_routes_push( 'GET', $path, $sub ); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub post { | ||||||||||
| 63 | 5 | ▶ | my ( $self, $path, $sub ) = @_; | ||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | 5 | return $self->_jedi_routes_push( 'POST', $path, $sub ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub put { | ||||||||||
| 68 | 3 | ▶ | my ( $self, $path, $sub ) = @_; | ||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 69 | 3 | return $self->_jedi_routes_push( 'PUT', $path, $sub ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub del { | ||||||||||
| 73 | 1 | ▶ | my ( $self, $path, $sub ) = @_; | ||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 74 | 1 | return $self->_jedi_routes_push( 'DELETE', $path, $sub ); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | sub missing { | ||||||||||
| 78 | 2 | ▶ | my ( $self, $sub ) = @_; | ||||||||
|
Subroutine
missing: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 79 | 2 | ▶ | croak "sub invalid !" if ref $sub ne 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 1 | push( @{ $self->_jedi_missing }, $sub ); | |||||||||
| 82 | 1 | $self->_clear_jedi_routes_cache; | |||||||||
| 83 | 1 | return; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | sub response { | ||||||||||
| 87 | 57 | ▶ | my ( $self, $request, $response ) = @_; | ||||||||
|
Subroutine
response: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||
| 88 | |||||||||||
| 89 | 57 | my $path = $request->path; | |||||||||
| 90 | |||||||||||
| 91 | 57 | my $request_method = $request->env->{REQUEST_METHOD}; | |||||||||
| 92 | 57 | ▶ | $request_method = 'GET' if $request_method eq 'HEAD'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | 57 | my $routes = $self->_jedi_routes->{$request_method}; | |||||||||
| 95 | 57 | my $methods; | |||||||||
| 96 | |||||||||||
| 97 | 57 | my $cache_key = $request_method . ':' . $path; | |||||||||
| 98 | |||||||||||
| 99 | 57 | ▶ | if ( my $cache_routes = $self->_jedi_routes_cache->get($cache_key) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 19 | $methods = $cache_routes; | |||||||||
| 101 | } | ||||||||||
| 102 | else { | ||||||||||
| 103 | 38 | $methods = []; | |||||||||
| 104 | 38 | ▶ | if ( ref $routes eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 36 | for my $route_def (@$routes) { | |||||||||
| 106 | 89 | my ( $route, $sub ) = @$route_def; | |||||||||
| 107 | 89 | ▶ | if ( ref $route eq 'Regexp' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 6 | ▶ | push @$methods, $sub if $path =~ $route; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | } | ||||||||||
| 110 | else { | ||||||||||
| 111 | 83 | ▶ | push @$methods, $sub if $path eq $route->full_path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } | ||||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 38 | ▶ | @$methods = @{ $self->_jedi_missing } if !scalar @$methods; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | |||||||||||
| 118 | 38 | $self->_jedi_routes_cache->set( $cache_key => $methods ); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 57 | for my $meth (@$methods) { | |||||||||
| 122 | 57 | ▶ | last if !$self->$meth( $request, $response ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 57 | return $response; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 6 | 1; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||