line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::View::JSON::_PerRequest; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use HTTP::Status; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
218
|
|
4
|
1
|
|
|
1
|
|
3
|
use Scalar::Util; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
601
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub data { |
7
|
42
|
|
|
42
|
0
|
1237
|
my ($self, $data) = @_; |
8
|
42
|
100
|
|
|
|
57
|
if($data) { |
9
|
4
|
50
|
|
|
|
8
|
if($self->{data}) { |
10
|
0
|
|
|
|
|
0
|
die "Can't set view data attribute if its already set"; |
11
|
|
|
|
|
|
|
} else { |
12
|
4
|
100
|
|
|
|
10
|
$data = $self->{ctx}->model($data) unless ref $data; |
13
|
|
|
|
|
|
|
#die "Model $data does not do a required method 'TO_JSON'" |
14
|
|
|
|
|
|
|
# unless $data->can('TO_JSON'); |
15
|
|
|
|
|
|
|
|
16
|
4
|
|
|
|
|
994
|
return $self->{data} = $data; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
} else { |
19
|
38
|
|
66
|
|
|
182
|
return $self->{data} ||= do { |
20
|
4
|
|
|
|
|
16
|
my $default_view_model = $self->{parent}->default_view_model; |
21
|
4
|
50
|
|
|
|
31
|
$default_view_model = $self->{ctx}->model($default_view_model) |
22
|
|
|
|
|
|
|
unless ref $default_view_model; |
23
|
4
|
|
|
|
|
2104
|
$default_view_model; |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub handle_encode_error { |
29
|
3
|
|
|
3
|
0
|
35
|
my ($self, $value) = @_; |
30
|
3
|
100
|
|
|
|
6
|
if(defined $value) { |
31
|
1
|
|
|
|
|
2
|
$self->{handle_encode_error} = $value; |
32
|
|
|
|
|
|
|
} |
33
|
3
|
|
|
|
|
8
|
return $self->{handle_encode_error}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub callback_param { |
37
|
0
|
|
|
0
|
0
|
0
|
my ($self, $value) = @_; |
38
|
0
|
0
|
|
|
|
0
|
if(defined $value) { |
39
|
0
|
|
|
|
|
0
|
$self->{callback_param} = $value; |
40
|
|
|
|
|
|
|
} |
41
|
0
|
|
|
|
|
0
|
return $self->{callback_param}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
0
|
0
|
0
|
sub res { return shift->response(@_) } |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub response { |
47
|
8
|
|
|
8
|
0
|
11
|
my ($self, @proto) = @_; |
48
|
8
|
|
|
|
|
12
|
my ($status, @headers) = (); |
49
|
|
|
|
|
|
|
|
50
|
8
|
50
|
33
|
|
|
52
|
if( (ref \$proto[0] eq 'SCALAR') and |
51
|
|
|
|
|
|
|
Scalar::Util::looks_like_number($proto[0]) |
52
|
|
|
|
|
|
|
){ |
53
|
8
|
|
|
|
|
10
|
$status = shift @proto; |
54
|
|
|
|
|
|
|
} else { |
55
|
0
|
|
|
|
|
0
|
$status = 200; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
8
|
100
|
66
|
|
|
51
|
if( |
|
|
50
|
33
|
|
|
|
|
59
|
|
|
|
|
|
|
scalar(@proto) && |
60
|
|
|
|
|
|
|
ref $proto[$#proto] eq 'HASH' |
61
|
|
|
|
|
|
|
) { |
62
|
5
|
|
|
|
|
24
|
my $var = pop @proto; |
63
|
5
|
|
|
|
|
14
|
foreach my $key (keys %$var) { |
64
|
9
|
50
|
|
|
|
5536
|
if($self->data->can('set')) { |
65
|
9
|
|
|
|
|
14
|
$self->data->set($key,$var->{$key}); |
66
|
|
|
|
|
|
|
} else { |
67
|
0
|
|
|
|
|
0
|
$self->data->$key($var->{$key}); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} elsif( |
71
|
|
|
|
|
|
|
scalar(@proto) && |
72
|
|
|
|
|
|
|
Scalar::Util::blessed($proto[$#proto]) |
73
|
|
|
|
|
|
|
) { |
74
|
3
|
|
|
|
|
4
|
my $obj = pop @proto; |
75
|
3
|
|
|
|
|
9
|
$self->data($obj); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
8
|
50
|
|
|
|
285
|
if(@proto) { |
79
|
0
|
|
|
|
|
0
|
@headers = @proto; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$self->{ctx}->stats->profile(begin => "=> JSON->send". ($status ? "($status)": '')) |
83
|
8
|
0
|
|
|
|
21
|
if $self->{ctx}->debug; |
|
|
50
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
8
|
|
|
|
|
181
|
my $res = $self->{ctx}->response; |
86
|
8
|
|
|
|
|
43
|
my $json = $self->render($self->data); |
87
|
|
|
|
|
|
|
|
88
|
6
|
50
|
|
|
|
11
|
$res->headers->push_header(@headers) if @headers; |
89
|
6
|
50
|
|
|
|
22
|
$res->status($status) unless $res->status != 200; # Catalyst default is 200... |
90
|
6
|
50
|
|
|
|
916
|
$res->content_type('application/json') unless $res->content_type; |
91
|
|
|
|
|
|
|
|
92
|
6
|
50
|
|
|
|
1782
|
if(my $param = $self->{callback_param}) { |
93
|
0
|
|
|
|
|
0
|
my $cb = $c->req->query_parameter($cbparam); |
94
|
0
|
0
|
|
|
|
0
|
$cb =~ /^[a-zA-Z0-9\.\_\[\]]+$/ || die "Invalid callback parameter $cb"; |
95
|
0
|
|
|
|
|
0
|
$json = "$cb($json)"; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
6
|
50
|
|
|
|
16
|
$res->body($json) unless $res->has_body; |
99
|
6
|
50
|
|
|
|
317
|
return $self->{ctx}->detach if $self->{auto_detach}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub render { |
103
|
8
|
|
|
8
|
0
|
8
|
my ($self, $data) = @_; |
104
|
|
|
|
|
|
|
my $json = eval { |
105
|
|
|
|
|
|
|
$self->{json}->encode($self->data); |
106
|
8
|
|
66
|
|
|
7
|
} || do { |
107
|
|
|
|
|
|
|
if(my $cb = $self->handle_encode_error) { |
108
|
|
|
|
|
|
|
delete $self->{data}; # Clear out any existing data since its not valid |
109
|
|
|
|
|
|
|
return $cb->($self, $@); |
110
|
|
|
|
|
|
|
} else { |
111
|
|
|
|
|
|
|
# Bubble up the unhandled error |
112
|
|
|
|
|
|
|
die $@; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
}; |
115
|
6
|
|
|
|
|
291
|
return $json; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub process { |
119
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $c ) = @_; |
120
|
0
|
|
|
|
|
0
|
$self->send; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Send Helpers. |
124
|
|
|
|
|
|
|
foreach my $helper( grep { $_=~/^http/i} @HTTP::Status::EXPORT_OK) { |
125
|
|
|
|
|
|
|
my $subname = lc $helper; |
126
|
|
|
|
|
|
|
$subname =~s/http_//i; |
127
|
0
|
|
|
0
|
0
|
0
|
eval "sub $subname { return shift->response(HTTP::Status::$helper,\@_) }"; |
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
1
|
|
|
1
|
0
|
576
|
|
|
0
|
|
|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
|
|
|
|
|
Catalyst::View::JSON::_PerRequest - Private object for JSON views that own data |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
No user servicable bits |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 DESCRIPTION |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
See L<Catalyst::View::JSON::PerRequest> for details. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 SEE ALSO |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
L<Catalyst>, L<Catalyst::View>, L<Catalyst::View::JSON::PerRequest>, |
148
|
|
|
|
|
|
|
L<HTTP::Status> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 AUTHOR |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
John Napiorkowski L<email:jjnapiork@cpan.org> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under |
159
|
|
|
|
|
|
|
the same terms as Perl itself. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |