| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Shodo::Suzuri; |
|
2
|
5
|
|
|
5
|
|
26
|
use strict; |
|
|
5
|
|
|
|
|
7
|
|
|
|
5
|
|
|
|
|
158
|
|
|
3
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
|
5
|
|
|
|
|
8
|
|
|
|
5
|
|
|
|
|
109
|
|
|
4
|
5
|
|
|
5
|
|
24
|
use Carp qw//; |
|
|
5
|
|
|
|
|
7
|
|
|
|
5
|
|
|
|
|
70
|
|
|
5
|
5
|
|
|
5
|
|
4275
|
use Try::Tiny; |
|
|
5
|
|
|
|
|
7725
|
|
|
|
5
|
|
|
|
|
287
|
|
|
6
|
5
|
|
|
5
|
|
3307
|
use JSON qw/from_json to_json/; |
|
|
5
|
|
|
|
|
46827
|
|
|
|
5
|
|
|
|
|
33
|
|
|
7
|
5
|
|
|
5
|
|
5243
|
use Data::Validator; |
|
|
5
|
|
|
|
|
594344
|
|
|
|
5
|
|
|
|
|
194
|
|
|
8
|
5
|
|
|
5
|
|
4149
|
use Clone qw/clone/; |
|
|
5
|
|
|
|
|
67935
|
|
|
|
5
|
|
|
|
|
3717
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
|
11
|
5
|
|
|
5
|
0
|
24
|
my ($class, %args) = @_; |
|
12
|
5
|
|
|
|
|
23
|
my $self = bless { |
|
13
|
|
|
|
|
|
|
hanshi => $args{hanshi} |
|
14
|
|
|
|
|
|
|
}, $class; |
|
15
|
5
|
|
100
|
|
|
45
|
$self->stash->{description} = $args{description} || ''; |
|
16
|
5
|
|
|
|
|
22
|
$self; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub hanshi { |
|
20
|
4
|
|
|
4
|
0
|
16
|
shift->{hanshi}; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub stash { |
|
24
|
48
|
|
|
48
|
0
|
1562
|
my $self = shift; |
|
25
|
48
|
|
100
|
|
|
176
|
$self->{stash} ||= {}; |
|
26
|
48
|
|
|
|
|
185
|
return $self->{stash}; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub request { |
|
30
|
4
|
|
|
4
|
1
|
16515
|
my ($self, $req) = @_; |
|
31
|
4
|
50
|
|
4
|
|
42
|
unless (try { $req->isa('HTTP::Request') }) { |
|
|
4
|
|
|
|
|
131
|
|
|
32
|
0
|
|
|
|
|
0
|
Carp::croak("Request is not HTTP::Request: $req"); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
4
|
|
|
|
|
149
|
$self->stash->{method} = $req->method; |
|
35
|
4
|
|
|
|
|
22
|
$self->stash->{path} = $req->uri->path; |
|
36
|
4
|
|
|
|
|
20
|
$self->stash->{query} = $req->uri->query; |
|
37
|
4
|
|
|
|
|
41
|
$self->stash->{request_body} = $req->decoded_content; |
|
38
|
4
|
100
|
|
|
|
41
|
if($req->content_type =~ m!^application/json!) { |
|
39
|
3
|
|
|
|
|
110
|
my $json_body = to_json(from_json($self->stash->{request_body}, { utf8 => 1 }), { pretty => 1 }); |
|
40
|
3
|
|
|
|
|
235
|
$self->stash->{request_body} = $json_body; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
4
|
|
|
|
|
46
|
return $req; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub response { |
|
46
|
3
|
|
|
3
|
1
|
245
|
my ($self, $res) = @_; |
|
47
|
3
|
50
|
|
3
|
|
21
|
unless (try { $res->isa('HTTP::Response') }) { |
|
|
3
|
|
|
|
|
72
|
|
|
48
|
0
|
|
|
|
|
0
|
Carp::croak("Response is not HTTP::Response: $res"); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
3
|
|
|
|
|
49
|
$self->stash->{code} = $res->code; |
|
51
|
3
|
|
|
|
|
18
|
$self->stash->{status_line} = $res->status_line; |
|
52
|
3
|
|
|
|
|
32
|
$self->stash->{response_body} = $res->decoded_content; |
|
53
|
3
|
100
|
|
|
|
21
|
if($res->content_type =~ m!^application/json!) { |
|
54
|
2
|
|
|
|
|
70
|
my $json_body = to_json(from_json($self->stash->{response_body}, { utf8 => 1}), { pretty => 1 }); |
|
55
|
2
|
|
|
|
|
129
|
$self->stash->{response_body} = $json_body; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
3
|
|
|
|
|
24
|
return $res; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub document { |
|
61
|
4
|
|
|
4
|
0
|
347
|
my $self = shift; |
|
62
|
4
|
|
|
|
|
24
|
return $self->hanshi->render( $self->stash ); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub params { |
|
66
|
4
|
|
|
4
|
1
|
37
|
my ($self, %args) = @_; |
|
67
|
4
|
|
|
|
|
107
|
$self->stash->{rule} = clone(\%args); |
|
68
|
4
|
|
|
|
|
80
|
my $validator = Data::Validator->new( %args )->with('NoThrow'); |
|
69
|
4
|
|
|
|
|
23740
|
$self->{validator} = $validator; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub validate { |
|
73
|
4
|
|
|
4
|
1
|
32
|
my ($self, @args) = @_; |
|
74
|
4
|
50
|
|
|
|
49
|
Carp::croak "Rule is not set on Suzuri instance" unless $self->{validator}; |
|
75
|
4
|
|
|
|
|
7
|
my $result; |
|
76
|
4
|
50
|
33
|
|
|
40
|
if( ref $args[0] && ref $args[0] eq 'HASH') { |
|
77
|
4
|
|
|
|
|
43
|
$result = $self->{validator}->validate($args[0]); |
|
78
|
|
|
|
|
|
|
}else{ |
|
79
|
0
|
|
|
|
|
0
|
$result = $self->{validator}->validate(@args); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
4
|
100
|
|
|
|
530
|
if($self->{validator}->has_errors()) { |
|
82
|
2
|
|
|
|
|
21
|
return; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
2
|
|
|
|
|
12
|
return $result; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
*req = \&request; |
|
88
|
|
|
|
|
|
|
*res = \&response; |
|
89
|
|
|
|
|
|
|
*doc = \&document; |
|
90
|
|
|
|
|
|
|
*rule = \¶ms; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |