blib/lib/Limper/SendJSON.pm

filter: Statementstmt 65.6% 21 / 32 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 0.0% 0 / 1 totaltotal 61.3% 27 / 44 SCAR 8.0
CC
2
Cov
62%
send_json
6.0
__ANON__
2.0
__ANON__
2.0
CRAP 2.2
↑ Summary
1 package Limper::SendJSON;
2 2 $Limper::SendJSON::VERSION = '0.001';
3 2 ▶use base 'Limper';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Limper;
9 2 $Limper::VERSION = '0.001';
10 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 push @Limper::EXPORT, qw/send_json/;
14  
15 sub send_json {
16 0 ▶ my ($data, @options) = @_;
Subroutine send_json: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my @headers = headers;
18 0 ▶ headers 'Content-Type' => 'application/json', headers unless grep { $_ eq 'Content-Type' } keys %{{&headers}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "Content-Type";} keys %{+{&headers};} 0 0 0
19 try {
20 0 ▶ JSON::MaybeXS->new(@options)->encode($data);
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 } catch {
22 0 ▶ warning $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 headers @headers;
24 0 status 500;
25 0 'Internal Server Error';
26 0 };
27 }
28  
29 2 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.001
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 C<Limper::SendJSON> extends C<Limper> 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 C<HASH> or C<ARRAY> given as JSON. If B<Content-Type> is not
73 already set, it will be set to B<application/json>. Returns B<500> if the
74 scalar cannot be encoded by L<JSON::MaybeXS>.
75  
76 =head1 COPYRIGHT AND LICENSE
77  
78 Copyright (C) 2014 by Ashley Willis E<lt>ashley+perl@gitable.orgE<gt>
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<Limper>
87 L<Limper::SendFile>
88  
89 =cut
↑ Summary