line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::API::Mapper; |
2
|
2
|
|
|
2
|
|
63716
|
use warnings; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
74
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
1936
|
use Any::Moose; |
|
2
|
|
|
|
|
91425
|
|
|
2
|
|
|
|
|
15
|
|
5
|
2
|
|
|
2
|
|
4021
|
use Web::API::Mapper::RuleSet; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
1385
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has route => ( is => 'rw' ); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# post dispatcher |
12
|
|
|
|
|
|
|
has post => ( is => 'rw' , default => sub { return Web::API::Mapper::RuleSet->new; } ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# get dispatcher |
15
|
|
|
|
|
|
|
has get => ( is => 'rw' , default => sub { return Web::API::Mapper::RuleSet->new; } ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has any => ( is => 'rw' , default => sub { return Web::API::Mapper::RuleSet->new; } ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has fallback => ( is => 'rw' , isa => 'CodeRef' , default => sub { sub { } } ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
around BUILDARGS => sub { |
22
|
|
|
|
|
|
|
my $orig = shift; |
23
|
|
|
|
|
|
|
my $class = shift; |
24
|
|
|
|
|
|
|
if ( ! ref $_[0] && ref $_[1] eq 'HASH') { |
25
|
|
|
|
|
|
|
my $base = shift @_; |
26
|
|
|
|
|
|
|
my $route = shift @_; |
27
|
|
|
|
|
|
|
$class->$orig( base => $base , route => $route , @_); |
28
|
|
|
|
|
|
|
} else { |
29
|
|
|
|
|
|
|
$class->$orig(@_); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub BUILD { |
35
|
3
|
|
|
3
|
1
|
9
|
my ( $self , $args ) = @_; |
36
|
3
|
|
|
|
|
7
|
my $route = $args->{route}; |
37
|
3
|
|
|
|
|
8
|
my $base = $args->{base}; |
38
|
3
|
100
|
|
|
|
31
|
$self->post( Web::API::Mapper::RuleSet->new( $base , $route->{post} ) ) if $route->{post}; |
39
|
3
|
100
|
|
|
|
52
|
$self->get(Web::API::Mapper::RuleSet->new( $base , $route->{get} )) if $route->{get}; |
40
|
3
|
100
|
|
|
|
50
|
$self->any(Web::API::Mapper::RuleSet->new( $base , $route->{any} )) if $route->{any}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub mount { |
44
|
2
|
|
|
2
|
1
|
4
|
my ($self,$base,$route) = @_; |
45
|
2
|
|
|
|
|
4
|
for ( qw(post get any) ) { |
46
|
6
|
100
|
|
|
|
28
|
$self->$_->mount( $base => $route->{$_} ) if $route->{$_}; |
47
|
|
|
|
|
|
|
} |
48
|
2
|
|
|
|
|
15
|
return $self; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub auto_route { |
52
|
1
|
|
|
1
|
1
|
26
|
my ($class,$caller_package,$options) = @_; |
53
|
1
|
50
|
|
|
|
7
|
my $caller_class = ref $caller_package ? ref $caller_package : $caller_package; |
54
|
1
|
|
50
|
|
|
4
|
$options ||= {}; |
55
|
1
|
|
50
|
|
|
11
|
my $routetype = $options->{type} || 'any'; |
56
|
1
|
|
|
|
|
7
|
my $routes = { |
57
|
|
|
|
|
|
|
post => [], |
58
|
|
|
|
|
|
|
get => [], |
59
|
|
|
|
|
|
|
any => [], |
60
|
|
|
|
|
|
|
}; |
61
|
2
|
|
|
2
|
|
21
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
822
|
|
62
|
1
|
|
|
|
|
3
|
while( my ($funcname,$b) = each %{ $caller_class . '::' } ) { |
|
4
|
|
|
|
|
35
|
|
63
|
3
|
100
|
66
|
|
|
38
|
next if $options->{prefix} && $funcname !~ /^@{[ $options->{prefix} ]}/; |
|
3
|
|
|
|
|
54
|
|
64
|
2
|
50
|
33
|
|
|
10
|
next if $options->{regexp} && $funcname !~ $options->{regexp}; |
65
|
2
|
50
|
|
|
|
7
|
next if ! defined &$b; |
66
|
2
|
|
|
|
|
4
|
my $path = $funcname; |
67
|
2
|
|
|
|
|
5
|
$path =~ tr{_}{/}; # translate _ to / |
68
|
2
|
50
|
|
|
|
8
|
$path =~ s/^@{[ $options->{prefix} ]}//g if $options->{prefix}; # strip prefix if prefix defined. |
|
2
|
|
|
|
|
22
|
|
69
|
2
|
|
|
1
|
|
5
|
push @{ $routes->{ $routetype } }, $path , sub { return $b->( $caller_package , @_ ); }; |
|
2
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
242
|
|
70
|
|
|
|
|
|
|
} |
71
|
1
|
|
|
|
|
4
|
return $routes; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub dispatch { |
75
|
4
|
|
|
4
|
1
|
9141
|
my ( $self, $path, $args ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# my $base = $self->base; |
78
|
|
|
|
|
|
|
# $path =~ s{^/$base/}{} if $base; |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
8
|
my $ret; |
81
|
|
|
|
|
|
|
|
82
|
4
|
|
|
|
|
30
|
$ret = $self->any->dispatch( $path , $args ); |
83
|
4
|
100
|
|
|
|
76
|
return $ret if $ret; |
84
|
|
|
|
|
|
|
|
85
|
3
|
|
|
|
|
16
|
$ret = $self->post->dispatch( $path , $args ); |
86
|
3
|
100
|
|
|
|
2199
|
return $ret if $ret; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
10
|
$ret = $self->get->dispatch( $path , $args ); |
89
|
1
|
50
|
|
|
|
938
|
return $ret if $ret; |
90
|
|
|
|
|
|
|
|
91
|
1
|
50
|
|
|
|
13
|
return $self->fallback->( $args ) if $self->fallback; |
92
|
0
|
|
|
|
|
|
return; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |