| 1 | package JSON::JSend; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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 | 1 | 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 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 55 | 1 | my $self = {}; | |||||||||||
| 56 | 1 | bless $self, $class; | |||||||||||
| 57 | 1 | 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 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
success: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 93 | 2 | ▶ | my $data = shift || undef; | ||||||||||
|
Condition100%2/2
shift() || undef
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 94 | 2 | my $result = { status => 'success', | |||||||||||
| 95 | data => $data }; | ||||||||||||
| 96 | 2 | 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 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
fail: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 121 | 2 | ▶ | my $data = shift || undef; | ||||||||||
|
Condition100%2/2
shift() || undef
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 122 | 2 | my $result = { status => 'fail', | |||||||||||
| 123 | data => $data }; | ||||||||||||
| 124 | 2 | 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 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
error: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 167 | 3 | my $message = shift; | |||||||||||
| 168 | 3 | my $second = shift; | |||||||||||
| 169 | 3 | my $third = shift; | |||||||||||
| 170 | 3 | my ($code, $data); | |||||||||||
| 171 | |||||||||||||
| 172 | 3 | foreach my $param (($second, $third)) { | |||||||||||
| 173 | 6 | ▶ | if (defined($param)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 3 | ▶ | if (ref($param) eq 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 175 | 2 | $data = $param; | |||||||||||
| 176 | } else { | ||||||||||||
| 177 | 1 | $code = $param; | |||||||||||
| 178 | } | ||||||||||||
| 179 | } | ||||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 3 | my $result = { status => "error", | |||||||||||
| 183 | message => $message }; | ||||||||||||
| 184 | 3 | ▶ | $result->{code} = $code if defined($code); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 3 | ▶ | $result->{data} = $data if defined($data); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | 3 | return to_json($result); | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | |||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||
| 191 | |||||||||||||
| 192 | Hoe-Kit Chew, C<< <hoekit at gmail dot com> >> | ||||||||||||
| 193 | |||||||||||||
| 194 | =head1 BUGS | ||||||||||||
| 195 | |||||||||||||
| 196 | Please report any bugs or feature requests to C<bug-json-jsend at | ||||||||||||
| 197 | rt.cpan.org>, or through the web interface at | ||||||||||||
| 198 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-JSend>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JSON-JSend> | ||||||||||||
| 216 | |||||||||||||
| 217 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 218 | |||||||||||||
| 219 | L<http://annocpan.org/dist/JSON-JSend> | ||||||||||||
| 220 | |||||||||||||
| 221 | =item * CPAN Ratings | ||||||||||||
| 222 | |||||||||||||
| 223 | L<http://cpanratings.perl.org/d/JSON-JSend> | ||||||||||||
| 224 | |||||||||||||
| 225 | =item * Search CPAN | ||||||||||||
| 226 | |||||||||||||
| 227 | L<http://search.cpan.org/dist/JSON-JSend/> | ||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||
| 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 | 1; # End of JSON::JSend | |||||||||||