HTTP/Request/JSON.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 52 / 52 SCAR 6.9
CC
2
Cov
100%
json_content
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« HTTP/Message/JSON.pm ↑ Summary
1 package HTTP::Request::JSON;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use parent 'HTTP::Message::JSON', 'HTTP::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = $LWP::JSON::Tiny::VERSION;
10  
11 6 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use LWP::JSON::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 HTTP::Request::JSON - a subclass of HTTP::Request that understands JSON
18  
19 =head1 SYNOPSIS
20  
21 my $request = HTTP::Request::JSON->new(PATCH => "$base_url/death_ray");
22 # $request has an Accept header saying it's OK to send JSON back
23 $request->json_content(
24 {
25 self_destruct_mechanism => 'disabled',
26 users_allowed_to_override => [],
27 }
28 );
29 # Request content is JSON-encoded, and the content-type is set.
30  
31 =head1 DESCRIPTION
32  
33 This is a simple subclass of HTTP::Request::JSON that does two things.
34 First of all, it sets the Accept header to C<application/json> as soon
35 as it's created. Secondly, it implements a L</json_content>
36 method that adds the supplied data structure to the request, as JSON,
37 or returns the current JSON contents as a Perl structure.
38  
39 =head2 new
40  
41 In: ...
42 Out: $request
43  
44 As HTTP::Request->new, but also sets the Accept header.
45  
46 =cut
47  
48 sub new {
49 13 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 13 my $self = $class->SUPER::new(@_);
51 13 $self->header('Accept' => 'application/json');
52 13 return $self;
53 }
54  
55 =head2 json_content
56  
57 In: $perl_data (optional)
58 Out: $converted_content
59  
60 A mutator for the request's JSON contents.
61  
62 As a setter, supplied with a valid JSON data structure, sets the request
63 contents to be the JSON-encoded version of that data structure, and sets the
64 Content-Type header to C<application/json>. Will throw an exception if the
65 data structure cannot be converted to JSON. Returns the resulting string
66 contents.
67  
68 All strings in $perl_data must be Unicode strings, or you will get
69 encoding errors.
70  
71 As a getter, decodes the request contents from JSON
72 into a Perl structure, and returns the resulting data structure.
73  
74 =cut
75  
76 sub json_content {
77 29 ▶ my $self = shift;
Subroutine json_content: called CC=2 SCAR=6.9
Pod documented
78  
79 29 my $json = $self->json_object;
80  
81 # Setter
82 29 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 27 2 29
83 27 $self->content(Encode::encode('UTF8', $json->encode(shift)));
84 26 $self->content_type('application/json');
85 26 return $self->decoded_content;
86 }
87  
88 # Getter
89 2 my $perl_data = $json->decode($self->decoded_content);
90 2 return $perl_data;
91 }
92  
93 =head2 json_object
94  
95 Out: $json_object
96  
97 Returns an object that knows how to handle the C<encode> and C<decode>
98 methods. By default whatever LWP::JSON::Tiny->json_object returns.
99 This is what you'd subclass if you wanted to use some other kind of JSON
100 object instead.
101  
102 =cut
103  
104 sub json_object {
105 20 ▶ my ($self) = @_;
Subroutine json_object: called CC=1 SCAR=0.0
Pod documented
106  
107 20 return LWP::JSON::Tiny->json_object;
108 }
109  
110 =head1 AUTHOR
111  
112 Sam Kington <skington@cpan.org>
113  
114 The source code for this module is hosted on GitHub
115 L<https://github.com/skington/lwp-json-tiny> - this is probably the
116 best place to look for suggestions and feedback.
117  
118 =head1 COPYRIGHT
119  
120 Copyright (c) 2015 Sam Kington.
121  
122 =head1 LICENSE
123  
124 This library is free software and may be distributed under the same terms as
125 perl itself.
126  
127 =cut
128  
129 6 1;
« HTTP/Message/JSON.pm ↑ Summary