File Coverage

blib/lib/Limper/SendJSON.pm
Criterion Covered Total %
statement 18 29 62.0
branch 0 2 0.0
condition n/a
subroutine 6 9 66.6
pod 0 1 0.0
total 24 41 58.5


line stmt bran cond sub pod time code
1             package Limper::SendJSON;
2             $Limper::SendJSON::VERSION = '0.002';
3 2     2   15325 use base 'Limper';
  2         4  
  2         892  
4 2     2   43760 use 5.10.0;
  2         5  
  2         66  
5 2     2   15 use strict;
  2         3  
  2         42  
6 2     2   8 use warnings;
  2         2  
  2         76  
7              
8             package Limper;
9             $Limper::VERSION = '0.002';
10 2     2   914 use JSON::MaybeXS;
  2         10539  
  2         125  
11 2     2   934 use Try::Tiny;
  2         2274  
  2         435  
12              
13             push @Limper::EXPORT, qw/send_json/;
14              
15             sub send_json {
16 0     0 0   my ($data, @options) = @_;
17 0           my @headers = headers;
18 0 0         headers 'Content-Type' => 'application/json', headers unless grep { $_ eq 'Content-Type' } keys %{{&headers}};
  0            
  0            
19             try {
20 0     0     JSON::MaybeXS->new(@options)->encode($data);
21             } catch {
22 0     0     warning $_;
23 0           headers @headers;
24 0           status 500;
25 0           'Internal Server Error';
26 0           };
27             }
28              
29             1;
30              
31             =for Pod::Coverage
32              
33             =head1 NAME
34              
35             Limper::SendJSON - adds a send_json function to Limper
36              
37             =head1 VERSION
38              
39             version 0.002
40              
41             =head1 SYNOPSIS
42              
43             use Limper::SendJSON;
44             use Limper; # this must come after all extensions
45              
46             # some other routes
47              
48             get '/json' = sub {
49             send_json { foo => 'bar' };
50             };
51              
52             get '/json-pretty' = sub {
53             send_json { foo => 'bar' }, pretty => 1;
54             };
55              
56             limp;
57              
58             =head1 DESCRIPTION
59              
60             B extends L to easily return JSON, with the proper Content-Type header.
61              
62             =head1 EXPORTS
63              
64             The following are all additionally exported by default:
65              
66             send_json
67              
68             =head1 FUNCTIONS
69              
70             =head2 send_json
71              
72             Sends the B or B given as JSON. If B is not
73             already set, it will be set to B. Returns B<500> if the
74             scalar cannot be encoded by L.
75              
76             =head1 COPYRIGHT AND LICENSE
77              
78             Copyright (C) 2014 by Ashley Willis Eashley+perl@gitable.orgE
79              
80             This library is free software; you can redistribute it and/or modify
81             it under the same terms as Perl itself, either Perl version 5.12.4 or,
82             at your option, any later version of Perl 5 you may have available.
83              
84             =head1 SEE ALSO
85              
86             L
87              
88             L
89              
90             L
91              
92             =cut