| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package JSON::JSend; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24615
|
use 5.006; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
26
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
23
|
|
|
5
|
1
|
|
|
1
|
|
2
|
use JSON; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
5
|
|
|
6
|
1
|
|
|
1
|
|
101
|
use warnings FATAL => 'all'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
272
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
JSON::JSend - Simple JSON responses (see: labs.omniti.com/labs/jsend) |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.01 |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use JSON::JSend; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $jsend = JSON::JSend->new(); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$jsend->success(); |
|
28
|
|
|
|
|
|
|
$jsend->success({ |
|
29
|
|
|
|
|
|
|
"post" => { |
|
30
|
|
|
|
|
|
|
id => 1, |
|
31
|
|
|
|
|
|
|
title => 'A blog post', |
|
32
|
|
|
|
|
|
|
body => 'Some useful content' |
|
33
|
|
|
|
|
|
|
}}); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$jsend->fail({ title => 'A title is required' }); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database'); |
|
38
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database', |
|
39
|
|
|
|
|
|
|
50001, |
|
40
|
|
|
|
|
|
|
{ some_extra_info => 'Extra info goes here' }); |
|
41
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database', |
|
42
|
|
|
|
|
|
|
{ some_extra_info => 'Extra info goes here' }); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head2 new () |
|
47
|
|
|
|
|
|
|
$jsend = JSON::JSend->new(); |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Creates a new JSend object. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new { |
|
54
|
1
|
|
|
1
|
1
|
6
|
my $class = shift; |
|
55
|
1
|
|
|
|
|
1
|
my $self = {}; |
|
56
|
1
|
|
|
|
|
2
|
bless $self, $class; |
|
57
|
1
|
|
|
|
|
2
|
return $self; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 success ( [$data] ) |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$jsend->success(); |
|
63
|
|
|
|
|
|
|
$jsend->success({ |
|
64
|
|
|
|
|
|
|
"post" => { |
|
65
|
|
|
|
|
|
|
id => 1, |
|
66
|
|
|
|
|
|
|
title => 'A blog post', |
|
67
|
|
|
|
|
|
|
body => 'Some useful content' |
|
68
|
|
|
|
|
|
|
}}); |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The above commands return the following JSON objects: |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
{ |
|
73
|
|
|
|
|
|
|
"status" : "success", |
|
74
|
|
|
|
|
|
|
"data" : null |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
{ |
|
78
|
|
|
|
|
|
|
"status" : "success", |
|
79
|
|
|
|
|
|
|
"data" : { |
|
80
|
|
|
|
|
|
|
"post" : { |
|
81
|
|
|
|
|
|
|
"id" : 1, |
|
82
|
|
|
|
|
|
|
"title" : "A blog post", |
|
83
|
|
|
|
|
|
|
"body" : "Some useful content" |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
The status:"success" key-value pair is automatically included in the success response. |
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub success { |
|
92
|
2
|
|
|
2
|
1
|
575
|
my $self = shift; |
|
93
|
2
|
|
100
|
|
|
6
|
my $data = shift || undef; |
|
94
|
2
|
|
|
|
|
4
|
my $result = { status => 'success', |
|
95
|
|
|
|
|
|
|
data => $data }; |
|
96
|
2
|
|
|
|
|
4
|
return to_json($result); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 fail ( [$data] ) |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$jsend->fail(); |
|
102
|
|
|
|
|
|
|
$jsend->fail({ title => 'A title is required' }); |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
The above commands return the following JSON objects: |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
{ |
|
107
|
|
|
|
|
|
|
"status" : "fail", |
|
108
|
|
|
|
|
|
|
"data" : null |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
{ |
|
112
|
|
|
|
|
|
|
"status" : "fail", |
|
113
|
|
|
|
|
|
|
"data" : { "title" : "A title is required" } |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The status:"fail" key-value pair is automatically included in the fail response. |
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub fail { |
|
120
|
2
|
|
|
2
|
1
|
760
|
my $self = shift; |
|
121
|
2
|
|
100
|
|
|
6
|
my $data = shift || undef; |
|
122
|
2
|
|
|
|
|
3
|
my $result = { status => 'fail', |
|
123
|
|
|
|
|
|
|
data => $data }; |
|
124
|
2
|
|
|
|
|
6
|
return to_json($result); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 error ( $message, [$code|$data], [$code|$data] ) |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database'); |
|
130
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database', |
|
131
|
|
|
|
|
|
|
50001, |
|
132
|
|
|
|
|
|
|
{ some_extra_info => 'Extra info goes here' }); |
|
133
|
|
|
|
|
|
|
$jsend->error( 'Unable to communicate with the database', |
|
134
|
|
|
|
|
|
|
{ some_extra_info => 'Extra info goes here' }); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
The first parameter, $message, is mandatory. The second and third optional |
|
137
|
|
|
|
|
|
|
parameters, if defined, contain either, $code, the numeric error code or |
|
138
|
|
|
|
|
|
|
$data, additional data. The $code parameter must be a scalar while the $data |
|
139
|
|
|
|
|
|
|
parameter must be a hashref. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
The above commands return the following JSON objects |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
{ |
|
144
|
|
|
|
|
|
|
"status" : "error", |
|
145
|
|
|
|
|
|
|
"message" : "Unable to communicate with the database" |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
{ |
|
149
|
|
|
|
|
|
|
"status" : "error", |
|
150
|
|
|
|
|
|
|
"message" : "Unable to communicate with the database", |
|
151
|
|
|
|
|
|
|
"code" : 50001, |
|
152
|
|
|
|
|
|
|
"data" : { "some_extra_info" : "Extra info goes here" } |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
{ |
|
156
|
|
|
|
|
|
|
"status" : "error", |
|
157
|
|
|
|
|
|
|
"message" : "Unable to communicate with the database" |
|
158
|
|
|
|
|
|
|
"data" : { "some_extra_info" : "Extra info goes here" } |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
The "status":"error" key-value pair is included automatically in the error |
|
162
|
|
|
|
|
|
|
response. |
|
163
|
|
|
|
|
|
|
=cut |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub error { |
|
166
|
3
|
|
|
3
|
1
|
1107
|
my $self = shift; |
|
167
|
3
|
|
|
|
|
2
|
my $message = shift; |
|
168
|
3
|
|
|
|
|
3
|
my $second = shift; |
|
169
|
3
|
|
|
|
|
3
|
my $third = shift; |
|
170
|
3
|
|
|
|
|
2
|
my ($code, $data); |
|
171
|
|
|
|
|
|
|
|
|
172
|
3
|
|
|
|
|
4
|
foreach my $param (($second, $third)) { |
|
173
|
6
|
100
|
|
|
|
10
|
if (defined($param)) { |
|
174
|
3
|
100
|
|
|
|
6
|
if (ref($param) eq 'HASH') { |
|
175
|
2
|
|
|
|
|
3
|
$data = $param; |
|
176
|
|
|
|
|
|
|
} else { |
|
177
|
1
|
|
|
|
|
2
|
$code = $param; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
3
|
|
|
|
|
5
|
my $result = { status => "error", |
|
183
|
|
|
|
|
|
|
message => $message }; |
|
184
|
3
|
100
|
|
|
|
5
|
$result->{code} = $code if defined($code); |
|
185
|
3
|
100
|
|
|
|
5
|
$result->{data} = $data if defined($data); |
|
186
|
3
|
|
|
|
|
5
|
return to_json($result); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 AUTHOR |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Hoe-Kit Chew, C<< >> |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 BUGS |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Please report any bugs or feature requests to C
|
|
197
|
|
|
|
|
|
|
rt.cpan.org>, or through the web interface at |
|
198
|
|
|
|
|
|
|
L. I will be |
|
199
|
|
|
|
|
|
|
notified, and then you'll automatically be notified of progress on your bug as |
|
200
|
|
|
|
|
|
|
I make changes. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 SUPPORT |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
perldoc JSON::JSend |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
You can also look for information at: |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=over 4 |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
L |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * Search CPAN |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=back |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Copyright 2015 Hoe-Kit Chew. |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
240
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
|
241
|
|
|
|
|
|
|
copy of the full license at: |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
L |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
|
246
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
|
247
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
|
248
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
|
251
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
|
252
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
|
255
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
|
258
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
|
259
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
|
260
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
|
261
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
|
262
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
|
263
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
|
264
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
|
267
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
|
268
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
|
269
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
|
270
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
|
271
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
|
272
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
|
273
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=cut |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
1; # End of JSON::JSend |