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